forked from coredns/fallback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
67 lines (56 loc) · 1.26 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
package fallback
import (
"fmt"
"strings"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/plugin/proxy"
"github.com/caddyserver/caddy"
"github.com/miekg/dns"
)
func init() {
caddy.RegisterPlugin("fallback", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
t := dnsserver.GetConfig(c).Handler("trace")
f := New(t)
for c.Next() {
var (
original bool
rcode string
)
if !c.Dispenser.Args(&rcode) {
return c.ArgErr()
}
if rcode == "original" {
original = true
// Reread parameter is not rcode. Get it again.
if !c.Dispenser.Args(&rcode) {
return c.ArgErr()
}
}
rc, ok := dns.StringToRcode[strings.ToUpper(rcode)]
if !ok {
return fmt.Errorf("%s is not a valid rcode", rcode)
}
u, err := proxy.NewStaticUpstream(&c.Dispenser)
if err != nil {
return plugin.Error("fallback", err)
}
if _, ok := f.rules[rc]; ok {
return fmt.Errorf("rcode '%s' is specified more than once", rcode)
}
f.rules[rc] = rule{original: original, proxyUpstream: u}
if original {
f.original = true
}
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
f.Next = next
return f
})
return nil
}