-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.go
73 lines (61 loc) · 1.88 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/wi1dcard/fingerproxy/pkg/metadata"
)
func echoServer(w http.ResponseWriter, req *http.Request) {
// create logger for this request, it outputs logs with client IP and port as prefix
logger := log.New(os.Stdout, fmt.Sprintf("[client %s] ", req.RemoteAddr), log.LstdFlags|log.Lmsgprefix)
// get metadata from request context
data, ok := metadata.FromContext(req.Context())
if !ok {
logger.Printf("failed to get context")
http.Error(w, "failed to get context", http.StatusInternalServerError)
return
}
// prepare response
response := &echoResponse{
log: logger,
Detail: &detailResponse{
Metadata: data,
UserAgent: req.UserAgent(),
},
}
// calculate and add fingerprints to the response
if err := response.fingerprintJA3(); err != nil {
logger.Printf(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := response.fingerprintJA4(); err != nil {
logger.Printf(err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
response.fingerrpintHTTP2()
// print detail if -verbose is specified in CLI
if *flagVerbose {
detail, _ := json.Marshal(response.Detail)
logger.Printf("detail: %s", detail)
}
// send HTTP response
switch req.URL.Path {
case "/json":
w.Header().Set("Content-Type", "application/json")
response.Detail = nil
json.NewEncoder(w).Encode(response)
case "/json/detail":
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
default:
fmt.Fprintf(w, "User-Agent: %s\n", response.Detail.UserAgent)
fmt.Fprintf(w, "TLS ClientHello Record: %x\n", response.Detail.Metadata.ClientHelloRecord)
fmt.Fprintf(w, "JA3 fingerprint: %s\n", response.JA3)
fmt.Fprintf(w, "JA4 fingerprint: %s\n", response.JA4)
fmt.Fprintf(w, "HTTP2 fingerprint: %s\n", response.HTTP2)
}
}