-
Notifications
You must be signed in to change notification settings - Fork 36
/
database.go
executable file
·267 lines (225 loc) · 5.37 KB
/
database.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"fmt"
"log"
"net/url"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
)
type RequestLog struct {
UserAgent string `bson:"user_agent"`
JA3 string `bson:"ja3"`
H2 string `bson:"h2"`
PeetPrint string `bson:"peetprint"`
IP string `bson:"ip"`
Time int64
}
type ByJA3 struct {
JA3 string `json:"ja3"`
H2 map[string]int `json:"h2_fps"`
PeetPrint map[string]int `json:"peet_prints"`
UserAgents map[string]int `json:"user_agents"`
}
type ByPeetPrint struct {
PeetPrint string `json:"peet_print"`
JA3 map[string]int `json:"ja3s"`
H2 map[string]int `json:"h2_fps"`
UserAgents map[string]int `json:"user_agents"`
}
type ByH2 struct {
H2 string `json:"h2_fp"`
JA3 map[string]int `json:"ja3s"`
PeetPrint map[string]int `json:"peet_prints"`
UserAgents map[string]int `json:"user_agents"`
}
type ByUserAgent struct {
UserAgent string `json:"useragent"`
H2 map[string]int `json:"h2_fps"`
JA3 map[string]int `json:"ja3s"`
PeetPrint map[string]int `json:"peet_prints"`
}
func SaveRequest(req Response) {
reqLog := RequestLog{
JA3: req.TLS.JA3,
PeetPrint: req.TLS.PeetPrint,
Time: time.Now().Unix(),
}
if req.HTTPVersion == "h2" {
reqLog.H2 = req.Http2.AkamaiFingerprint
} else if req.HTTPVersion == "http/1.1" {
reqLog.H2 = "-"
}
if LoadedConfig.LogIPs {
parts := strings.Split(req.IP, ":")
ip := strings.Join(parts[0:len(parts)-1], ":")
reqLog.IP = ip
}
reqLog.UserAgent = GetUserAgent(req)
_, err := collection.InsertOne(ctx, reqLog)
if err != nil {
log.Println(err)
}
}
func GetTotalRequestCount() int64 {
if !connectedToDB {
return 999
}
itemCount, err := collection.CountDocuments(ctx, bson.M{})
if err != nil {
log.Println(err)
return -1
}
return itemCount
}
func queryDB(query, val string) []RequestLog {
dbRes := []RequestLog{}
cur, err := collection.Find(ctx, bson.D{{Key: query, Value: val}})
if err != nil {
log.Println("Error quering data:", err)
return dbRes
}
for cur.Next(ctx) {
var b RequestLog
err := cur.Decode(&b)
if err != nil {
log.Println("Error decoding:", err)
return dbRes
}
dbRes = append(dbRes, b)
}
if err := cur.Err(); err != nil {
log.Println("Error - cur.Err()", err)
return dbRes
}
if cur.Close(ctx) != nil {
log.Println("Could not close")
}
return dbRes
}
const COUNT = 10
func GetByJa3(val string) ByJA3 {
res := ByJA3{
JA3: val,
H2: map[string]int{},
PeetPrint: map[string]int{},
UserAgents: map[string]int{},
}
dbRes := queryDB("ja3", val)
for _, r := range dbRes {
if v, ok := res.H2[r.H2]; ok {
res.H2[r.H2] = v + 1
} else {
res.H2[r.H2] = 1
}
if v, ok := res.PeetPrint[r.PeetPrint]; ok {
res.PeetPrint[r.PeetPrint] = v + 1
} else {
res.PeetPrint[r.PeetPrint] = 1
}
if v, ok := res.UserAgents[r.UserAgent]; ok {
res.UserAgents[r.UserAgent] = v + 1
} else {
res.UserAgents[r.UserAgent] = 1
}
}
res.PeetPrint = sortByVal(res.PeetPrint, COUNT)
res.H2 = sortByVal(res.H2, COUNT)
res.UserAgents = sortByVal(res.UserAgents, COUNT)
return res
}
func GetByH2(val string) ByH2 {
res := ByH2{
H2: val,
JA3: map[string]int{},
PeetPrint: map[string]int{},
UserAgents: map[string]int{},
}
dbRes := queryDB("h2", val)
for _, r := range dbRes {
if v, ok := res.JA3[r.JA3]; ok {
res.JA3[r.JA3] = v + 1
} else {
res.JA3[r.JA3] = 1
}
if v, ok := res.PeetPrint[r.PeetPrint]; ok {
res.PeetPrint[r.PeetPrint] = v + 1
} else {
res.PeetPrint[r.PeetPrint] = 1
}
if v, ok := res.UserAgents[r.UserAgent]; ok {
res.UserAgents[r.UserAgent] = v + 1
} else {
res.UserAgents[r.UserAgent] = 1
}
}
res.PeetPrint = sortByVal(res.PeetPrint, COUNT)
res.JA3 = sortByVal(res.JA3, COUNT)
res.UserAgents = sortByVal(res.UserAgents, COUNT)
return res
}
func GetByPeetPrint(val string) ByPeetPrint {
res := ByPeetPrint{
PeetPrint: val,
H2: map[string]int{},
JA3: map[string]int{},
UserAgents: map[string]int{},
}
dbRes := queryDB("peetprint", val)
for _, r := range dbRes {
if v, ok := res.H2[r.H2]; ok {
res.H2[r.H2] = v + 1
} else {
res.H2[r.H2] = 1
}
if v, ok := res.JA3[r.JA3]; ok {
res.JA3[r.JA3] = v + 1
} else {
res.JA3[r.JA3] = 1
}
if v, ok := res.UserAgents[r.UserAgent]; ok {
res.UserAgents[r.UserAgent] = v + 1
} else {
res.UserAgents[r.UserAgent] = 1
}
}
res.JA3 = sortByVal(res.JA3, COUNT)
res.H2 = sortByVal(res.H2, COUNT)
res.UserAgents = sortByVal(res.UserAgents, COUNT)
return res
}
func GetByUserAgent(val string) ByUserAgent {
res := ByUserAgent{
UserAgent: val,
H2: map[string]int{},
JA3: map[string]int{},
PeetPrint: map[string]int{},
}
decodedValue, err := url.QueryUnescape(val)
if err != nil {
return res
}
fmt.Println(val)
dbRes := queryDB("user_agent", decodedValue)
for _, r := range dbRes {
if v, ok := res.H2[r.H2]; ok {
res.H2[r.H2] = v + 1
} else {
res.H2[r.H2] = 1
}
if v, ok := res.JA3[r.JA3]; ok {
res.JA3[r.JA3] = v + 1
} else {
res.JA3[r.JA3] = 1
}
if v, ok := res.PeetPrint[r.PeetPrint]; ok {
res.PeetPrint[r.PeetPrint] = v + 1
} else {
res.PeetPrint[r.PeetPrint] = 1
}
}
res.JA3 = sortByVal(res.JA3, COUNT)
res.H2 = sortByVal(res.H2, COUNT)
res.PeetPrint = sortByVal(res.PeetPrint, COUNT)
return res
}