-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup.go
102 lines (91 loc) · 1.97 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
101
102
package unbound
import (
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
)
func init() {
caddy.RegisterPlugin("unbound", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
u, err := unboundParse(c)
if err != nil {
return plugin.Error("unbound", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
u.Next = next
return u
})
c.OnStartup(func() error {
once.Do(func() {
m := dnsserver.GetConfig(c).Handler("prometheus")
if m == nil {
return
}
if x, ok := m.(*metrics.Metrics); ok {
x.MustRegister(RequestDuration)
x.MustRegister(RcodeCount)
}
})
return nil
})
c.OnShutdown(u.Stop)
return nil
}
func unboundParse(c *caddy.Controller) (*Unbound, error) {
u := New()
i := 0
for c.Next() {
// Return an error if unbound block specified more than once
if i > 0 {
return nil, plugin.ErrOnce
}
i++
u.from = c.RemainingArgs()
if len(u.from) == 0 {
u.from = make([]string, len(c.ServerBlockKeys))
copy(u.from, c.ServerBlockKeys)
}
for i, str := range u.from {
u.from[i] = plugin.Host(str).Normalize()
}
for c.NextBlock() {
var args []string
var err error
switch c.Val() {
case "except":
except := c.RemainingArgs()
if len(except) == 0 {
return nil, c.ArgErr()
}
for i := 0; i < len(except); i++ {
except[i] = plugin.Host(except[i]).Normalize()
}
u.except = except
case "option":
args = c.RemainingArgs()
if len(args) != 2 {
return nil, c.ArgErr()
}
if err = u.setOption(args[0], args[1]); err != nil {
return nil, err
}
case "config":
args = c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
if err = u.config(args[0]); err != nil {
return nil, err
}
default:
return nil, c.ArgErr()
}
}
}
return u, nil
}