|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+package ipblocklist
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bufio"
|
|
|
5
|
+ "context"
|
|
|
6
|
+ "fmt"
|
|
|
7
|
+ "io"
|
|
|
8
|
+ "io/ioutil"
|
|
|
9
|
+ "net"
|
|
|
10
|
+ "net/http"
|
|
|
11
|
+ "net/url"
|
|
|
12
|
+ "os"
|
|
|
13
|
+ "regexp"
|
|
|
14
|
+ "strings"
|
|
|
15
|
+ "sync"
|
|
|
16
|
+ "time"
|
|
|
17
|
+
|
|
|
18
|
+ "github.com/9seconds/mtg/v2/mtglib"
|
|
|
19
|
+ "github.com/kentik/patricia"
|
|
|
20
|
+ "github.com/kentik/patricia/bool_tree"
|
|
|
21
|
+ "github.com/panjf2000/ants"
|
|
|
22
|
+)
|
|
|
23
|
+
|
|
|
24
|
+const (
|
|
|
25
|
+ fireholIPv4DefaultCIDR = 32
|
|
|
26
|
+ fireholIPv6DefaultCIDR = 128
|
|
|
27
|
+)
|
|
|
28
|
+
|
|
|
29
|
+var fireholRegexpComment = regexp.MustCompile(`\s*#.*?$`)
|
|
|
30
|
+
|
|
|
31
|
+type Firehol struct {
|
|
|
32
|
+ logger mtglib.Logger
|
|
|
33
|
+ rwMutex sync.RWMutex
|
|
|
34
|
+ remoteURLs []string
|
|
|
35
|
+ localFiles []string
|
|
|
36
|
+ httpClient *http.Client
|
|
|
37
|
+ workerPool *ants.Pool
|
|
|
38
|
+ treeV4 *bool_tree.TreeV4
|
|
|
39
|
+ treeV6 *bool_tree.TreeV6
|
|
|
40
|
+}
|
|
|
41
|
+
|
|
|
42
|
+func (f *Firehol) Contains(ip net.IP) bool {
|
|
|
43
|
+ if ip == nil {
|
|
|
44
|
+ return true
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ ip4 := ip.To4()
|
|
|
48
|
+
|
|
|
49
|
+ f.rwMutex.RLock()
|
|
|
50
|
+ defer f.rwMutex.RUnlock()
|
|
|
51
|
+
|
|
|
52
|
+ if ip4 != nil {
|
|
|
53
|
+ return f.containsIPv4(ip4)
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ return f.containsIPv6(ip.To16())
|
|
|
57
|
+}
|
|
|
58
|
+
|
|
|
59
|
+func (f *Firehol) containsIPv4(addr net.IP) bool {
|
|
|
60
|
+ ip := patricia.NewIPv4AddressFromBytes(addr, 32)
|
|
|
61
|
+
|
|
|
62
|
+ if ok, _, err := f.treeV4.FindDeepestTag(ip); ok && err == nil {
|
|
|
63
|
+ return true
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ return false
|
|
|
67
|
+}
|
|
|
68
|
+
|
|
|
69
|
+func (f *Firehol) containsIPv6(addr net.IP) bool {
|
|
|
70
|
+ ip := patricia.NewIPv6Address(addr, 128)
|
|
|
71
|
+
|
|
|
72
|
+ if ok, _, err := f.treeV6.FindDeepestTag(ip); ok && err == nil {
|
|
|
73
|
+ return true
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ return false
|
|
|
77
|
+}
|
|
|
78
|
+
|
|
|
79
|
+func (f *Firehol) Run(ctx context.Context, updateEach time.Duration) {
|
|
|
80
|
+ ticker := time.NewTicker(updateEach)
|
|
|
81
|
+
|
|
|
82
|
+ defer func() {
|
|
|
83
|
+ ticker.Stop()
|
|
|
84
|
+
|
|
|
85
|
+ select {
|
|
|
86
|
+ case <-ticker.C:
|
|
|
87
|
+ default:
|
|
|
88
|
+ }
|
|
|
89
|
+ }()
|
|
|
90
|
+
|
|
|
91
|
+ if err := f.update(ctx); err != nil {
|
|
|
92
|
+ f.logger.WarningError("cannot update blocklist", err)
|
|
|
93
|
+ }
|
|
|
94
|
+
|
|
|
95
|
+ for {
|
|
|
96
|
+ select {
|
|
|
97
|
+ case <-ctx.Done():
|
|
|
98
|
+ return
|
|
|
99
|
+ case <-ticker.C:
|
|
|
100
|
+ if err := f.update(ctx); err != nil {
|
|
|
101
|
+ f.logger.WarningError("cannot update blocklist", err)
|
|
|
102
|
+ }
|
|
|
103
|
+ }
|
|
|
104
|
+ }
|
|
|
105
|
+}
|
|
|
106
|
+
|
|
|
107
|
+func (f *Firehol) update(ctx context.Context) error { // nolint: funlen, cyclop
|
|
|
108
|
+ ctx, cancel := context.WithCancel(ctx)
|
|
|
109
|
+ defer cancel()
|
|
|
110
|
+
|
|
|
111
|
+ wg := &sync.WaitGroup{}
|
|
|
112
|
+ wg.Add(len(f.remoteURLs) + len(f.localFiles))
|
|
|
113
|
+
|
|
|
114
|
+ treeMutex := &sync.Mutex{}
|
|
|
115
|
+ v4tree := bool_tree.NewTreeV4()
|
|
|
116
|
+ v6tree := bool_tree.NewTreeV6()
|
|
|
117
|
+
|
|
|
118
|
+ errorChan := make(chan error, 1)
|
|
|
119
|
+ defer close(errorChan)
|
|
|
120
|
+
|
|
|
121
|
+ for _, v := range f.localFiles {
|
|
|
122
|
+ go func(filename string) {
|
|
|
123
|
+ defer wg.Done()
|
|
|
124
|
+
|
|
|
125
|
+ if err := f.updateLocalFile(ctx, filename, treeMutex, v4tree, v6tree); err != nil {
|
|
|
126
|
+ cancel()
|
|
|
127
|
+ f.logger.BindStr("filename", filename).WarningError("cannot update", err)
|
|
|
128
|
+
|
|
|
129
|
+ select {
|
|
|
130
|
+ case errorChan <- err:
|
|
|
131
|
+ default:
|
|
|
132
|
+ }
|
|
|
133
|
+ }
|
|
|
134
|
+ }(v)
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ for _, v := range f.remoteURLs {
|
|
|
138
|
+ value := v
|
|
|
139
|
+
|
|
|
140
|
+ f.workerPool.Submit(func() { // nolint: errcheck
|
|
|
141
|
+ defer wg.Done()
|
|
|
142
|
+
|
|
|
143
|
+ if err := f.updateRemoteURL(ctx, value, treeMutex, v4tree, v6tree); err != nil {
|
|
|
144
|
+ cancel()
|
|
|
145
|
+ f.logger.BindStr("url", value).WarningError("cannot update", err)
|
|
|
146
|
+
|
|
|
147
|
+ select {
|
|
|
148
|
+ case errorChan <- err:
|
|
|
149
|
+ default:
|
|
|
150
|
+ }
|
|
|
151
|
+ }
|
|
|
152
|
+ })
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+ wg.Wait()
|
|
|
156
|
+
|
|
|
157
|
+ select {
|
|
|
158
|
+ case err := <-errorChan:
|
|
|
159
|
+ return fmt.Errorf("cannot update trees: %w", err)
|
|
|
160
|
+ default:
|
|
|
161
|
+ }
|
|
|
162
|
+
|
|
|
163
|
+ f.rwMutex.Lock()
|
|
|
164
|
+ defer f.rwMutex.Unlock()
|
|
|
165
|
+
|
|
|
166
|
+ f.treeV4 = v4tree
|
|
|
167
|
+ f.treeV6 = v6tree
|
|
|
168
|
+
|
|
|
169
|
+ return nil
|
|
|
170
|
+}
|
|
|
171
|
+
|
|
|
172
|
+func (f *Firehol) updateLocalFile(ctx context.Context, filename string,
|
|
|
173
|
+ mutex sync.Locker,
|
|
|
174
|
+ v4tree *bool_tree.TreeV4, v6tree *bool_tree.TreeV6) error {
|
|
|
175
|
+ filefp, err := os.Open(filename)
|
|
|
176
|
+ if err != nil {
|
|
|
177
|
+ return fmt.Errorf("cannot open file: %w", err)
|
|
|
178
|
+ }
|
|
|
179
|
+
|
|
|
180
|
+ defer filefp.Close()
|
|
|
181
|
+
|
|
|
182
|
+ return f.updateTrees(ctx, mutex, filefp, v4tree, v6tree)
|
|
|
183
|
+}
|
|
|
184
|
+
|
|
|
185
|
+func (f *Firehol) updateRemoteURL(ctx context.Context, url string,
|
|
|
186
|
+ mutex sync.Locker,
|
|
|
187
|
+ v4tree *bool_tree.TreeV4, v6tree *bool_tree.TreeV6) error {
|
|
|
188
|
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
|
189
|
+ if err != nil {
|
|
|
190
|
+ return fmt.Errorf("cannot build a request: %w", err)
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ resp, err := f.httpClient.Do(req)
|
|
|
194
|
+ if err != nil {
|
|
|
195
|
+ return fmt.Errorf("cannot request a remote URL %s: %w", url, err)
|
|
|
196
|
+ }
|
|
|
197
|
+
|
|
|
198
|
+ defer func() {
|
|
|
199
|
+ io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck
|
|
|
200
|
+ resp.Body.Close()
|
|
|
201
|
+ }()
|
|
|
202
|
+
|
|
|
203
|
+ return f.updateTrees(ctx, mutex, resp.Body, v4tree, v6tree)
|
|
|
204
|
+}
|
|
|
205
|
+
|
|
|
206
|
+func (f *Firehol) updateTrees(ctx context.Context,
|
|
|
207
|
+ mutex sync.Locker,
|
|
|
208
|
+ reader io.Reader,
|
|
|
209
|
+ v4tree *bool_tree.TreeV4,
|
|
|
210
|
+ v6tree *bool_tree.TreeV6) error {
|
|
|
211
|
+ scanner := bufio.NewScanner(reader)
|
|
|
212
|
+
|
|
|
213
|
+ for scanner.Scan() {
|
|
|
214
|
+ select {
|
|
|
215
|
+ case <-ctx.Done():
|
|
|
216
|
+ return ctx.Err()
|
|
|
217
|
+ default:
|
|
|
218
|
+ }
|
|
|
219
|
+
|
|
|
220
|
+ text := scanner.Text()
|
|
|
221
|
+ text = fireholRegexpComment.ReplaceAllLiteralString(text, "")
|
|
|
222
|
+ text = strings.TrimSpace(text)
|
|
|
223
|
+
|
|
|
224
|
+ if text == "" {
|
|
|
225
|
+ continue
|
|
|
226
|
+ }
|
|
|
227
|
+
|
|
|
228
|
+ ip, cidr, err := f.updateParseLine(text)
|
|
|
229
|
+ if err != nil {
|
|
|
230
|
+ return fmt.Errorf("cannot parse a line: %w", err)
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ if err := f.updateAddToTrees(ip, cidr, mutex, v4tree, v6tree); err != nil {
|
|
|
234
|
+ return fmt.Errorf("cannot add a node to the tree: %w", err)
|
|
|
235
|
+ }
|
|
|
236
|
+ }
|
|
|
237
|
+
|
|
|
238
|
+ if scanner.Err() != nil {
|
|
|
239
|
+ return fmt.Errorf("cannot parse a response: %w", scanner.Err())
|
|
|
240
|
+ }
|
|
|
241
|
+
|
|
|
242
|
+ return nil
|
|
|
243
|
+}
|
|
|
244
|
+
|
|
|
245
|
+func (f *Firehol) updateParseLine(text string) (net.IP, uint, error) {
|
|
|
246
|
+ _, ipnet, err := net.ParseCIDR(text)
|
|
|
247
|
+ if err != nil {
|
|
|
248
|
+ ipaddr := net.ParseIP(text)
|
|
|
249
|
+ if ipaddr == nil {
|
|
|
250
|
+ return nil, 0, fmt.Errorf("incorrect ip address %s", text)
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ ip4 := ipaddr.To4()
|
|
|
254
|
+ if ip4 != nil {
|
|
|
255
|
+ return ip4, fireholIPv4DefaultCIDR, nil
|
|
|
256
|
+ }
|
|
|
257
|
+
|
|
|
258
|
+ return ipaddr.To16(), fireholIPv6DefaultCIDR, nil
|
|
|
259
|
+ }
|
|
|
260
|
+
|
|
|
261
|
+ ones, _ := ipnet.Mask.Size()
|
|
|
262
|
+
|
|
|
263
|
+ return ipnet.IP, uint(ones), nil
|
|
|
264
|
+}
|
|
|
265
|
+
|
|
|
266
|
+func (f *Firehol) updateAddToTrees(ip net.IP, cidr uint,
|
|
|
267
|
+ mutex sync.Locker,
|
|
|
268
|
+ v4tree *bool_tree.TreeV4, v6tree *bool_tree.TreeV6) error {
|
|
|
269
|
+ mutex.Lock()
|
|
|
270
|
+ defer mutex.Unlock()
|
|
|
271
|
+
|
|
|
272
|
+ if ip.To4() != nil {
|
|
|
273
|
+ addr := patricia.NewIPv4AddressFromBytes(ip, cidr)
|
|
|
274
|
+
|
|
|
275
|
+ if _, _, err := v4tree.Set(addr, true); err != nil {
|
|
|
276
|
+ return err // nolint: wrapcheck
|
|
|
277
|
+ }
|
|
|
278
|
+ } else {
|
|
|
279
|
+ addr := patricia.NewIPv6Address(ip, cidr)
|
|
|
280
|
+
|
|
|
281
|
+ if _, _, err := v6tree.Set(addr, true); err != nil {
|
|
|
282
|
+ return err // nolint: wrapcheck
|
|
|
283
|
+ }
|
|
|
284
|
+ }
|
|
|
285
|
+
|
|
|
286
|
+ return nil
|
|
|
287
|
+}
|
|
|
288
|
+
|
|
|
289
|
+func NewFirehol(logger mtglib.Logger, network mtglib.Network,
|
|
|
290
|
+ downloadConcurrency uint,
|
|
|
291
|
+ remoteURLs []string,
|
|
|
292
|
+ localFiles []string) (*Firehol, error) {
|
|
|
293
|
+ for _, v := range remoteURLs {
|
|
|
294
|
+ parsed, err := url.Parse(v)
|
|
|
295
|
+ if err != nil {
|
|
|
296
|
+ return nil, fmt.Errorf("incorrect url %s: %w", v, err)
|
|
|
297
|
+ }
|
|
|
298
|
+
|
|
|
299
|
+ switch parsed.Scheme {
|
|
|
300
|
+ case "http", "https":
|
|
|
301
|
+ default:
|
|
|
302
|
+ return nil, fmt.Errorf("unsupported url %s", v)
|
|
|
303
|
+ }
|
|
|
304
|
+ }
|
|
|
305
|
+
|
|
|
306
|
+ for _, v := range localFiles {
|
|
|
307
|
+ if stat, err := os.Stat(v); os.IsNotExist(err) || stat.IsDir() || stat.Mode().Perm()&0o400 == 0 {
|
|
|
308
|
+ return nil, fmt.Errorf("%s is not a readable file", v)
|
|
|
309
|
+ }
|
|
|
310
|
+ }
|
|
|
311
|
+
|
|
|
312
|
+ if downloadConcurrency == 0 {
|
|
|
313
|
+ downloadConcurrency = 1
|
|
|
314
|
+ }
|
|
|
315
|
+
|
|
|
316
|
+ workerPool, _ := ants.NewPool(int(downloadConcurrency))
|
|
|
317
|
+
|
|
|
318
|
+ return &Firehol{
|
|
|
319
|
+ logger: logger.Named("firehol"),
|
|
|
320
|
+ httpClient: network.MakeHTTPClient(nil),
|
|
|
321
|
+ treeV4: bool_tree.NewTreeV4(),
|
|
|
322
|
+ treeV6: bool_tree.NewTreeV6(),
|
|
|
323
|
+ workerPool: workerPool,
|
|
|
324
|
+ remoteURLs: remoteURLs,
|
|
|
325
|
+ localFiles: localFiles,
|
|
|
326
|
+ }, nil
|
|
|
327
|
+}
|