forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
96 lines (77 loc) · 1.73 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
package dnstap
import (
"strings"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/dnstap/dnstapio"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile"
)
var log = clog.NewWithPlugin("dnstap")
func init() {
caddy.RegisterPlugin("dnstap", caddy.Plugin{
ServerType: "dns",
Action: wrapSetup,
})
}
func wrapSetup(c *caddy.Controller) error {
if err := setup(c); err != nil {
return plugin.Error("dnstap", err)
}
return nil
}
type config struct {
target string
socket bool
full bool
}
func parseConfig(d *caddyfile.Dispenser) (c config, err error) {
d.Next() // directive name
if !d.Args(&c.target) {
return c, d.ArgErr()
}
if strings.HasPrefix(c.target, "tcp://") {
// remote IP endpoint
servers, err := dnsutil.ParseHostPortOrFile(c.target[6:])
if err != nil {
return c, d.ArgErr()
}
c.target = servers[0]
} else {
// default to UNIX socket
if strings.HasPrefix(c.target, "unix://") {
c.target = c.target[7:]
}
c.socket = true
}
c.full = d.NextArg() && d.Val() == "full"
return
}
func setup(c *caddy.Controller) error {
conf, err := parseConfig(&c.Dispenser)
if err != nil {
return err
}
dio := dnstapio.New(conf.target, conf.socket)
dnstap := Dnstap{IO: dio, JoinRawMessage: conf.full}
c.OnStartup(func() error {
dio.Connect()
return nil
})
c.OnRestart(func() error {
dio.Close()
return nil
})
c.OnFinalShutdown(func() error {
dio.Close()
return nil
})
dnsserver.GetConfig(c).AddPlugin(
func(next plugin.Handler) plugin.Handler {
dnstap.Next = next
return dnstap
})
return nil
}