-
Notifications
You must be signed in to change notification settings - Fork 65
/
blocklistloader-local.go
55 lines (47 loc) · 1.25 KB
/
blocklistloader-local.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
package rdns
import (
"bufio"
"os"
)
// FileLoader reads blocklist rules from a local file. Used to refresh blocklists
// from a file on the local machine.
type FileLoader struct {
filename string
opt FileLoaderOptions
lastSuccess []string
}
// FileLoaderOptions holds options for file blocklist loaders.
type FileLoaderOptions struct {
// Don't fail when trying to load the list
AllowFailure bool
}
var _ BlocklistLoader = &FileLoader{}
func NewFileLoader(filename string, opt FileLoaderOptions) *FileLoader {
return &FileLoader{filename, opt, nil}
}
func (l *FileLoader) Load() (rules []string, err error) {
log := Log.WithField("file", l.filename)
log.Trace("loading blocklist")
// If AllowFailure is enabled, return the last successfully loaded list
// and nil
defer func() {
if err != nil && l.opt.AllowFailure {
log.WithError(err).Warn("failed to load blocklist, continuing with previous ruleset")
rules = l.lastSuccess
err = nil
} else {
l.lastSuccess = rules
}
}()
f, err := os.Open(l.filename)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
rules = append(rules, scanner.Text())
}
log.Trace("completed loading blocklist")
return rules, scanner.Err()
}