-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfilter.go
101 lines (83 loc) · 2.78 KB
/
filter.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
package useragent
import (
"strings"
stringsutil "github.com/projectdiscovery/utils/strings"
)
// Filter represent the function signature for a filter
type Filter func(*UserAgent) bool
// FilterMap contains filter and its respective function signature
var FilterMap map[string]Filter
// ContainsTagsAny returns true if the user agent contains any of the provided tags
func ContainsTagsAny(userAgent *UserAgent, tags ...string) bool {
for _, tag := range userAgent.Tags {
if stringsutil.ContainsAny(tag, tags...) {
return true
}
}
return false
}
// ContainsTags returns true if the user agent contains all provided tags
func ContainsTags(userAgent *UserAgent, tags ...string) bool {
foundTags := make(map[string]struct{})
for _, tag := range userAgent.Tags {
for _, wantedTag := range tags {
if strings.Contains(tag, wantedTag) {
foundTags[tag] = struct{}{}
}
}
}
return len(foundTags) == len(tags)
}
// Mobile checks if the user agent has typical mobile tags
func Mobile(userAgent *UserAgent) bool {
return ContainsTags(userAgent, "mobile")
}
// Legacy checks if the user agent falls under legacy category
func Legacy(userAgent *UserAgent) bool {
return ContainsTags(userAgent, "Legacy")
}
// Google Checks if the user agent has typical GoogleBot tags
func GoogleBot(userAgent *UserAgent) bool {
return ContainsTags(userAgent, "Google", "Spiders")
}
// Chrome checks if the user agent has typical chrome tags
func Chrome(userAgent *UserAgent) bool {
return ContainsTagsAny(userAgent, "Chrome", "Chromium")
}
// Mozilla checks if the user agent has typical mozilla firefox tags
func Mozilla(userAgent *UserAgent) bool {
return ContainsTagsAny(userAgent, "Mozilla", "Firefox")
}
// Safari checks if the user agent has typical safari tags
func Safari(userAgent *UserAgent) bool {
return ContainsTags(userAgent, "Safari")
}
// Computer checks if the user agent has typical computer tags
func Computer(userAgent *UserAgent) bool {
return ContainsTags(userAgent, "computer")
}
// Apple checks if the user agent has typical apple tags
func Apple(userAgent *UserAgent) bool {
return ContainsTags(userAgent, "Apple Computer, Inc.")
}
// Windows checks if the user agent has typical windows tags
func Windows(userAgent *UserAgent) bool {
return ContainsTagsAny(userAgent, "Win32", "Windows")
}
// Bot checks if the user agent has typical bot tags
func Bot(userAgent *UserAgent) bool {
return ContainsTagsAny(userAgent, "Spiders - Search")
}
func init() {
FilterMap = map[string]Filter{}
FilterMap["computer"] = Computer
FilterMap["mobile"] = Mobile
FilterMap["legacy"] = Legacy
FilterMap["chrome"] = Chrome
FilterMap["mozilla"] = Mozilla
FilterMap["googlebot"] = GoogleBot
FilterMap["safari"] = Safari
FilterMap["apple"] = Apple
FilterMap["windows"] = Windows
FilterMap["bot"] = Bot
}