-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhosts.go
69 lines (64 loc) · 1.39 KB
/
hosts.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
package filter
import (
"bufio"
"bytes"
"strings"
"github.com/coredns/caddy"
)
func parseActionListHosts(c *caddy.Controller, f *Filter, a ActionType) error {
if !c.NextArg() {
return c.Errf("no %s hosts list specified", a)
}
switch a {
case ActionTypeAllow:
if err := f.allowConfig.AddHostsList(c.Val()); err != nil {
return err
}
case ActionTypeBlock:
if err := f.blockConfig.AddHostsList(c.Val()); err != nil {
return err
}
}
return ensureEOL(c)
}
// AddHostsList to match contents
func (a ActionConfig) AddHostsList(url string) error {
if _, ok := a.hostsLists[url]; !ok {
loadFunc, err := a.GetListLoader(url)
if err != nil {
return err
}
a.hostsLists[url] = loadFunc
}
return nil
}
func (a ActionConfig) BuildHosts(domains map[string]bool) {
for domain, loader := range a.hostsLists {
file, err := loader.Load(domain)
if err != nil {
log.Errorf(
"there was a problem fetching %s hosts list %q; %s",
a.configType,
domain,
err,
)
continue
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Bytes()
line = bytes.TrimSpace(line)
if a.shouldSkip(line) {
continue
}
line = HostsRegexp.ReplaceAll(line, []byte(" "))
hostsLine := strings.Split(string(line), " ")
if len(hostsLine) != 2 {
continue
}
domains[hostsLine[1]] = true
}
}
}