aboutsummaryrefslogtreecommitdiff
path: root/plugin/transfer/setup.go
blob: 604a269599a0c9bf3d3fe552374ed5d39bc15161 (plain) (blame)
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
package transfer

import (
	"github.com/coredns/caddy"
	"github.com/coredns/coredns/core/dnsserver"
	"github.com/coredns/coredns/plugin"
	"github.com/coredns/coredns/plugin/pkg/parse"
	"github.com/coredns/coredns/plugin/pkg/transport"
)

func init() {
	caddy.RegisterPlugin("transfer", caddy.Plugin{
		ServerType: "dns",
		Action:     setup,
	})
}

func setup(c *caddy.Controller) error {
	t, err := parseTransfer(c)

	if err != nil {
		return plugin.Error("transfer", err)
	}

	dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
		t.Next = next
		return t
	})

	c.OnStartup(func() error {
		// find all plugins that implement Transferer and add them to Transferers
		plugins := dnsserver.GetConfig(c).Handlers()
		for _, pl := range plugins {
			tr, ok := pl.(Transferer)
			if !ok {
				continue
			}
			t.Transferers = append(t.Transferers, tr)
		}
		return nil
	})

	return nil
}

func parseTransfer(c *caddy.Controller) (*Transfer, error) {
	t := &Transfer{}
	for c.Next() {
		x := &xfr{}
		x.Zones = plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys)
		for c.NextBlock() {
			switch c.Val() {
			case "to":
				args := c.RemainingArgs()
				if len(args) == 0 {
					return nil, c.ArgErr()
				}
				for _, host := range args {
					if host == "*" {
						x.to = append(x.to, host)
						continue
					}
					normalized, err := parse.HostPort(host, transport.Port)
					if err != nil {
						return nil, err
					}
					x.to = append(x.to, normalized)
				}
			default:
				return nil, plugin.Error("transfer", c.Errf("unknown property %q", c.Val()))
			}
		}
		if len(x.to) == 0 {
			return nil, plugin.Error("transfer", c.Err("'to' is required"))
		}
		t.xfrs = append(t.xfrs, x)
	}
	return t, nil
}