-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
62 lines (50 loc) · 1.17 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
package ecs
import (
"net"
"net/netip"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
)
func init() { plugin.Register("ecs_remap", setup) }
func setup(c *caddy.Controller) error {
c.Next()
if !c.NextBlock() {
return plugin.Error("ecs_remap", c.SyntaxErr("{"))
}
lookup := make(map[netip.Addr]cidr)
for c.Val() != "}" {
src := c.Val()
if !c.NextArg() {
return plugin.Error("ecs_remap", c.ArgErr())
}
dst := c.Val()
if !c.NextLine() {
return plugin.Error("ecs_remap", c.ArgErr())
}
srcAddr, err := netip.ParseAddr(src)
if err != nil {
return plugin.Error("ecs_remap", err)
}
_, dstNet, err := net.ParseCIDR(dst)
if err != nil {
return plugin.Error("ecs_remap", err)
}
sz, _ := dstNet.Mask.Size()
family := uint16(2)
if dstNet.IP.To4() != nil {
family = 1
}
lookup[srcAddr] = cidr{
dstNet.IP,
family,
uint8(sz),
}
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return &Ecs{Next: next, lookup: lookup}
})
// All OK, return a nil error.
return nil
}