-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsetup.go
79 lines (69 loc) · 1.74 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
package mdns
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/celebdor/zeroconf"
"github.com/coredns/caddy"
)
func init() {
caddy.RegisterPlugin("mdns", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
c.Next()
c.NextArg()
domain := c.Val()
minSRV := 3
// Note that a filter of "" will match everything
filter := ""
bindAddress := ""
if c.NextArg() {
val, err := strconv.Atoi(c.Val())
if err != nil {
text := fmt.Sprintf("Invalid minSRV: %s", err)
return plugin.Error("mdns", errors.New(text))
}
minSRV = val
}
if c.NextArg() {
filter = c.Val()
}
if c.NextArg() {
bindAddress = c.Val()
}
if c.NextArg() {
return plugin.Error("mdns", c.ArgErr())
}
// Because the plugin interface uses a value receiver, we need to make these
// pointers so all copies of the plugin point at the same maps.
mdnsHosts := make(map[string]*zeroconf.ServiceEntry)
srvHosts := make(map[string][]*zeroconf.ServiceEntry)
cnames := make(map[string]string)
mutex := sync.RWMutex{}
m := MDNS{Domain: strings.TrimSuffix(domain, "."), minSRV: minSRV, filter: filter, bindAddress: bindAddress, mutex: &mutex, mdnsHosts: &mdnsHosts, srvHosts: &srvHosts, cnames: &cnames}
c.OnStartup(func() error {
go browseLoop(&m)
return nil
})
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
m.Next = next
return m
})
return nil
}
func browseLoop(m *MDNS) {
for {
m.BrowseMDNS()
// 5 seconds seems to be the minimum ttl that the cache plugin will allow
// Since each browse operation takes around 2 seconds, this should be fine
time.Sleep(5 * time.Second)
}
}