From 4e16d5b9fc218c3111e0913a2433a028ca284848 Mon Sep 17 00:00:00 2001 From: Jordan Whited Date: Wed, 13 May 2020 16:15:48 -0700 Subject: [PATCH] represent pub keys in Base32 --- go.mod | 1 + go.sum | 4 ++++ wgsd.go | 32 +++++++++++++++++++++----------- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index d95892e..83ddf58 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,6 @@ require ( github.com/caddyserver/caddy v1.0.5 github.com/coredns/coredns v1.6.9 github.com/miekg/dns v1.1.29 + github.com/prometheus/common v0.9.1 golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200511024508-91d9787b944f ) diff --git a/go.sum b/go.sum index d1a142f..bceefac 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,10 @@ github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMx github.com/akamai/AkamaiOPEN-edgegrid-golang v0.9.0/go.mod h1:zpDJeKyp9ScW4NNrbdr+Eyxvry3ilGPewKoXw3XGN1k= github.com/alangpierce/go-forceexport v0.0.0-20160317203124-8f1d6941cd75/go.mod h1:uAXEEpARkRhCZfEvy/y0Jcc888f9tHCc1W7/UeEtreE= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190808125512-07798873deee/go.mod h1:myCDvQSzCW+wB1WAlocEru4wMGJxy+vlxHdhegi1CDQ= github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190307165228-86c17b95fcd5/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= @@ -326,6 +328,7 @@ github.com/sacloud/libsacloud v1.26.1/go.mod h1:79ZwATmHLIFZIMd7sxA3LwzVy/B77uj3 github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -525,6 +528,7 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.28.0 h1:bO/TA4OxCOummhSf10siHuG7vJOiwh7SpRpFZDkOgl4= google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= gopkg.in/DataDog/dd-trace-go.v1 v1.22.0/go.mod h1:DVp8HmDh8PuTu2Z0fVVlBsyWaC++fzwVCaGWylTe3tg= +gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/wgsd.go b/wgsd.go index 4e234c6..ad2bbc5 100644 --- a/wgsd.go +++ b/wgsd.go @@ -2,17 +2,21 @@ package wgsd import ( "context" - "encoding/base64" + "encoding/base32" "fmt" "net" "strings" "github.com/coredns/coredns/plugin" + clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/coredns/coredns/request" "github.com/miekg/dns" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) +// coredns plugin-specific logger +var logger = clog.NewWithPlugin("wgsd") + // WGSD is a CoreDNS plugin that provides Wireguard peer information via DNS-SD // semantics. WGSD implements the plugin.Handler interface. type WGSD struct { @@ -31,7 +35,7 @@ type wgctrlClient interface { } const ( - keyLen = 44 // the number of characters in a base64-encoded Wireguard public key + keyLen = 56 // the number of characters in a base32-encoded Wireguard public key spPrefix = "_wireguard._udp." serviceInstanceLen = keyLen + len(".") + len(spPrefix) ) @@ -52,9 +56,12 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter, name := strings.TrimSuffix(state.Name(), p.zone) qtype := state.QType() + logger.Debugf("received query for: %s type: %s", name, + dns.TypeToString[qtype]) + device, err := p.client.Device(p.device) if err != nil { - return dns.RcodeServerFailure, nil + return dns.RcodeServerFailure, err } if len(device.Peers) == 0 { return nxDomain(p.zone, w, r) @@ -76,17 +83,18 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter, Class: dns.ClassINET, Ttl: 0, }, - Ptr: fmt.Sprintf("%s.%s%s", - base64.StdEncoding.EncodeToString(peer.PublicKey[:]), - spPrefix, p.zone), + Ptr: strings.ToLower(fmt.Sprintf("%s.%s%s", + base32.StdEncoding.EncodeToString(peer.PublicKey[:]), + spPrefix, p.zone)), }) } w.WriteMsg(m) // nolint: errcheck return dns.RcodeSuccess, nil case len(name) == serviceInstanceLen && qtype == dns.TypeSRV: - pubKey := name[:44] + pubKey := name[:keyLen] for _, peer := range device.Peers { - if base64.StdEncoding.EncodeToString(peer.PublicKey[:]) == pubKey { + if strings.EqualFold( + base32.StdEncoding.EncodeToString(peer.PublicKey[:]), pubKey) { endpoint := peer.Endpoint hostRR := getHostRR(pubKey, p.zone, endpoint) if hostRR == nil { @@ -110,16 +118,18 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter, } } return nxDomain(p.zone, w, r) - case len(name) == keyLen+len(".") && (qtype == dns.TypeA || + case len(name) == keyLen+1 && (qtype == dns.TypeA || qtype == dns.TypeAAAA): - pubKey := name[:44] + pubKey := name[:keyLen] for _, peer := range device.Peers { - if base64.StdEncoding.EncodeToString(peer.PublicKey[:]) == pubKey { + if strings.EqualFold( + base32.StdEncoding.EncodeToString(peer.PublicKey[:]), pubKey) { endpoint := peer.Endpoint hostRR := getHostRR(pubKey, p.zone, endpoint) if hostRR == nil { return nxDomain(p.zone, w, r) } + m.Answer = append(m.Answer, hostRR) w.WriteMsg(m) // nolint: errcheck return dns.RcodeSuccess, nil }