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
|
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package urlfilter
import (
"regexp"
"github.com/philippta/flyscrape"
)
func init() {
flyscrape.RegisterModule(Module{})
}
type Module struct {
URL string `json:"url"`
URLs []string `json:"urls"`
AllowedURLs []string `json:"allowedURLs"`
BlockedURLs []string `json:"blockedURLs"`
allowedURLsRE []*regexp.Regexp
blockedURLsRE []*regexp.Regexp
}
func (Module) ModuleInfo() flyscrape.ModuleInfo {
return flyscrape.ModuleInfo{
ID: "urlfilter",
New: func() flyscrape.Module { return new(Module) },
}
}
func (m *Module) Provision(v flyscrape.Context) {
if m.disabled() {
return
}
for _, pat := range m.AllowedURLs {
re, err := regexp.Compile(pat)
if err != nil {
continue
}
m.allowedURLsRE = append(m.allowedURLsRE, re)
}
for _, pat := range m.BlockedURLs {
re, err := regexp.Compile(pat)
if err != nil {
continue
}
m.blockedURLsRE = append(m.blockedURLsRE, re)
}
}
func (m *Module) ValidateRequest(r *flyscrape.Request) bool {
if m.disabled() {
return true
}
// allow root url
if r.URL == m.URL {
return true
}
for _, u := range m.URLs {
if r.URL == u {
return true
}
}
// allow if no filter is set
if len(m.allowedURLsRE) == 0 && len(m.blockedURLsRE) == 0 {
return true
}
ok := false
if len(m.allowedURLsRE) == 0 {
ok = true
}
for _, re := range m.allowedURLsRE {
if re.MatchString(r.URL) {
ok = true
break
}
}
for _, re := range m.blockedURLsRE {
if re.MatchString(r.URL) {
ok = false
break
}
}
return ok
}
func (m *Module) disabled() bool {
return len(m.AllowedURLs) == 0 && len(m.BlockedURLs) == 0
}
var (
_ flyscrape.RequestValidator = (*Module)(nil)
_ flyscrape.Provisioner = (*Module)(nil)
)
|