forked from getlantern/lantern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useragents.go
48 lines (42 loc) · 1.03 KB
/
useragents.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
package logging
import (
"bytes"
"fmt"
"regexp"
"sync"
)
var (
userAgents = make(map[string]int)
agentsMutex = &sync.Mutex{}
reg = regexp.MustCompile("^Go.*package http$")
)
// registerUserAgent tries to find the User-Agent in the HTTP request
// and keep track of the applications using Lantern during this session
func RegisterUserAgent(agent string) {
// Do this asynchronously because it is not a critical operation,
// so there is no wait for the mutex in the caller goroutine
go func() {
if agent != "" {
agentsMutex.Lock()
defer agentsMutex.Unlock()
if n, ok := userAgents[agent]; ok {
userAgents[agent] = n + 1
} else {
userAgents[agent] = 1
}
}
}()
}
// getSessionUserAgents returns the
func getSessionUserAgents() string {
agentsMutex.Lock()
defer agentsMutex.Unlock()
var buffer bytes.Buffer
for key, val := range userAgents {
if !reg.MatchString(key) {
buffer.WriteString(key)
buffer.WriteString(fmt.Sprintf(": %d requests; ", val))
}
}
return string(buffer.String())
}