jwhited-wgsd/setup.go

111 lines
2.4 KiB
Go
Raw Normal View History

2020-05-09 16:47:41 -07:00
package wgsd
import (
2020-05-12 17:40:19 -07:00
"fmt"
"net"
"strconv"
2020-05-12 17:40:19 -07: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(pluginName, setup)
2020-05-09 16:47:41 -07:00
}
2021-01-02 15:51:53 -08:00
func parse(c *caddy.Controller) (Zones, error) {
z := make(map[string]*Zone)
names := []string{}
2020-05-13 11:48:30 -07:00
for c.Next() {
2021-01-02 15:51:53 -08:00
// wgsd zone device
args := c.RemainingArgs()
if len(args) != 2 {
2021-01-02 15:51:53 -08:00
return Zones{}, fmt.Errorf("expected 2 args, got %d", len(args))
}
zone := &Zone{
name: dns.Fqdn(args[0]),
device: args[1],
}
2021-01-02 15:51:53 -08:00
names = append(names, zone.name)
_, ok := z[zone.name]
if ok {
return Zones{}, fmt.Errorf("duplicate zone name %s",
zone.name)
}
z[zone.name] = zone
2020-05-13 11:48:30 -07:00
for c.NextBlock() {
switch c.Val() {
2021-01-02 15:51:53 -08:00
case "self":
// self [endpoint] [allowed-ips ... ]
zone.serveSelf = true
args = c.RemainingArgs()
if len(args) < 1 {
break
}
// assume first arg is endpoint
host, portS, err := net.SplitHostPort(args[0])
if err == nil {
port, err := strconv.Atoi(portS)
if err != nil {
2021-01-02 15:51:53 -08:00
return Zones{}, fmt.Errorf("error converting self endpoint port: %v", err)
}
2021-01-02 15:51:53 -08:00
ip := net.ParseIP(host)
if ip == nil {
return Zones{}, fmt.Errorf("invalid self endpoint IP address: %s", host)
}
zone.selfEndpoint = &net.UDPAddr{
IP: ip,
Port: port,
}
args = args[1:]
}
2021-01-02 15:51:53 -08:00
if len(args) > 0 {
zone.selfAllowedIPs = make([]net.IPNet, 0)
}
2021-01-02 15:51:53 -08:00
for _, allowedIPString := range args {
_, prefix, err := net.ParseCIDR(allowedIPString)
if err != nil {
return Zones{}, fmt.Errorf("invalid self allowed-ip '%s' err: %v", allowedIPString, err)
}
zone.selfAllowedIPs = append(zone.selfAllowedIPs, *prefix)
}
default:
2021-01-02 15:51:53 -08:00
return Zones{}, c.ArgErr()
}
}
2020-05-13 11:48:30 -07:00
}
2021-01-02 15:51:53 -08:00
return Zones{Z: z, Names: names}, nil
}
2020-05-13 11:48:30 -07:00
func setup(c *caddy.Controller) error {
2021-01-02 15:51:53 -08:00
zones, 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 {
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
}
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 {
2021-01-02 15:51:53 -08:00
return &WGSD{
Next: next,
Zones: zones,
client: client,
}
2020-05-09 16:47:41 -07:00
})
return nil
}