2020-05-09 16:47:41 -07:00
|
|
|
package wgsd
|
|
|
|
|
|
|
|
import (
|
2020-05-12 17:40:19 -07:00
|
|
|
"fmt"
|
2021-01-01 17:15:04 -08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2020-05-12 17:40:19 -07:00
|
|
|
|
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() {
|
2021-01-01 17:15:04 -08:00
|
|
|
plugin.Register(pluginName, setup)
|
2020-05-09 16:47:41 -07:00
|
|
|
}
|
|
|
|
|
2021-01-01 17:15:04 -08:00
|
|
|
const (
|
|
|
|
optionSelfAllowedIPs = "self-allowed-ips"
|
|
|
|
optionSelfEndpoint = "self-endpoint"
|
|
|
|
)
|
2020-05-13 11:48:30 -07:00
|
|
|
|
2021-01-01 17:15:04 -08:00
|
|
|
func parse(c *caddy.Controller) (*WGSD, error) {
|
|
|
|
p := &WGSD{}
|
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
if len(args) != 2 {
|
|
|
|
return nil, fmt.Errorf("expected 2 args, got %d", len(args))
|
|
|
|
}
|
|
|
|
p.zone = dns.Fqdn(args[0])
|
|
|
|
p.device = args[1]
|
2020-05-13 11:48:30 -07:00
|
|
|
|
2021-01-01 17:15:04 -08:00
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case optionSelfAllowedIPs:
|
|
|
|
p.selfAllowedIPs = make([]net.IPNet, 0)
|
|
|
|
for _, aip := range c.RemainingArgs() {
|
|
|
|
_, prefix, err := net.ParseCIDR(aip)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid self-allowed-ips: %s err: %v", c.Val(), err)
|
|
|
|
}
|
|
|
|
p.selfAllowedIPs = append(p.selfAllowedIPs, *prefix)
|
|
|
|
}
|
|
|
|
case optionSelfEndpoint:
|
|
|
|
endpoint := c.RemainingArgs()
|
|
|
|
if len(endpoint) != 1 {
|
|
|
|
return nil, fmt.Errorf("expected 1 arg, got %d", len(endpoint))
|
|
|
|
}
|
|
|
|
host, portS, err := net.SplitHostPort(endpoint[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid self-endpoint, err: %v", err)
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(portS)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error converting self-endpoint port: %v", err)
|
|
|
|
}
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip == nil {
|
|
|
|
return nil, fmt.Errorf("invalid self-endpoint, invalid IP address: %s", host)
|
|
|
|
}
|
|
|
|
p.selfEndpoint = &net.UDPAddr{
|
|
|
|
IP: ip,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 11:48:30 -07:00
|
|
|
}
|
|
|
|
|
2021-01-01 17:15:04 -08:00
|
|
|
return p, nil
|
|
|
|
}
|
2020-05-13 11:48:30 -07:00
|
|
|
|
2021-01-01 17:15:04 -08:00
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
wgsd, err := parse(c)
|
|
|
|
if err != nil {
|
|
|
|
return plugin.Error(pluginName, err)
|
|
|
|
}
|
2020-05-12 17:40:19 -07:00
|
|
|
client, err := wgctrl.New()
|
|
|
|
if err != nil {
|
2021-01-01 17:15:04 -08:00
|
|
|
return plugin.Error(pluginName,
|
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)
|
2021-01-01 17:15:04 -08:00
|
|
|
wgsd.client = client
|
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 {
|
2021-01-01 17:15:04 -08:00
|
|
|
wgsd.Next = next
|
|
|
|
return wgsd
|
2020-05-09 16:47:41 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|