-
Notifications
You must be signed in to change notification settings - Fork 12
/
namelist.go
418 lines (362 loc) · 8.55 KB
/
namelist.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package dnsredir
import (
"bufio"
"errors"
"fmt"
"github.com/coredns/coredns/plugin"
"golang.org/x/net/idna"
"io"
"os"
"strings"
"sync"
"time"
)
// uint16 used to store first two ASCII characters
type domainSet map[uint16]StringSet
func (d domainSet) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%T[", d))
var i uint64
n := d.Len()
for _, s := range d {
for name := range s {
sb.WriteString(name)
if i++; i != n {
sb.WriteString(", ")
}
}
}
sb.WriteString("]")
return sb.String()
}
// Return total number of domains in the domain set
func (d *domainSet) Len() uint64 {
var n uint64
for _, s := range *d {
n += uint64(len(s))
}
return n
}
func domainToIndex(s string) uint16 {
n := len(s)
if n == 0 {
panic(fmt.Sprintf("Unexpected empty string?!"))
}
// Since we use two ASCII characters to present index
// Insufficient length will padded with '-'
// Since a valid domain segment will never begin with '-'
// So it can maintain balance between buckets
if n == 1 {
return (uint16('-') << 8) | uint16(s[0])
}
// The index will be encoded in big endian
return (uint16(s[0]) << 8) | uint16(s[1])
}
// Return true if name added successfully, false otherwise
func (d *domainSet) Add(str string) bool {
// To reduce memory, we don't use full qualified name
name, ok := stringToDomain(str)
if !ok {
var err error
name, err = idna.ToASCII(str)
// idna.ToASCII("") return no error
if err != nil || len(name) == 0 {
return false
}
}
// To speed up name lookup, we utilized two-way hash
// The first one is the first two ASCII characters of the domain name
// The second one is the real domain set
// Which works somewhat like ordinary English dictionary lookup
s := (*d)[domainToIndex(name)]
if s == nil {
// MT-Unsafe: Initialize real domain set on demand
s = make(StringSet)
(*d)[domainToIndex(name)] = s
}
s.Add(name)
return true
}
// for loop will exit in advance if f() return error
func (d *domainSet) ForEachDomain(f func(name string) error) error {
for _, s := range *d {
for name := range s {
if err := f(name); err != nil {
return err
}
}
}
return nil
}
// Assume `child' is lower cased and without trailing dot
func (d *domainSet) Match(child string) bool {
if len(child) == 0 {
panic(fmt.Sprintf("Why child is an empty string?!"))
}
for {
s := (*d)[domainToIndex(child)]
// Fast lookup for a full match
if s.Contains(child) {
return true
}
// Fallback to iterate the whole set
for parent := range s {
if plugin.Name(parent).Matches(child) {
return true
}
}
i := strings.Index(child, ".")
if i <= 0 {
break
}
child = child[i+1:]
}
return false
}
const (
NameItemTypePath = iota
NameItemTypeUrl
NameItemTypeLast // Dummy
)
type NameItem struct {
sync.RWMutex
// Domain name set for lookups
names domainSet
whichType int
path string
mtime time.Time
size int64
url string
contentHash uint64
}
func NewNameItemsWithForms(forms []string) ([]*NameItem, error) {
items := make([]*NameItem, len(forms))
for i, from := range forms {
if j := strings.Index(from, "://"); j > 0 {
proto := strings.ToLower(from[:j])
if proto == "http" {
log.Warningf("Due to security reasons, URL %q is prohibited", from)
continue
}
if proto != "https" {
return nil, errors.New(fmt.Sprintf("Unsupport URL %q", from))
}
items[i] = &NameItem{
whichType: NameItemTypeUrl,
url: from,
}
} else {
items[i] = &NameItem{
whichType: NameItemTypePath,
path: from,
}
}
}
return items, nil
}
type NameList struct {
// List of name items
items []*NameItem
// All name items shared the same reload duration
pathReload time.Duration
stopPathReload chan struct{}
urlReload time.Duration
urlReadTimeout time.Duration
stopUrlReload chan struct{}
}
// Assume `child' is lower cased and without trailing dot
func (n *NameList) Match(child string) bool {
for _, item := range n.items {
item.RLock()
if item.names.Match(child) {
item.RUnlock()
return true
}
item.RUnlock()
}
return false
}
// MT-Unsafe
func (n *NameList) periodicUpdate(bootstrap []string) {
// Kick off initial name list content population
n.updateList(NameItemTypeLast, bootstrap)
if n.pathReload > 0 {
go func() {
ticker := time.NewTicker(n.pathReload)
for {
select {
case <-n.stopPathReload:
return
case <-ticker.C:
n.updateList(NameItemTypePath, bootstrap)
}
}
}()
}
if n.urlReload > 0 {
go func() {
ticker := time.NewTicker(n.urlReload)
for {
select {
case <-n.stopUrlReload:
return
case <-ticker.C:
n.updateList(NameItemTypeUrl, bootstrap)
}
}
}()
}
}
func (n *NameList) updateList(whichType int, bootstrap []string) {
for _, item := range n.items {
if whichType == NameItemTypeLast || whichType == item.whichType {
switch item.whichType {
case NameItemTypePath:
n.updateItemFromPath(item)
case NameItemTypeUrl:
if whichType == NameItemTypeLast {
n.initialUpdateFromUrl(item, bootstrap)
} else {
_ = n.updateItemFromUrl(item, bootstrap)
}
default:
panic(fmt.Sprintf("Unexpected NameItem type %v", whichType))
}
}
}
}
func (n *NameList) updateItemFromPath(item *NameItem) {
file, err := os.Open(item.path)
if err != nil {
if os.IsNotExist(err) {
// File not exist already reported at setup stage
log.Debugf("%v", err)
} else {
log.Warningf("%v", err)
}
return
}
defer Close(file)
stat, err := file.Stat()
if err == nil {
item.RLock()
mtime := item.mtime
size := item.size
item.RUnlock()
if stat.ModTime() == mtime && stat.Size() == size {
return
}
} else {
// Proceed parsing anyway
log.Warningf("%v", err)
}
t1 := time.Now()
names, totalLines := n.parse(file)
t2 := time.Since(t1)
log.Debugf("Parsed %v time spent: %v name added: %v / %v",
file.Name(), t2, names.Len(), totalLines)
item.Lock()
item.names = names
item.mtime = stat.ModTime()
item.size = stat.Size()
item.Unlock()
}
func (n *NameList) parse(r io.Reader) (domainSet, uint64) {
names := make(domainSet)
var totalLines uint64
scanner := bufio.NewScanner(r)
for scanner.Scan() {
totalLines++
line := scanner.Text()
if i := strings.IndexByte(line, '#'); i >= 0 {
line = line[:i]
}
f := strings.Split(line, "/")
if len(f) != 3 {
// Treat the whole line as a domain name
_ = names.Add(line)
continue
}
// Format: server=/<domain>/<?>
if f[0] != "server=" {
continue
}
// Don't check f[2], see: http://manpages.ubuntu.com/manpages/bionic/man8/dnsmasq.8.html
// Thus server=/<domain>/<ip>, server=/<domain>/, server=/<domain>/# won't be honored
if !names.Add(f[1]) {
log.Warningf("%q isn't a domain name", f[1])
}
}
return names, totalLines
}
// Return true if NameItem updated
func (n *NameList) updateItemFromUrl(item *NameItem, bootstrap []string) bool {
if item.whichType != NameItemTypeUrl || len(item.url) == 0 {
panic("Function call misuse or bad URL config")
}
t1 := time.Now()
content, err := getUrlContent(item.url, "text/plain", bootstrap, n.urlReadTimeout)
t2 := time.Since(t1)
if err != nil {
log.Warningf("Failed to update %q, err: %v", item.url, err)
return false
}
item.RLock()
contentHash := item.contentHash
item.RUnlock()
contentHash1 := stringHash(content)
if contentHash1 == contentHash {
return true
}
names := make(domainSet)
var totalLines uint64
t3 := time.Now()
lines := strings.Split(content, "\n")
for _, line := range lines {
totalLines++
if i := strings.IndexByte(line, '#'); i >= 0 {
line = line[:i]
}
f := strings.Split(line, "/")
if len(f) != 3 {
_ = names.Add(line)
continue
}
if f[0] != "server=" {
continue
}
if !names.Add(f[1]) {
log.Warningf("%q isn't a domain name", f[1])
}
}
t4 := time.Since(t3)
log.Debugf("Fetched %v, time spent: %v %v, added: %v / %v, hash: %#x",
item.url, t2, t4, names.Len(), totalLines, contentHash1)
item.Lock()
item.names = names
item.contentHash = contentHash1
item.Unlock()
return true
}
// Initial name list population needs a working DNS upstream
// thus we need to fallback to it(if any) in case of population failure
func (n *NameList) initialUpdateFromUrl(item *NameItem, bootstrap []string) {
go func() {
// Fast retry in case of unstable network
retryIntervals := []time.Duration{
500 * time.Millisecond,
1500 * time.Millisecond,
}
i := 0
for {
if n.updateItemFromUrl(item, bootstrap) {
break
}
if i == len(retryIntervals) {
break
}
time.Sleep(retryIntervals[i])
i++
}
}()
}