2020-05-09 16:47:41 -07:00
|
|
|
package wgsd
|
|
|
|
|
|
|
|
import (
|
2020-05-12 17:40:19 -07:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-24 14:59:59 -08:00
|
|
|
"github.com/coredns/caddy"
|
2020-05-09 16:47:41 -07:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
2020-05-13 11:48:30 -07:00
|
|
|
"github.com/miekg/dns"
|
2020-05-12 17:40:19 -07:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
2020-05-09 16:47:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
plugin.Register("wgsd", setup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setup(c *caddy.Controller) error {
|
2020-05-13 11:48:30 -07:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:40:19 -07:00
|
|
|
client, err := wgctrl.New()
|
|
|
|
if err != nil {
|
2020-05-26 16:24:45 -07:00
|
|
|
return plugin.Error("wgsd",
|
2020-05-27 13:03:24 -07:00
|
|
|
fmt.Errorf("error constructing wgctrl client: %v",
|
2020-05-26 16:24:45 -07:00
|
|
|
err))
|
2020-05-12 17:40:19 -07:00
|
|
|
}
|
2020-12-18 23:08:59 +01:00
|
|
|
c.OnFinalShutdown(client.Close)
|
2020-05-12 17:40:19 -07:00
|
|
|
|
2020-05-09 16:47:41 -07:00
|
|
|
// Add the Plugin to CoreDNS, so Servers can use it in their plugin chain.
|
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
2020-05-12 17:40:19 -07:00
|
|
|
return &WGSD{
|
|
|
|
Next: next,
|
|
|
|
client: client,
|
2020-05-13 11:48:30 -07:00
|
|
|
zone: zone,
|
|
|
|
device: device,
|
2020-05-12 17:40:19 -07:00
|
|
|
}
|
2020-05-09 16:47:41 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|