-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
validate.go
539 lines (440 loc) · 12.3 KB
/
validate.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package agents
import (
_ "embed"
"encoding/hex"
"encoding/json"
"fmt"
"hash/maphash"
"regexp"
"regexp/syntax"
"strconv"
"strings"
"time"
"unicode"
)
//go:embed crawler-user-agents.json
var crawlersJson []byte
// Crawler contains information about one crawler.
type Crawler struct {
// Regexp of User Agent of the crawler.
Pattern string `json:"pattern"`
// Discovery date.
AdditionDate time.Time `json:"addition_date"`
// Official url of the robot.
URL string `json:"url"`
// Examples of full User Agent strings.
Instances []string `json:"instances"`
}
// Private type needed to convert addition_date from/to the format used in JSON.
type jsonCrawler struct {
Pattern string `json:"pattern"`
AdditionDate string `json:"addition_date"`
URL string `json:"url"`
Instances []string `json:"instances"`
}
const timeLayout = "2006/01/02"
func (c Crawler) MarshalJSON() ([]byte, error) {
jc := jsonCrawler{
Pattern: c.Pattern,
AdditionDate: c.AdditionDate.Format(timeLayout),
URL: c.URL,
Instances: c.Instances,
}
return json.Marshal(jc)
}
func (c *Crawler) UnmarshalJSON(b []byte) error {
var jc jsonCrawler
if err := json.Unmarshal(b, &jc); err != nil {
return err
}
c.Pattern = jc.Pattern
c.URL = jc.URL
c.Instances = jc.Instances
if c.Pattern == "" {
return fmt.Errorf("empty pattern in record %s", string(b))
}
if jc.AdditionDate != "" {
tim, err := time.ParseInLocation(timeLayout, jc.AdditionDate, time.UTC)
if err != nil {
return err
}
c.AdditionDate = tim
}
return nil
}
// The list of crawlers, built from contents of crawler-user-agents.json.
var Crawlers = func() []Crawler {
var crawlers []Crawler
if err := json.Unmarshal(crawlersJson, &crawlers); err != nil {
panic(err)
}
return crawlers
}()
// analyzePattern expands a regular expression to the list of matching texts
// for plain search. The list is complete, i.e. iff a text matches the input
// pattern, then it contains at least one of the returned texts. If such a list
// can't be built, then the resulting list contains one element (main literal),
// it also returns built regexp object to run in this case. The main literal is
// a text that is contained in any matching text and is used to optimize search
// (pre-filter with this main literal before running a regexp). In the case such
// a main literal can't be found or the regexp is invalid, an error is returned.
func analyzePattern(pattern string) ([]string, *regexp.Regexp, error) {
re, err := syntax.Parse(pattern, syntax.Perl)
if err != nil {
return nil, nil, fmt.Errorf("re %q does not compile: %w", pattern, err)
}
re = re.Simplify()
// Try to convert it to the list of literals.
const maxLiterals = 100
literals, ok := literalizeRegexp(re, maxLiterals)
if ok {
return literals, nil, nil
}
// Fallback to using a regexp, but we need some string serving as
// an indicator of its possible presence.
mainLiteral := findLongestCommonLiteral(re)
const minLiteralLen = 3
if len(mainLiteral) < minLiteralLen {
return nil, nil, fmt.Errorf("re %q does not contain sufficiently long literal to serve an indicator. The longest literal is %q", pattern, mainLiteral)
}
return []string{mainLiteral}, regexp.MustCompile(pattern), nil
}
// literalizeRegexp expands a regexp to the list of matching sub-strings.
// Iff a text matches the regexp, it contains at least one of the returned
// texts. Argument maxLiterals regulates the maximum number of patterns to
// return. In case of an overflow or if it is impossible to build such a list
// from the regexp, false is returned.
func literalizeRegexp(re *syntax.Regexp, maxLiterals int) (literals []string, ok bool) {
switch re.Op {
case syntax.OpNoMatch:
return nil, true
case syntax.OpEmptyMatch:
return []string{""}, true
case syntax.OpLiteral:
return unwrapCase(re, []string{string(re.Rune)}, maxLiterals)
case syntax.OpCharClass:
count := 0
for i := 0; i < len(re.Rune); i += 2 {
first := re.Rune[i]
last := re.Rune[i+1]
count += int(last - first + 1)
}
if count > maxLiterals {
return nil, false
}
patterns := make([]string, 0, count)
for i := 0; i < len(re.Rune); i += 2 {
first := re.Rune[i]
last := re.Rune[i+1]
for r := first; r <= last; r++ {
patterns = append(patterns, string([]rune{r}))
}
}
return unwrapCase(re, patterns, maxLiterals)
case syntax.OpAnyCharNotNL, syntax.OpAnyChar:
// Not supported.
return nil, false
case syntax.OpBeginLine, syntax.OpBeginText:
return []string{"^"}, true
case syntax.OpEndLine, syntax.OpEndText:
return []string{"$"}, true
case syntax.OpWordBoundary, syntax.OpNoWordBoundary:
// Not supported.
return nil, false
case syntax.OpCapture:
subList, ok := literalizeRegexp(re.Sub[0], maxLiterals)
if !ok {
return nil, false
}
return unwrapCase(re, subList, maxLiterals)
case syntax.OpStar, syntax.OpPlus:
// Not supported.
return nil, false
case syntax.OpQuest:
if re.Flags&syntax.FoldCase != 0 {
return nil, false
}
subList, ok := literalizeRegexp(re.Sub[0], maxLiterals)
if !ok {
return nil, false
}
subList = append(subList, "")
return subList, true
case syntax.OpRepeat:
// Not supported.
return nil, false
case syntax.OpConcat:
if re.Flags&syntax.FoldCase != 0 {
return nil, false
}
matrix := make([][]string, len(re.Sub))
for i, sub := range re.Sub {
subList, ok := literalizeRegexp(sub, maxLiterals)
if !ok {
return nil, false
}
matrix[i] = subList
}
return combinations(matrix, maxLiterals)
case syntax.OpAlternate:
results := []string{}
for _, sub := range re.Sub {
subList, ok := literalizeRegexp(sub, maxLiterals)
if !ok {
return nil, false
}
results = append(results, subList...)
}
if len(results) > maxLiterals {
return nil, false
}
return unwrapCase(re, results, maxLiterals)
default:
// Not supported.
return nil, false
}
}
// combinations produces all combination of elements of matrix.
// Each sub-slice of matrix contributes one part of a resulting string.
// If the number of combinations is larger than maxLiterals, the function
// returns false.
func combinations(matrix [][]string, maxLiterals int) ([]string, bool) {
if len(matrix) == 1 {
if len(matrix[0]) > maxLiterals {
return nil, false
}
return matrix[0], true
}
prefixes := matrix[0]
suffixes, ok := combinations(matrix[1:], maxLiterals)
if !ok {
return nil, false
}
size := len(prefixes) * len(suffixes)
if size > maxLiterals {
return nil, false
}
results := make([]string, 0, size)
for _, prefix := range prefixes {
for _, suffix := range suffixes {
results = append(results, prefix+suffix)
}
}
return results, true
}
// unwrapCase takes the regexp and the list of patterns expanded from it and
// further expands it for a case-insensitive regexp, if needed. Argument
// maxLiterals regulates the maximum number of patterns to return. In case of an
// overflow, false is returned.
func unwrapCase(re *syntax.Regexp, patterns []string, maxLiterals int) ([]string, bool) {
if re.Flags&syntax.FoldCase == 0 {
return patterns, true
}
results := []string{}
for _, pattern := range patterns {
matrix := make([][]string, len(pattern))
for i, r := range pattern {
upper := unicode.ToUpper(r)
lower := unicode.ToLower(r)
matrix[i] = []string{
string([]rune{upper}),
string([]rune{lower}),
}
}
patterns, ok := combinations(matrix, maxLiterals)
if !ok {
return nil, false
}
results = append(results, patterns...)
if len(results) > maxLiterals {
return nil, false
}
}
return results, true
}
// findLongestCommonLiteral finds the longest common literal in the regexp. It's
// such a string which is contained in any text matching the regexp. If such a
// literal can't be found, it returns an empty string.
func findLongestCommonLiteral(re *syntax.Regexp) string {
if re.Flags&syntax.FoldCase != 0 {
return ""
}
switch re.Op {
case syntax.OpNoMatch, syntax.OpEmptyMatch:
return ""
case syntax.OpLiteral:
return string(re.Rune)
case syntax.OpCharClass, syntax.OpAnyCharNotNL, syntax.OpAnyChar:
return ""
case syntax.OpBeginLine, syntax.OpBeginText:
return "^"
case syntax.OpEndLine, syntax.OpEndText:
return "$"
case syntax.OpWordBoundary, syntax.OpNoWordBoundary:
return ""
case syntax.OpCapture:
return findLongestCommonLiteral(re.Sub[0])
case syntax.OpStar:
return ""
case syntax.OpPlus:
return findLongestCommonLiteral(re.Sub[0])
case syntax.OpQuest:
return ""
case syntax.OpRepeat:
if re.Min >= 1 {
return findLongestCommonLiteral(re.Sub[0])
}
return ""
case syntax.OpConcat:
longest := ""
for _, sub := range re.Sub {
str := findLongestCommonLiteral(sub)
if len(str) > len(longest) {
longest = str
}
}
return longest
case syntax.OpAlternate:
return ""
default:
return ""
}
}
type regexpPattern struct {
re *regexp.Regexp
index int
}
type matcher struct {
replacer *strings.Replacer
regexps []regexpPattern
}
var uniqueToken = hex.EncodeToString((&maphash.Hash{}).Sum(nil))
const (
uniqueTokenLen = 2 * 8
numLen = 5
literalLabel = '-'
regexpLabel = '*'
)
var m = func() matcher {
if len(uniqueToken) != uniqueTokenLen {
panic("len(uniqueToken) != uniqueTokenLen")
}
regexps := []regexpPattern{}
oldnew := make([]string, 0, len(Crawlers)*2)
// Put re-based patterns to the end to prevent AdsBot-Google from
// shadowing AdsBot-Google-Mobile.
var oldnew2 []string
for i, crawler := range Crawlers {
literals, re, err := analyzePattern(crawler.Pattern)
if err != nil {
panic(err)
}
label := literalLabel
num := i
if re != nil {
label = regexpLabel
num = len(regexps)
regexps = append(regexps, regexpPattern{
re: re,
index: i,
})
}
replaceWith := fmt.Sprintf(" %s%c%0*d ", uniqueToken, label, numLen, num)
for _, literal := range literals {
if re != nil {
oldnew2 = append(oldnew2, literal, replaceWith)
} else {
oldnew = append(oldnew, literal, replaceWith)
}
}
}
oldnew = append(oldnew, oldnew2...)
// Allocate another array with regexps of exact size to save memory.
regexps2 := make([]regexpPattern, len(regexps))
copy(regexps2, regexps)
r := strings.NewReplacer(oldnew...)
r.Replace("") // To cause internal build process.
return matcher{
replacer: r,
regexps: regexps2,
}
}()
// Returns if User Agent string matches any of crawler patterns.
func IsCrawler(userAgent string) bool {
// This code is mostly copy-paste of MatchingCrawlers,
// but with early exit logic, so it works a but faster.
text := "^" + userAgent + "$"
replaced := m.replacer.Replace(text)
if replaced == text {
return false
}
for {
uniquePos := strings.Index(replaced, uniqueToken)
if uniquePos == -1 {
break
}
start := uniquePos + uniqueTokenLen + 1
if start+numLen >= len(replaced) {
panic("corrupt replaced: " + replaced)
}
label := replaced[start-1]
switch label {
case literalLabel:
return true
case regexpLabel:
// Rare case. Run regexp to confirm the match.
indexStr := replaced[start : start+numLen]
index, err := strconv.Atoi(indexStr)
if err != nil {
panic("corrupt replaced: " + replaced)
}
rp := m.regexps[index]
if rp.re.MatchString(userAgent) {
return true
}
default:
panic("corrupt replaced: " + replaced)
}
replaced = replaced[start+numLen:]
}
return false
}
// Finds all crawlers matching the User Agent and returns the list of their indices in Crawlers.
func MatchingCrawlers(userAgent string) []int {
text := "^" + userAgent + "$"
replaced := m.replacer.Replace(text)
if replaced == text {
return []int{}
}
indices := []int{}
for {
uniquePos := strings.Index(replaced, uniqueToken)
if uniquePos == -1 {
break
}
start := uniquePos + uniqueTokenLen + 1
if start+numLen >= len(replaced) {
panic("corrupt replaced: " + replaced)
}
indexStr := replaced[start : start+numLen]
index, err := strconv.Atoi(indexStr)
if err != nil {
panic("corrupt replaced: " + replaced)
}
label := replaced[start-1]
switch label {
case literalLabel:
indices = append(indices, index)
case regexpLabel:
// Rare case. Run regexp to confirm the match.
rp := m.regexps[index]
if rp.re.MatchString(userAgent) {
indices = append(indices, rp.index)
}
default:
panic("corrupt replaced: " + replaced)
}
replaced = replaced[start+numLen:]
}
return indices
}