-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
52 lines (43 loc) · 1.22 KB
/
detect.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
package botdetect
import (
"net/http"
"regexp"
)
// BotDetect need for checking request is from bot or not
type BotDetect struct {
userAgent string
rules []string
}
// NewBotDetect function create instance of BotDetect
func NewBotDetect(r *http.Request, rules []string) *BotDetect {
if rules == nil {
rules = makeRules()
}
d := &BotDetect{
userAgent: r.UserAgent(),
rules: rules,
}
return d
}
func makeRules() []string {
return []string{
"Googlebot|Mediapartners-Google|AdsBot-Google|Google Keyword Suggestion|Googlebot-Mobile|AdsBot-Google-Mobile|APIs-Google",
"YandexAccessibilityBot|YandexBot|YandexMobileBot|YandexImages|yandex.com/bots",
"YahooSeeker/M1A1-R2D2|facebookexternalhit|Facebot|bingbot|ia_archiver|AhrefsBot|Ezooms|GSLFbot|WBSearchBot|Twitterbot|TweetmemeBot|Twikle|PaperLiBot|Wotbox|UnwindFetchor|Exabot|MJ12bot|TurnitinBot|Pingdom",
}
}
// IsBot method detect is bot or not
func (d *BotDetect) IsBot() bool {
for _, ruleValue := range d.rules {
if d.match(ruleValue) {
return true
}
}
return false
}
func (d *BotDetect) match(ruleValue string) bool {
ruleValue = `(?is)` + ruleValue
var re *regexp.Regexp
re = regexp.MustCompile(ruleValue)
return re.MatchString(d.userAgent)
}