-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwhitelist.go
222 lines (191 loc) · 4 KB
/
whitelist.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package dnsp
import (
"crypto/md5"
"log"
"regexp"
"strings"
"time"
"github.com/miekg/dns"
)
type hosts map[checksum]struct{}
type hostsRX map[checksum]*regexp.Regexp
type checksum [md5.Size / 2]byte
// isAllowed returns whether we are allowed to resolve this host.
//
// If the server is whitelisting, the result will be true if the host is on the whitelist.
// If the server is blacklisting, the result will be true if the host is NOT on the blacklist.
//
// NOTE: "host" must end with a dot.
func (s *Server) isAllowed(host string) bool {
s.m.RLock()
defer s.m.RUnlock()
_, ok := s.hosts[hash(host)]
if !ok {
_, ok = s.privateHosts[host]
}
if s.white { // whitelist mode
if ok {
return true
}
for _, rx := range s.hostsRX {
if rx.MatchString(host) {
return true
}
}
for _, rx := range s.privateHostsRX {
if rx.MatchString(host) {
return true
}
}
return false
}
// blacklist mode
if ok {
return false
}
for _, rx := range s.hostsRX {
if rx.MatchString(host) {
return false
}
}
for _, rx := range s.privateHostsRX {
if rx.MatchString(host) {
return false
}
}
return true
}
func (s *Server) filter(qs []dns.Question) []dns.Question {
result := []dns.Question{}
for _, q := range qs {
if s.isAllowed(q.Name) {
result = append(result, q)
}
}
return result
}
// Load the host entries into separate structures and swap the existing entries.
func (s *Server) loadHostEntries() error {
s.m.RLock()
path := s.hostsFile.path
s.m.RUnlock()
if path == "" {
return nil
}
hosts := hosts{}
hostsRX := hostsRX{}
if err := readHosts(path, func(host string) {
if host[len(host)-1] != '.' {
host += "."
}
if !strings.ContainsRune(host, '*') {
// Plain host string:
hosts[hash(host)] = struct{}{}
} else if rx := compilePattern(host); rx != nil {
// Host pattern (regex):
hostsRX[hash(rx.String())] = rx
}
}); err != nil {
return err
}
s.m.Lock()
s.hosts = hosts
s.hostsRX = hostsRX
s.m.Unlock()
return nil
}
func (s *Server) monitorHostEntries(poll time.Duration) {
s.m.RLock()
hf := s.hostsFile
s.m.RUnlock()
if hf.path == "" {
return
}
for _ = range time.Tick(poll) {
log.Printf("dnsp: checking %q for updates…", hf.path)
mtime, size, err := hostsFileMetadata(hf.path)
if err != nil {
log.Printf("dnsp: %s", err)
continue
}
if hf.mtime.Equal(mtime) && hf.size == size {
continue // no updates
}
if err := s.loadHostEntries(); err != nil {
log.Printf("dnsp: %s", err)
}
s.m.Lock()
s.hostsFile.mtime = mtime
s.hostsFile.size = size
hf = s.hostsFile
s.m.Unlock()
}
}
func (s *Server) addHostEntry(host string) {
if host == "" {
return
}
if host[len(host)-1] != '.' {
host += "."
}
if !strings.ContainsRune(host, '*') {
// Plain host string:
s.m.Lock()
s.hosts[hash(host)] = struct{}{}
s.m.Unlock()
} else if rx := compilePattern(host); rx != nil {
// Host pattern (regex):
s.m.Lock()
s.hostsRX[hash(rx.String())] = rx
s.m.Unlock()
}
}
func (s *Server) removeHostEntry(host string) {
if host == "" {
return
}
if host[len(host)-1] != '.' {
host += "."
}
if !strings.ContainsRune(host, '*') {
// Plain host string:
s.m.Lock()
delete(s.hosts, hash(host))
s.m.Unlock()
} else if rx := compilePattern(host); rx != nil {
// Host pattern (regex):
s.m.Lock()
delete(s.hostsRX, hash(rx.String()))
s.m.Unlock()
}
}
func (s *Server) publicEntriesCount() int {
s.m.Lock()
n := len(s.hosts) + len(s.hostsRX)
s.m.Unlock()
return n
}
func compilePattern(pat string) *regexp.Regexp {
pat = strings.Replace(pat, ".", `\.`, -1)
pat = strings.Replace(pat, "*", ".*", -1)
pat = "^" + pat + `$`
rx, err := regexp.Compile(pat)
if err != nil {
log.Printf("dnsp: could not compile %q: %s", pat, err)
return nil
}
return rx
}
func hash(s string) checksum {
sum := md5.Sum([]byte(s))
return checksum{
sum[0] ^ sum[1],
sum[2] ^ sum[3],
sum[4] ^ sum[5],
sum[6] ^ sum[7],
sum[8] ^ sum[9],
sum[10] ^ sum[11],
sum[12] ^ sum[13],
sum[14] ^ sum[15],
}
}