This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics.go
363 lines (319 loc) · 9.31 KB
/
statistics.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"crypto/tls"
"net"
"strings"
ua "github.com/mileusna/useragent"
"golang.org/x/text/language"
"golang.org/x/text/language/display"
)
type statistics struct {
Directory string `json:"directory"`
SizeBytes int64 `json:"sizeBytes"`
ParseDuration float64 `json:"parseDuration"`
FirstStamp int64 `json:"firstStamp"`
LastStamp int64 `json:"lastStamp"`
Hosts map[string]*host `json:"hosts"`
}
type hour uint64
type host struct {
Total metrics `json:"total"`
Hourly map[hour]*metrics `json:"hourly"`
Visitors visitors `json:"visitors"`
Requests requests `json:"requests"`
}
type metrics struct {
Requests int64 `json:"requests"`
Latency float64 `json:"latency"`
SentBytes int64 `json:"sentBytes"`
Visitors int64 `json:"visitors"`
ObservedUsers map[uniqueVisitor]bool `json:"-"`
ObservedBots map[uniqueVisitor]bool `json:"-"`
}
type uniqueVisitor struct {
IPAddress string
RawUserAgent string
}
type visitors struct {
Bots map[string]int64 `json:"bots"`
Devices map[string]int64 `json:"devices"`
Browsers map[string]int64 `json:"browsers"`
Systems map[string]int64 `json:"systems"`
Languages map[string]int64 `json:"languages"`
Countries map[string]int64 `json:"countries"`
Encodings map[string]int64 `json:"encodings"`
}
type requests struct {
Protocols map[string]int64 `json:"protocols"`
Methods map[string]int64 `json:"methods"`
Cryptos map[string]int64 `json:"cryptos"`
Ciphers map[string]int64 `json:"ciphers"`
Contents map[string]int64 `json:"contents"`
Locations map[string]*statuses `json:"locations"`
}
type statuses [6]int64
func newStatistics() *statistics {
return &statistics{
Hosts: make(map[string]*host),
}
}
func newHost() *host {
return &host{
Total: *newMetrics(),
Hourly: make(map[hour]*metrics),
Visitors: *newVisitors(),
Requests: *newRequests(),
}
}
func newMetrics() *metrics {
return &metrics{
ObservedUsers: make(map[uniqueVisitor]bool),
ObservedBots: make(map[uniqueVisitor]bool),
}
}
func newVisitors() *visitors {
return &visitors{
Bots: make(map[string]int64),
Devices: make(map[string]int64),
Browsers: make(map[string]int64),
Systems: make(map[string]int64),
Languages: make(map[string]int64),
Countries: make(map[string]int64),
Encodings: make(map[string]int64),
}
}
func newRequests() *requests {
return &requests{
Protocols: make(map[string]int64),
Methods: make(map[string]int64),
Cryptos: make(map[string]int64),
Ciphers: make(map[string]int64),
Contents: make(map[string]int64),
Locations: make(map[string]*statuses),
}
}
func newStatuses() *statuses {
var array [6]int64
statuses := statuses(array)
return &statuses
}
// Convert a unix timestamp to the hour it is in
func unixToHour(unix float64) hour {
seconds := hour(unix)
rounded := seconds - (seconds % (60 * 60))
return rounded
}
// Remove the port suffix from a string if there is one
func stripPortSuffix(str string) string {
host, _, err := net.SplitHostPort(str)
if err != nil {
return str
}
return host
}
// Remove the http(s) prefix from a string if there is one
func stripHTTPPrefix(str string) string {
if strings.HasPrefix(str, "http://") {
return str[7:]
} else if strings.HasPrefix(str, "https://") {
return str[8:]
} else {
return str
}
}
// Get the first user agent from a slice of user agents
func getRawUserAgent(slc []string) string {
if len(slc) > 0 {
return slc[0]
}
return ""
}
// Get preferred language from a slice of Accept-Language headers
func getPreferredLanguage(slc []string) string {
if len(slc) > 0 {
raw := slc[0]
comma := strings.IndexRune(raw, ',')
if comma > 0 {
raw = raw[:comma]
}
dash := strings.IndexRune(raw, '-')
if dash > 0 {
raw = raw[:dash]
}
tag, err := language.Parse(raw)
if err != nil {
return "Unknown"
}
lang := display.English.Tags().Name(tag)
return lang
}
return "None"
}
// Get supported encoding/compression schemes from Accept-Encodings header
func getSupportedEncodings(slc []string) []string {
if len(slc) > 0 {
slc := strings.Split(slc[0], ",")
var clean []string
for _, enc := range slc {
enc = strings.TrimSpace(enc)
switch enc {
case "identity": // Do nothing, everyone supports this
case "utf-8": // Do nothing, everyone supports this
case "gzip":
clean = append(clean, "Gzip")
case "deflate":
clean = append(clean, "Deflate")
case "br":
clean = append(clean, "Brotli")
case "snappy":
clean = append(clean, "Snappy")
case "sdch":
clean = append(clean, "SDCH")
default:
clean = append(clean, enc)
}
}
return clean
}
return make([]string, 0)
}
// Get content type from response header
func getContentType(slc []string) string {
if len(slc) > 0 {
raw := slc[0]
semicolon := strings.IndexRune(raw, ';')
if semicolon > 0 {
raw = raw[:semicolon]
}
return raw
}
return "none"
}
// Convert crypto/tls protocol version to human-readable string
func getProtocolFromVersion(version int) string {
switch version {
case 0x0300:
return "SSL v3.0"
case 0x0301:
return "TLS v1.0"
case 0x0302:
return "TLS v1.1"
case 0x0303:
return "TLS v1.2"
case 0x0304:
return "TLS v1.3"
default:
return "Unknown"
}
}
// Add a log entry to an instance of statistics
func addToStats(entry *logEntry, stats *statistics) error {
// Get counter corresponding to host
hostname := stripPortSuffix(entry.Request.Host)
host := stats.Hosts[hostname]
if host == nil {
host = newHost()
stats.Hosts[hostname] = host
}
// Add general stats
host.Total.Requests++
host.Total.SentBytes += entry.Size
host.Total.Latency += entry.Duration
// Check if the visitor has not been seen yet
ipAddress := stripPortSuffix(entry.Request.Address)
userAgent := getRawUserAgent(entry.Request.Headers.UserAgent)
uniqueVisitor := uniqueVisitor{ipAddress, userAgent}
neverObservedBefore := !host.Total.ObservedUsers[uniqueVisitor] && !host.Total.ObservedBots[uniqueVisitor]
if neverObservedBefore {
uaInfo := ua.Parse(userAgent)
// Get visitor's browser name
browser := uaInfo.Name
if browser == "" {
browser = "Unknown"
}
// Check if visitor is a bot and add stats if not
if uaInfo.Bot {
host.Visitors.Bots[browser]++
host.Total.ObservedBots[uniqueVisitor] = true
} else {
host.Visitors.Browsers[browser]++
// Get visitor device type
if uaInfo.Tablet {
host.Visitors.Devices["Tablet"]++
} else if uaInfo.Mobile {
host.Visitors.Devices["Mobile"]++
} else if uaInfo.Desktop {
host.Visitors.Devices["Desktop"]++
} else {
host.Visitors.Devices["Other"]++
}
// Get visitor operating system
os := uaInfo.OS
if os == "" {
os = "Unknown"
}
host.Visitors.Systems[os]++
// Get the main/preferred language of the visitor
language := getPreferredLanguage(entry.Request.Headers.Languages)
host.Visitors.Languages[language]++
// Get all the encodings the visitor supports
encodings := getSupportedEncodings(entry.Request.Headers.Encodings)
for _, enc := range encodings {
host.Visitors.Encodings[enc]++
}
host.Total.Visitors++
host.Total.ObservedUsers[uniqueVisitor] = true
}
}
// Get stats counter corresponding with the hour of the timestamp
hourStamp := unixToHour(entry.Stamp)
hour := host.Hourly[hourStamp]
if hour == nil {
hour = newMetrics()
host.Hourly[hourStamp] = hour
}
// Add general stats to the hourly counter
hour.Requests++
hour.SentBytes += entry.Size
hour.Latency += entry.Duration
neverObservedInHour := !hour.ObservedUsers[uniqueVisitor] && !hour.ObservedBots[uniqueVisitor]
if neverObservedInHour {
uaInfo := ua.Parse(userAgent)
if !uaInfo.Bot {
hour.Visitors++
hour.ObservedUsers[uniqueVisitor] = true
} else {
hour.ObservedBots[uniqueVisitor] = true
}
}
// Add crypto protocol and cipher stats
cipher := tls.CipherSuiteName(entry.Request.Encryption.Cipher)
host.Requests.Ciphers[cipher]++
crypto := getProtocolFromVersion(int(entry.Request.Encryption.Version))
host.Requests.Cryptos[crypto]++
// Add content type stats
content := getContentType(entry.Response.ContentType)
host.Requests.Contents[content]++
// Add location stats with the status code
location := entry.Request.Location
status := int(entry.Status / 100)
statuses := host.Requests.Locations[location]
if statuses == nil {
statuses = newStatuses()
host.Requests.Locations[location] = statuses
}
statuses[status]++
// Add HTTP method stats
host.Requests.Methods[entry.Request.Method]++
// Add HTTP protocol stats
host.Requests.Protocols[entry.Request.Protocol]++
// Change timestamp if current one lies outside the current boundaries
stamp := int64(entry.Stamp)
if stats.FirstStamp > stamp || stats.FirstStamp == 0 {
stats.FirstStamp = stamp
}
if stats.LastStamp < stamp || stats.LastStamp == 0 {
stats.LastStamp = stamp
}
return nil
}