forked from coredns/coredns
/
setup.go
173 lines (147 loc) · 3.42 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package hosts
import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/mholt/caddy"
)
var log = clog.NewWithPlugin("hosts")
func init() {
caddy.RegisterPlugin("hosts", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func periodicHostsUpdate(h *Hosts) chan bool {
parseChan := make(chan bool)
if h.options.reload == durationOf0s {
return parseChan
}
go func() {
ticker := time.NewTicker(h.options.reload)
for {
select {
case <-parseChan:
return
case <-ticker.C:
h.readHosts()
}
}
}()
return parseChan
}
func setup(c *caddy.Controller) error {
h, err := hostsParse(c)
if err != nil {
return plugin.Error("hosts", err)
}
parseChan := periodicHostsUpdate(&h)
c.OnStartup(func() error {
h.readHosts()
return nil
})
c.OnShutdown(func() error {
close(parseChan)
return nil
})
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
h.Next = next
return h
})
return nil
}
func hostsParse(c *caddy.Controller) (Hosts, error) {
config := dnsserver.GetConfig(c)
options := newOptions()
h := Hosts{
Hostsfile: &Hostsfile{
path: "/etc/hosts",
hmap: newHostsMap(),
options: options,
},
}
inline := []string{}
i := 0
for c.Next() {
if i > 0 {
return h, plugin.ErrOnce
}
i++
args := c.RemainingArgs()
if len(args) >= 1 {
h.path = args[0]
args = args[1:]
if !filepath.IsAbs(h.path) && config.Root != "" {
h.path = filepath.Join(config.Root, h.path)
}
s, err := os.Stat(h.path)
if err != nil {
if os.IsNotExist(err) {
log.Warningf("File does not exist: %s", h.path)
} else {
return h, c.Errf("unable to access hosts file '%s': %v", h.path, err)
}
}
if s != nil && s.IsDir() {
log.Warningf("Hosts file %q is a directory", h.path)
}
}
origins := make([]string, len(c.ServerBlockKeys))
copy(origins, c.ServerBlockKeys)
if len(args) > 0 {
origins = args
}
for i := range origins {
origins[i] = plugin.Host(origins[i]).Normalize()
}
h.Origins = origins
for c.NextBlock() {
switch c.Val() {
case "fallthrough":
h.Fall.SetZonesFromArgs(c.RemainingArgs())
case "no_reverse":
options.autoReverse = false
case "ttl":
remaining := c.RemainingArgs()
if len(remaining) < 1 {
return h, c.Errf("ttl needs a time in second")
}
ttl, err := strconv.Atoi(remaining[0])
if err != nil {
return h, c.Errf("ttl needs a number of second")
}
if ttl <= 0 || ttl > 65535 {
return h, c.Errf("ttl provided is invalid")
}
options.ttl = uint32(ttl)
case "reload":
remaining := c.RemainingArgs()
if len(remaining) != 1 {
return h, c.Errf("reload needs a duration (zero seconds to disable)")
}
reload, err := time.ParseDuration(remaining[0])
if err != nil {
return h, c.Errf("invalid duration for reload '%s'", remaining[0])
}
if reload < durationOf0s {
return h, c.Errf("invalid negative duration for reload '%s'", remaining[0])
}
options.reload = reload
default:
if len(h.Fall.Zones) == 0 {
line := strings.Join(append([]string{c.Val()}, c.RemainingArgs()...), " ")
inline = append(inline, line)
continue
}
return h, c.Errf("unknown property '%s'", c.Val())
}
}
}
h.initInline(inline)
return h, nil
}