-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.go
72 lines (59 loc) · 1.69 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
package ipset
import (
"context"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/caddy"
"github.com/miekg/dns"
)
// N implements the plugin interface.
type N struct {
Next plugin.Handler
listName []string
mappedListName map[string][]string
}
func init() { plugin.Register("ipset", setup) }
func setup(c *caddy.Controller) error {
mappedListName := make(map[string][]string)
listName := []string{}
for c.Next() {
args := c.RemainingArgs()
hasBlock := false
for c.NextBlock() {
val := c.Val()
blockArgs := []string{}
for _, arg := range c.RemainingArgs() {
blockArgs = append(blockArgs, plugin.Host(arg).NormalizeExact()...)
}
if val != "" && len(blockArgs) > 0 {
mappedListName[val] = blockArgs
hasBlock = true
}
}
if !hasBlock {
listName = append(listName, args...)
}
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return N{Next: next, listName: listName, mappedListName: mappedListName}
})
if len(listName) > 0 || len(mappedListName) > 0 {
c.OnStartup(func() error {
return initLib()
})
c.OnShutdown(func() error {
return shutdownLib()
})
}
return nil
}
// ServeDNS implements the plugin.Handler interface.
func (n N) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
newWriter := NewResponseReverter(w, r, n.listName, n.mappedListName)
if len(n.listName) == 0 && len(n.mappedListName) == 0 {
return plugin.NextOrFailure(n.Name(), n.Next, ctx, w, r)
}
return plugin.NextOrFailure(n.Name(), n.Next, ctx, newWriter, r)
}
// Name implements the Handler interface.
func (n N) Name() string { return "ipset" }