-
Notifications
You must be signed in to change notification settings - Fork 10
/
detector.go
125 lines (108 loc) · 2.74 KB
/
detector.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
package botdetector
import (
"net/http"
"regexp"
lru "github.com/hashicorp/golang-lru"
)
// Config defines the behaviour of the detector
type Config struct {
Denylist []string `json:"deny"`
Allowlist []string `json:"allow"`
Patterns []string `json:"patterns"`
CacheSize int `json:"cache_size"`
RejectIfEmpty bool `json:"empty_user_agent_is_bot"`
}
// DetectorFunc is a func that chek if a request was made by a bot
type DetectorFunc func(r *http.Request) bool
// New returns a detector function with or without LRU cache depending on the params
func New(cfg Config) (DetectorFunc, error) {
if cfg.CacheSize == 0 {
d, err := NewDetector(cfg)
return d.IsBot, err
}
d, err := NewLRU(cfg)
return d.IsBot, err
}
// NewDetector creates a Detector
func NewDetector(cfg Config) (*Detector, error) {
deny := make(map[string]struct{}, len(cfg.Denylist))
for _, e := range cfg.Denylist {
deny[e] = struct{}{}
}
allow := make(map[string]struct{}, len(cfg.Allowlist))
for _, e := range cfg.Allowlist {
allow[e] = struct{}{}
}
patterns := make([]*regexp.Regexp, len(cfg.Patterns))
for i, p := range cfg.Patterns {
rp, err := regexp.Compile(p)
if err != nil {
return nil, err
}
patterns[i] = rp
}
return &Detector{
deny: deny,
allow: allow,
patterns: patterns,
rejectIfEmpty: cfg.RejectIfEmpty,
}, nil
}
// Detector is a struct able to detect bot-made requests
type Detector struct {
deny map[string]struct{}
allow map[string]struct{}
patterns []*regexp.Regexp
rejectIfEmpty bool
}
// IsBot returns true if the request was made by a bot
func (d *Detector) IsBot(r *http.Request) bool {
userAgent := r.Header.Get("User-Agent")
if userAgent == "" {
return d.rejectIfEmpty
}
if _, ok := d.allow[userAgent]; ok {
return false
}
if _, ok := d.deny[userAgent]; ok {
return true
}
for _, p := range d.patterns {
if p.MatchString(userAgent) {
return true
}
}
return false
}
// NewLRU creates a new LRUDetector
func NewLRU(cfg Config) (*LRUDetector, error) {
d, err := NewDetector(cfg)
if err != nil {
return nil, err
}
cache, err := lru.New(cfg.CacheSize)
if err != nil {
return nil, err
}
return &LRUDetector{
detectorFunc: d.IsBot,
cache: cache,
}, nil
}
// LRUDetector is a struct able to detect bot-made requests and cache the results
// for future reutilization
type LRUDetector struct {
detectorFunc DetectorFunc
cache *lru.Cache
}
// IsBot returns true if the request was made by a bot
func (d *LRUDetector) IsBot(r *http.Request) bool {
userAgent := r.Header.Get("User-Agent")
cached, ok := d.cache.Get(userAgent)
if ok {
return cached.(bool)
}
res := d.detectorFunc(r)
d.cache.Add(userAgent, res)
return res
}