-
Notifications
You must be signed in to change notification settings - Fork 2
/
detector.go
152 lines (130 loc) · 3.12 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
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
package botdetector
import (
lru "github.com/hashicorp/golang-lru/v2"
"strings"
)
const (
strict = iota
startWith
endWith
contains
)
type expressionInfo struct {
expressionType int
source string
detector string
}
type BotDetector struct {
expressions map[string]expressionInfo
cache *lru.Cache[string, bool]
}
// New creates a new instance of BotDetector using predefined rules.
func New(opt ...Option) (*BotDetector, error) {
b := &BotDetector{}
b.importRules(rules)
var err error
for i := range opt {
b, err = opt[i](b)
if err != nil {
return nil, err
}
}
return b, nil
}
func (b *BotDetector) importRules(r []string) {
if r == nil || len(r) == 0 {
b.expressions = make(map[string]expressionInfo)
return
}
b.expressions = make(map[string]expressionInfo, len(r))
for _, s := range r {
b.addExpression(s)
}
}
// NewWithRules initializes a new instance of BotDetector with provided rules.
func NewWithRules(rules []string) *BotDetector {
uBot := BotDetector{expressions: make(map[string]expressionInfo)}
for _, s := range rules {
uBot.addExpression(s)
}
return &uBot
}
func (b *BotDetector) addExpression(original string) {
lowered := strings.ToLower(original)
isStrict := strings.HasPrefix(lowered, "^") && strings.HasSuffix(lowered, "$")
isStartWith := strings.HasPrefix(lowered, "^")
isEndWith := strings.HasSuffix(lowered, "$")
switch {
case isStrict:
b.addExpressionInfo(original, strict, lowered[1:len(lowered)-1])
case isStartWith:
b.addExpressionInfo(original, startWith, lowered[1:])
case isEndWith:
b.addExpressionInfo(original, endWith, lowered[:len(lowered)-1])
default:
b.addExpressionInfo(original, contains, lowered)
}
}
func (b *BotDetector) addExpressionInfo(source string, exprType int, detector string) {
b.expressions[source] = expressionInfo{
source: source,
expressionType: exprType,
detector: detector,
}
}
// IsBot tests whether the useragent is a bot, crawler or a spider.
func (b *BotDetector) IsBot(ua string) bool {
uaNormalized := normalize(ua)
if b.cache != nil {
if ret, ok := b.cache.Get(uaNormalized); ok {
return ret
}
}
ret := false
for _, exp := range b.expressions {
switch exp.expressionType {
case strict:
if uaNormalized == exp.detector {
ret = true
}
case startWith:
if strings.HasPrefix(uaNormalized, exp.detector) {
ret = true
}
case endWith:
if strings.HasSuffix(uaNormalized, exp.detector) {
ret = true
}
case contains:
if strings.Contains(uaNormalized, exp.detector) {
ret = true
}
}
}
if b.cache != nil {
b.cache.Add(uaNormalized, ret)
}
return ret
}
func normalize(userAgent string) string {
uaNormalized := strings.ToLower(userAgent)
if strings.HasPrefix(uaNormalized, "lynx/") {
uaNormalized = strings.Replace(uaNormalized, "libwww-fm", "", 1)
return uaNormalized
}
toReplace := []string{
"cubot",
"; m bot",
"; crono",
"; b bot",
"; idbot",
"; id bot",
"; power bot",
"yandexsearch/",
"amigavoyager",
}
for _, rep := range toReplace {
uaNormalized = strings.Replace(uaNormalized, rep, "", -1)
}
return uaNormalized
}