-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
88 lines (82 loc) · 2.11 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
package split
import (
"net"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
)
// init registers this plugin.
func init() { plugin.Register("split", setup) }
// setup is the function that gets called when the config parser see the token "example". Setup is responsible
// for parsing any extra options the example plugin may have. The first token this function sees is "example".
func setup(c *caddy.Controller) error {
var s Split
for c.Next() {
r := Rule{
Zones: plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys),
}
log.Debug("zones", r.Zones)
for c.NextBlock() {
prop := c.Val()
args := c.RemainingArgs()
switch prop {
case "net":
log.Debug("net", args)
if len(args) == 0 {
return c.Errf("net: expected at least 1 argument, got 0")
}
var nets []*net.IPNet
var allow bool
var allowNets []*net.IPNet
for _, v := range args {
switch v {
case "allow":
allow = true
default:
_, ipnet, err := net.ParseCIDR(v)
if err != nil {
return err
}
if allow {
allowNets = append(allowNets, ipnet)
} else {
nets = append(nets, ipnet)
}
}
}
if len(allowNets) == 0 {
allowNets = nets[:]
}
for _, v := range nets {
r.Networks = append(r.Networks, Network{
RecordNetwork: v,
Allowed: allowNets,
})
}
case "fallback":
log.Debug("fallback", args)
if r.Fallback != nil {
return c.Errf("fallback already set")
}
if len(args) != 1 {
return c.Errf("fallback: expected 1 argument, got %d", len(args))
}
ip := net.ParseIP(args[0])
if ip == nil {
return c.Errf("fallback: invalid IP %s", args[0])
}
r.Fallback = ip
default:
return c.Errf("unknown property '%s'", prop)
}
}
s.Rules = append(s.Rules, r)
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
s.Next = next
return s
})
// All OK, return a nil error.
return nil
}