|
|
@@ -27,6 +27,19 @@ const (
|
|
27
|
27
|
|
|
28
|
28
|
var fireholRegexpComment = regexp.MustCompile(`\s*#.*?$`)
|
|
29
|
29
|
|
|
|
30
|
+// Firehol is IPBlocklist which uses lists from FireHOL:
|
|
|
31
|
+// https://iplists.firehol.org/
|
|
|
32
|
+//
|
|
|
33
|
+// It can use both local files and remote URLs. This is not necessary
|
|
|
34
|
+// that blocklists should be taken from this website, we expect only
|
|
|
35
|
+// compatible formats here.
|
|
|
36
|
+//
|
|
|
37
|
+// Example of the format:
|
|
|
38
|
+//
|
|
|
39
|
+// # this is a comment
|
|
|
40
|
+// # to ignore
|
|
|
41
|
+// 127.0.0.1 # you can specify an IP
|
|
|
42
|
+// 10.0.0.0/8 # or cidr
|
|
30
|
43
|
type Firehol struct {
|
|
31
|
44
|
ctx context.Context
|
|
32
|
45
|
ctxCancel context.CancelFunc
|
|
|
@@ -44,6 +57,12 @@ type Firehol struct {
|
|
44
|
57
|
treeV6 *bool_tree.TreeV6
|
|
45
|
58
|
}
|
|
46
|
59
|
|
|
|
60
|
+// Shutdown stop a background update process.
|
|
|
61
|
+func (f *Firehol) Shutdown() {
|
|
|
62
|
+ f.ctxCancel()
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+// Contains is given IP list can be found in FireHOL blocklists.
|
|
47
|
66
|
func (f *Firehol) Contains(ip net.IP) bool {
|
|
48
|
67
|
if ip == nil {
|
|
49
|
68
|
return true
|
|
|
@@ -61,27 +80,15 @@ func (f *Firehol) Contains(ip net.IP) bool {
|
|
61
|
80
|
return f.containsIPv6(ip.To16())
|
|
62
|
81
|
}
|
|
63
|
82
|
|
|
64
|
|
-func (f *Firehol) containsIPv4(addr net.IP) bool {
|
|
65
|
|
- ip := patricia.NewIPv4AddressFromBytes(addr, 32)
|
|
66
|
|
-
|
|
67
|
|
- if ok, _, err := f.treeV4.FindDeepestTag(ip); ok && err == nil {
|
|
68
|
|
- return true
|
|
69
|
|
- }
|
|
70
|
|
-
|
|
71
|
|
- return false
|
|
72
|
|
-}
|
|
73
|
|
-
|
|
74
|
|
-func (f *Firehol) containsIPv6(addr net.IP) bool {
|
|
75
|
|
- ip := patricia.NewIPv6Address(addr, 128)
|
|
76
|
|
-
|
|
77
|
|
- if ok, _, err := f.treeV6.FindDeepestTag(ip); ok && err == nil {
|
|
78
|
|
- return true
|
|
|
83
|
+// Run starts a background update process.
|
|
|
84
|
+//
|
|
|
85
|
+// This is a blocking method so you probably want to run it in a
|
|
|
86
|
+// goroutine.
|
|
|
87
|
+func (f *Firehol) Run(updateEach time.Duration) {
|
|
|
88
|
+ if updateEach == 0 {
|
|
|
89
|
+ updateEach = DefaultFireholUpdateEach
|
|
79
|
90
|
}
|
|
80
|
91
|
|
|
81
|
|
- return false
|
|
82
|
|
-}
|
|
83
|
|
-
|
|
84
|
|
-func (f *Firehol) Run(updateEach time.Duration) {
|
|
85
|
92
|
ticker := time.NewTicker(updateEach)
|
|
86
|
93
|
|
|
87
|
94
|
defer func() {
|
|
|
@@ -113,8 +120,24 @@ func (f *Firehol) Run(updateEach time.Duration) {
|
|
113
|
120
|
}
|
|
114
|
121
|
}
|
|
115
|
122
|
|
|
116
|
|
-func (f *Firehol) Shutdown() {
|
|
117
|
|
- f.ctxCancel()
|
|
|
123
|
+func (f *Firehol) containsIPv4(addr net.IP) bool {
|
|
|
124
|
+ ip := patricia.NewIPv4AddressFromBytes(addr, 32)
|
|
|
125
|
+
|
|
|
126
|
+ if ok, _, err := f.treeV4.FindDeepestTag(ip); ok && err == nil {
|
|
|
127
|
+ return true
|
|
|
128
|
+ }
|
|
|
129
|
+
|
|
|
130
|
+ return false
|
|
|
131
|
+}
|
|
|
132
|
+
|
|
|
133
|
+func (f *Firehol) containsIPv6(addr net.IP) bool {
|
|
|
134
|
+ ip := patricia.NewIPv6Address(addr, 128)
|
|
|
135
|
+
|
|
|
136
|
+ if ok, _, err := f.treeV6.FindDeepestTag(ip); ok && err == nil {
|
|
|
137
|
+ return true
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ return false
|
|
118
|
141
|
}
|
|
119
|
142
|
|
|
120
|
143
|
func (f *Firehol) update() error { // nolint: funlen, cyclop
|
|
|
@@ -302,6 +325,10 @@ func (f *Firehol) updateAddToTrees(ip net.IP, cidr uint,
|
|
302
|
325
|
return nil
|
|
303
|
326
|
}
|
|
304
|
327
|
|
|
|
328
|
+// NewFirehol creates a new instance of FireHOL IP blocklist.
|
|
|
329
|
+//
|
|
|
330
|
+// This method does not start an update process so please execute Run
|
|
|
331
|
+// when it is necessary.
|
|
305
|
332
|
func NewFirehol(logger mtglib.Logger, network mtglib.Network,
|
|
306
|
333
|
downloadConcurrency uint,
|
|
307
|
334
|
remoteURLs []string,
|
|
|
@@ -326,7 +353,7 @@ func NewFirehol(logger mtglib.Logger, network mtglib.Network,
|
|
326
|
353
|
}
|
|
327
|
354
|
|
|
328
|
355
|
if downloadConcurrency == 0 {
|
|
329
|
|
- downloadConcurrency = DefaultDownloadConcurrency
|
|
|
356
|
+ downloadConcurrency = DefaultFireholDownloadConcurrency
|
|
330
|
357
|
}
|
|
331
|
358
|
|
|
332
|
359
|
workerPool, _ := ants.NewPool(int(downloadConcurrency))
|