-
Notifications
You must be signed in to change notification settings - Fork 7
/
setup.go
67 lines (52 loc) · 1.57 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 records
import (
"strings"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns"
)
var log = clog.NewWithPlugin("records")
func init() { plugin.Register("records", setup) }
func setup(c *caddy.Controller) error {
re, err := recordsParse(c)
if err != nil {
return plugin.Error("records", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
re.Next = next
return re
})
return nil
}
func recordsParse(c *caddy.Controller) (*Records, error) {
re := New()
i := 0
for c.Next() {
if i > 0 {
return re, plugin.ErrOnce
}
i++
// copy the server block origins, if ZONES are given we will overwrite these again
re.origins = plugin.OriginsFromArgsOrServerBlock(c.RemainingArgs(), c.ServerBlockKeys)
if len(re.origins) == 0 { // do we really need this default, just in the tests?
re.origins = []string{"."}
}
// c.Val() + c.RemainingArgs() is the record we need to parse (for each zone given; now tracked in re.origins). When parsing
// the record we just set the ORIGIN to the correct value and magic will happen. If no origin we set it to "."
for c.NextBlock() {
s := c.Val() + " "
s += strings.Join(c.RemainingArgs(), " ")
for _, o := range re.origins {
rr, err := dns.NewRR("$ORIGIN " + o + "\n" + s + "\n")
if err != nil {
return nil, err
}
rr.Header().Name = strings.ToLower(rr.Header().Name)
re.m[o] = append(re.m[o], rr)
}
}
}
return re, nil
}