forked from jwhited/wgsd
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.go
More file actions
55 lines (45 loc) · 1.14 KB
/
Copy pathsetup.go
File metadata and controls
55 lines (45 loc) · 1.14 KB
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
package wgsd
import (
"fmt"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/miekg/dns"
"golang.zx2c4.com/wireguard/wgctrl"
)
func init() {
plugin.Register("wgsd", setup)
}
func setup(c *caddy.Controller) error {
c.Next() // Ignore "wgsd" and give us the next token.
// return an error if there is no zone specified
if !c.NextArg() {
return plugin.Error("wgsd", c.ArgErr())
}
zone := dns.Fqdn(c.Val())
// return an error if there is no device name specified
if !c.NextArg() {
return plugin.Error("wgsd", c.ArgErr())
}
device := c.Val()
// return an error if there are more tokens on this line
if c.NextArg() {
return plugin.Error("wgsd", c.ArgErr())
}
client, err := wgctrl.New()
if err != nil {
return plugin.Error("wgsd",
fmt.Errorf("error constructing wgctrl client: %v",
err))
}
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return &WGSD{
Next: next,
client: client,
zone: zone,
device: device,
}
})
return nil
}