This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
116 lines (104 loc) · 2.58 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/CapacitorSet/ja3-server/crypto/tls"
"github.com/CapacitorSet/ja3-server/net/http"
"github.com/go-redis/redis"
"net"
"os"
"strconv"
)
var client *redis.Client
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET")
hash := md5.Sum([]byte(r.JA3Fingerprint))
out := make([]byte, 32)
hex.Encode(out, hash[:])
if r.URL.Path == "/cached" {
// Prevent results from being registered twice
w.Header().Set("Cache-Control", "public,max-age=31556926,immutable")
w.Header().Set("Expires", "Mon, 30 Dec 2019 08:00:00 GMT")
w.Header().Set("Last-Modified", "Sun, 30 Dec 2018 08:00:00 GMT")
w.WriteHeader(200)
w.Write(out)
_, err := client.HIncrBy("freqs", string(out), 1).Result()
if err != nil {
fmt.Println(err)
return
}
_, err = client.HIncrBy("freqs", "total", 1).Result()
if err != nil {
fmt.Println(err)
return
}
} else {
num, err := client.HGet("freqs", string(out)).Result()
if err != nil {
fmt.Println(err)
w.WriteHeader(200)
w.Write([]byte("error"))
return
}
numF, err := strconv.ParseFloat(num, 64)
if err != nil {
fmt.Println(err)
w.WriteHeader(200)
w.Write([]byte("error"))
return
}
total, err := client.HGet("freqs", "total").Result()
if err != nil {
fmt.Println(err)
w.WriteHeader(200)
w.Write([]byte("error"))
return
}
totalF, err := strconv.ParseFloat(total, 64)
if err != nil {
fmt.Println(err)
w.WriteHeader(200)
w.Write([]byte("error"))
return
}
fmt.Fprintf(w, "%.2f", totalF/numF)
}
}
func main() {
if len(os.Args) != 4 {
fmt.Printf("Syntax: %s redis_ip:redis_port path/to/certificate.pem path/to/key.pem\n", os.Args[0])
return
}
client = redis.NewClient(&redis.Options{
Addr: os.Args[1],
Password: "", // no password set
DB: 0, // use default DB
})
_, err := client.Ping().Result()
if err != nil {
panic(err)
}
fmt.Println("Redis up.")
handler := http.HandlerFunc(handler)
server := &http.Server{Addr: ":8443", Handler: handler}
ln, err := net.Listen("tcp", ":8443")
if err != nil {
panic(err)
}
defer ln.Close()
cert, err := tls.LoadX509KeyPair(os.Args[2], os.Args[3])
if err != nil {
panic(err)
}
tlsConfig := tls.Config{Certificates: []tls.Certificate{cert}}
tlsListener := tls.NewListener(ln, &tlsConfig)
fmt.Println("HTTP up.")
err = server.Serve(tlsListener)
if err != nil {
panic(err)
}
ln.Close()
}