-
Notifications
You must be signed in to change notification settings - Fork 0
/
isbot.go
99 lines (80 loc) · 1.76 KB
/
isbot.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
package isbot
import (
_ "embed"
"encoding/json"
"strings"
regexp "github.com/dlclark/regexp2"
)
//go:generate go run ./generate/generate.go
var matchers []*regexp.Regexp
type definition struct {
Pattern string `json:"pattern"`
}
//go:embed crawler-user-agents.json
var crawlerUserAgents []byte
//go:embed custom.json
var customUserAgents []byte
//go:embed user-agents-bots.txt
var userAgentsRaw string
var userAgents = strings.Split(userAgentsRaw, "\n")
func init() {
var definitions []definition
err := json.Unmarshal(crawlerUserAgents, &definitions)
if err != nil {
panic(err)
}
var customDefinitions []definition
err = json.Unmarshal(customUserAgents, &customDefinitions)
if err != nil {
panic(err)
}
matchers = make([]*regexp.Regexp, len(customDefinitions)+len(definitions))
for i, d := range customDefinitions {
matcher, err := regexp.Compile(d.Pattern, regexp.IgnoreCase)
if err != nil {
panic(err)
}
matchers[i] = matcher
}
for i, d := range definitions {
matcher, err := regexp.Compile(d.Pattern, regexp.IgnoreCase)
if err != nil {
panic(err)
}
matchers[i+len(customDefinitions)] = matcher
}
}
// Check using only the regexes
func CheckRegex(userAgent string) bool {
for _, m := range matchers {
match, _ := m.MatchString(userAgent)
if match {
return true
}
}
return false
}
// Check using the list of known user agent strings
func CheckList(userAgent string) bool {
for _, ua := range userAgents {
if userAgent == ua {
return true
}
}
return false
}
// Check using both methods
func Check(userAgent string) bool {
for _, ua := range userAgents {
if userAgent == ua {
return true
}
}
for _, m := range matchers {
match, _ := m.MatchString(userAgent)
if match {
return true
}
}
return false
}