-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.go
161 lines (130 loc) · 3.12 KB
/
filter.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package filter
import (
"bufio"
"context"
"os"
"regexp"
"strconv"
"strings"
"sync"
utils "github.com/bolt-observer/go_common/utils"
"github.com/fsnotify/fsnotify"
"github.com/golang/glog"
)
// FileFilter struct
type FileFilter struct {
Filter
WhitelistFilePath string
Mutex sync.Mutex
DefaultOptions Options
}
// Reload from file
func (f *FileFilter) Reload() error {
f.Mutex.Lock()
defer f.Mutex.Unlock()
r := regexp.MustCompile(`\s*#.*$`)
readFile, err := os.Open(f.WhitelistFilePath)
if err != nil {
return err
}
defer readFile.Close()
f.nodeIDWhitelist = make(map[string]struct{})
f.chanIDWhitelist = make(map[uint64]struct{})
f.Options = f.DefaultOptions
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := strings.Trim(fileScanner.Text(), " ")
if strings.HasPrefix("#", line) {
continue
}
line = strings.Trim(r.ReplaceAllString(line, ""), " \t\r\n")
if line == "" {
continue
}
if utils.ValidatePubkey(line) {
f.nodeIDWhitelist[line] = struct{}{}
} else if strings.ToLower(line) == "private" {
f.Options |= AllowAllPrivate
} else if strings.ToLower(line) == "public" {
f.Options |= AllowAllPublic
} else {
val, err := strconv.ParseUint(line, 10, 64)
if err != nil {
glog.Warningf("Invalid line %s", line)
continue
}
f.chanIDWhitelist[val] = struct{}{}
}
}
glog.V(3).Infof("Filter reloaded")
return nil
}
// NewFilterFromFile create new FileFilter
func NewFilterFromFile(ctx context.Context, filePath string, options Options) (FilteringInterface, error) {
f := &FileFilter{
WhitelistFilePath: filePath,
}
f.DefaultOptions = options
err := f.Reload()
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
err = watcher.Add(filePath)
if err != nil {
return nil, err
}
go func(watcher *fsnotify.Watcher) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
continue
}
f.Reload()
if event.Op&fsnotify.Rename == fsnotify.Rename || event.Op&fsnotify.Remove == fsnotify.Remove {
// Happens when you save the changes via text editor
err := watcher.Add(event.Name)
if err != nil {
glog.Warningf("Watcher error %v\n", err)
}
}
case err := <-watcher.Errors:
glog.Warningf("Watcher error %v\n", err)
case <-ctx.Done():
if watcher != nil {
watcher.Close()
}
return
}
}
}(watcher)
return f, nil
}
// AllowPubKey checks whether pubkey is allowed
func (f *FileFilter) AllowPubKey(id string) bool {
f.Mutex.Lock()
defer f.Mutex.Unlock()
_, ok := f.nodeIDWhitelist[id]
return ok
}
// AllowChanID checks short channel id
func (f *FileFilter) AllowChanID(id uint64) bool {
f.Mutex.Lock()
defer f.Mutex.Unlock()
_, ok := f.chanIDWhitelist[id]
return ok
}
// AllowSpecial is used to allow all private/public chans
func (f *FileFilter) AllowSpecial(private bool) bool {
f.Mutex.Lock()
defer f.Mutex.Unlock()
if private {
return f.Options&AllowAllPrivate == AllowAllPrivate
}
return f.Options&AllowAllPublic == AllowAllPublic
}