-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.go
More file actions
399 lines (344 loc) · 7.54 KB
/
bot.go
File metadata and controls
399 lines (344 loc) · 7.54 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package knownbots
import (
"log"
"net"
"net/http"
"net/netip"
"os"
"path/filepath"
"strings"
"sync/atomic"
"github.com/cnlangzi/knownbots/asn"
"github.com/kentik/patricia"
"github.com/kentik/patricia/uint_tree"
"gopkg.in/yaml.v3"
)
type IPPrefix = netip.Prefix
type IPTree struct {
v4 *uint_tree.TreeV4
v6 *uint_tree.TreeV6
}
func NewIPTree() *IPTree {
return &IPTree{
v4: uint_tree.NewTreeV4(),
v6: uint_tree.NewTreeV6(),
}
}
func (t *IPTree) Add(prefix netip.Prefix) {
addr := prefix.Addr()
if addr.Is4() {
patriciaAddr, _, _ := patricia.ParseFromNetIPPrefix(prefix)
if patriciaAddr != nil {
t.v4.Add(*patriciaAddr, 1, nil)
}
} else {
_, patriciaAddr, _ := patricia.ParseFromNetIPPrefix(prefix)
if patriciaAddr != nil {
t.v6.Add(*patriciaAddr, 1, nil)
}
}
}
func (t *IPTree) Contains(ip netip.Addr) bool {
patriciaAddrV4, patriciaAddrV6, _ := patricia.ParseFromNetIPAddr(ip)
if ip.Is4() && patriciaAddrV4 != nil {
found, _ := t.v4.FindDeepestTag(*patriciaAddrV4)
return found
}
if patriciaAddrV6 != nil {
found, _ := t.v6.FindDeepestTag(*patriciaAddrV6)
return found
}
return false
}
func (t *IPTree) Count() int {
return t.v4.CountTags() + t.v6.CountTags()
}
type BotKind string
const (
KindSearchEngine BotKind = "SearchEngine"
KindSocialMedia BotKind = "SocialMedia"
KindAITraining BotKind = "AITraining"
KindAIAssist BotKind = "AIAssist"
KindAIMixed BotKind = "AIMixed"
KindSEO BotKind = "SEO"
KindMonitor BotKind = "Monitor"
KindSecurity BotKind = "Security"
KindScraper BotKind = "Scraper"
KindUnknown BotKind = "Unknown"
)
type botConfig struct {
Name string `yaml:"name"`
Kind BotKind `yaml:"kind"`
Parser string `yaml:"parser"`
UA string `yaml:"ua"`
URLs []string `yaml:"urls"`
Custom []string `yaml:"custom"`
ASN []int `yaml:"asn"`
Domains []string `yaml:"domains"`
RDNS bool `yaml:"rdns"`
}
type Bot struct {
Name string `yaml:"name"`
Kind BotKind `yaml:"kind"`
Parser string `yaml:"parser"`
UA string `yaml:"ua"`
URLs []string `yaml:"urls"`
ips *atomic.Pointer[IPTree]
asns *atomic.Pointer[ASN]
ASN []int `yaml:"asn"`
Domains []string `yaml:"domains"`
RDNS bool `yaml:"rdns"`
rdns *RDNS
fail *LRU
}
func (b *Bot) storePrefixes(prefixes []netip.Prefix) {
if len(prefixes) == 0 {
return
}
if b.ips == nil {
b.ips = &atomic.Pointer[IPTree]{}
}
tree := NewIPTree()
for _, prefix := range prefixes {
tree.Add(prefix)
}
b.ips.Store(tree)
}
func (b *Bot) initIPs(path string) {
if len(b.URLs) > 0 {
return
}
data, err := os.ReadFile(path)
if err != nil {
if EnableLog {
log.Printf("initIPs: failed to read IP prefixes from %q: %v", path, err)
}
return
}
var prefixes []netip.Prefix
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
prefix, err := netip.ParsePrefix(line)
if err != nil {
continue
}
prefixes = append(prefixes, prefix)
}
b.storePrefixes(prefixes)
}
func (b *Bot) refreshIPs(httpClient *http.Client, root string) {
prefixes := downloadIPs(httpClient, b)
if len(prefixes) == 0 {
return
}
b.storePrefixes(prefixes)
writeIPs(filepath.Join(root, b.Name, "ips.txt"), prefixes)
}
func (b *Bot) initRDNS(path string) {
if b.RDNS {
rdnscache, err := NewRDNS(path)
if err != nil {
if EnableLog {
log.Printf("[knownbots] failed to create rdns cache for %s: %v", path, err)
}
return
}
b.rdns = rdnscache
}
}
func (b *Bot) refreshRDNS() {
if b.RDNS && b.rdns != nil {
rdnscache := b.rdns
rdnscache.Prune(b.Domains)
if err := rdnscache.Persist(); err != nil {
if EnableLog {
log.Printf("[knownbots] failed to persist cache for %s: %v", b.Name, err)
}
}
}
}
func (b *Bot) initASN(store *asn.Store) {
if len(b.ASN) == 0 {
return
}
if b.asns == nil {
b.asns = &atomic.Pointer[ASN]{}
}
cache := NewASN()
for _, asnNum := range b.ASN {
prefixes := store.Load(b.Name, asnNum)
if len(prefixes) == 0 {
continue
}
cache.Add(asnNum, prefixes)
}
b.asns.Store(cache)
}
func (b *Bot) refreshASN(store *asn.Store) {
if len(b.ASN) == 0 {
return
}
newASN := NewASN()
for _, asnNum := range b.ASN {
prefixes := store.Refresh(b.Name, asnNum)
if len(prefixes) == 0 {
continue
}
newASN.Add(asnNum, prefixes)
}
if b.asns == nil {
b.asns = &atomic.Pointer[ASN]{}
}
b.asns.Store(newASN)
}
func Load(dir string) ([]*Bot, error) {
embedded, err := loadEmbedded()
if err != nil {
return nil, err
}
customDir := filepath.Join(dir, "conf.d")
entries, err := os.ReadDir(customDir)
if err != nil {
if os.IsNotExist(err) {
bots := make([]*Bot, 0, len(embedded))
for _, bot := range embedded {
bots = append(bots, bot)
}
return bots, nil
}
return nil, err
}
bots := embedded
for _, entry := range entries {
if entry.IsDir() {
continue
}
ext := strings.ToLower(filepath.Ext(entry.Name()))
if ext != ".yaml" && ext != ".yml" {
continue
}
path := filepath.Join(customDir, entry.Name())
bot, err := loadBot(path)
if err != nil {
return nil, err
}
if bot == nil {
continue
}
if _, exists := embedded[bot.Name]; exists {
if EnableLog {
log.Printf("[knownbots] custom config %q overrides built-in bot", bot.Name)
}
}
bots[bot.Name] = bot
}
result := make([]*Bot, 0, len(bots))
for _, bot := range bots {
result = append(result, bot)
}
return result, nil
}
func loadBot(path string) (*Bot, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return buildBot(data, path)
}
func buildBot(data []byte, filename string) (*Bot, error) {
var cfg botConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
if cfg.Name == "" {
if EnableLog {
log.Printf("[knownbots] skip %q: missing required 'name' field", filename)
}
return nil, nil
}
parser := cfg.Parser
if parser == "" {
parser = cfg.Name
}
customNets := parseCIDRs(cfg.Custom)
ips := &atomic.Pointer[IPTree]{}
if len(customNets) > 0 {
tree := NewIPTree()
for _, prefix := range customNets {
tree.Add(prefix)
}
if tree.Count() > 0 {
ips.Store(tree)
}
}
return &Bot{
Name: cfg.Name,
Kind: cfg.Kind,
Parser: parser,
UA: cfg.UA,
URLs: cfg.URLs,
ips: ips,
asns: nil,
ASN: cfg.ASN,
Domains: cfg.Domains,
RDNS: cfg.RDNS,
rdns: nil,
fail: nil,
}, nil
}
func parseCIDRs(cidrs []string) []IPPrefix {
var nets []IPPrefix
for _, cidr := range cidrs {
prefix, err := netip.ParsePrefix(cidr)
if err == nil {
nets = append(nets, prefix)
}
}
return nets
}
func (b *Bot) ContainsIP(ipStr string) bool {
ip, err := netip.ParseAddr(ipStr)
if err != nil {
return false
}
if b.ips != nil {
tree := b.ips.Load()
if tree != nil && tree.Contains(ip) {
return true
}
}
return false
}
func (b *Bot) VerifyRDNS(ipStr string) ResultStatus {
if b.fail.Contains(ipStr) {
return StatusFailed
}
if hostname, ok := b.rdns.Get(ipStr); ok {
if matchDomain(hostname, b.Domains) {
return StatusVerified
}
return StatusFailed
}
names, err := net.LookupAddr(ipStr)
if err != nil {
if dnsErr, ok := err.(*net.DNSError); ok && dnsErr.IsNotFound {
b.fail.Add(ipStr)
return StatusFailed
}
return StatusPending
}
if len(names) == 0 {
b.fail.Add(ipStr)
return StatusFailed
}
hostname := strings.TrimSuffix(names[0], ".")
if matchDomain(hostname, b.Domains) {
b.rdns.Set(ipStr, hostname)
return StatusVerified
}
b.fail.Add(ipStr)
return StatusFailed
}