-
Notifications
You must be signed in to change notification settings - Fork 10
/
setup.go
100 lines (84 loc) · 2.51 KB
/
setup.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
package blocklist
import (
"errors"
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/miekg/dns"
)
func init() { plugin.Register("blocklist", setup) }
func setup(c *caddy.Controller) error {
for c.Next() {
domainMetrics := false
var blocklistLocation string
var allowlistLocation string
var allowlist []string
var blockResponse string
var bootStrapDNS string
c.Args(&blocklistLocation)
if blocklistLocation == "" {
return plugin.Error("blocklist", errors.New("Missing url or path to blocklist."))
}
for c.NextBlock() {
option := c.Val()
switch option {
case "allowlist":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
return plugin.Error("blocklist", errors.New("allowlist requires a single argument."))
}
allowlistLocation = remaining[0]
log.Debugf("Setting allowlist location to %s", allowlistLocation)
case "domain_metrics":
domainMetrics = true
case "bootstrap_dns":
bootStrapDNS = c.RemainingArgs()[0]
case "block_response":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
return plugin.Error("blocklist", errors.New("block_response requires a single argument."))
}
blockResponse = remaining[0]
log.Debugf("Setting block response code to %s", blockResponse)
default:
return plugin.Error("blocklist", c.Errf("unexpected '%v' command", option))
}
}
if c.NextArg() {
return plugin.Error("blocklist", errors.New("To many arguments for blocklist."))
}
blocklist, err := loadList(c, blocklistLocation, bootStrapDNS)
if err != nil {
return plugin.Error("blocklist", err)
}
if allowlistLocation != "" {
allowlist, err = loadList(c, allowlistLocation, bootStrapDNS)
if err != nil {
return plugin.Error("blocklist", err)
}
}
if blockResponse == "" {
blockResponse = "nxdomain"
}
blockResponseCode, err := getBlockResponseCode(blockResponse)
if err != nil {
return plugin.Error("blocklist", err)
}
dnsserver.GetConfig(c).
AddPlugin(func(next plugin.Handler) plugin.Handler {
return NewBlocklistPlugin(next, blocklist, allowlist, domainMetrics, blockResponseCode)
})
}
return nil
}
func getBlockResponseCode(blockResponse string) (int, error) {
switch blockResponse {
case "nxdomain":
return dns.RcodeNameError, nil
case "refused":
return dns.RcodeRefused, nil
default:
return 0, fmt.Errorf("unknown response code '%s', must be either 'nxdomain' or 'refused'", blockResponse)
}
}