-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache.go
86 lines (70 loc) · 1.42 KB
/
cache.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
package caddy_ja3
import (
"sync"
"github.com/caddyserver/caddy/v2"
"github.com/dreadl0ck/ja3"
"github.com/dreadl0ck/tlsx"
)
const (
CacheAppId = "ja3.cache"
)
func init() {
caddy.RegisterModule(Cache{})
}
type Cache struct {
ja3 map[string]string
ja3Lock sync.RWMutex
}
func (c *Cache) Provision(ctx caddy.Context) error {
c.ja3 = make(map[string]string)
return nil
}
func (c *Cache) SetClientHello(addr string, ch []byte) error {
c.ja3Lock.Lock()
defer c.ja3Lock.Unlock()
parsedCh := &tlsx.ClientHelloBasic{}
if err := parsedCh.Unmarshal(ch); err != nil {
return err
}
c.ja3[addr] = ja3.DigestHex(parsedCh)
return nil
}
func (c *Cache) ClearJA3(addr string) {
c.ja3Lock.Lock()
defer c.ja3Lock.Unlock()
delete(c.ja3, addr)
}
func (c *Cache) GetJA3(addr string) *string {
c.ja3Lock.RLock()
defer c.ja3Lock.RUnlock()
if md5, found := c.ja3[addr]; found {
return &md5
} else {
return nil
}
}
// CaddyModule implements caddy.Module
func (Cache) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: CacheAppId,
New: func() caddy.Module {
return &Cache{
ja3: make(map[string]string),
ja3Lock: sync.RWMutex{},
}
},
}
}
// Start implements caddy.App
func (c *Cache) Start() error {
return nil
}
// Stop implements caddy.App
func (c *Cache) Stop() error {
return nil
}
// Interface guards
var (
_ caddy.App = (*Cache)(nil)
_ caddy.Provisioner = (*Cache)(nil)
)