From 401ad4ea47898b63bfe3bd365cb051cfb0ab54eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Ganne?= Date: Tue, 8 Dec 2020 12:14:20 +0100 Subject: [PATCH] always use full Service Instance Name Service instance name is defined in RFC6763 section 4.1 as Service Instance Name = . . Use it instead of . for consistency. --- README.md | 8 ++++---- wgsd.go | 10 +++++----- wgsd_test.go | 16 ++++++++-------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e7c70cd..38f90b1 100644 --- a/README.md +++ b/README.md @@ -74,12 +74,12 @@ _wireguard._udp.example.com. 0 IN PTR yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn2 _wireguard._udp.example.com. 0 IN PTR wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====._wireguard._udp.example.com. $ $ dig @127.0.0.1 -p 5353 yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====._wireguard._udp.example.com. SRV +noall +answer +additional -yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====._wireguard._udp.example.com. 0 IN SRV 0 0 7777 yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====.example.com. -yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====.example.com. 0 IN A 203.0.113.1 +yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====._wireguard._udp.example.com. 0 IN SRV 0 0 7777 yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====._wireguard._udp.example.com. +yutrled535igkl7bdlerl6m4vjxsxm3uqqpl4nmsn27mt56ad4ha====._wireguard._udp.example.com. 0 IN A 203.0.113.1 $ $ dig @127.0.0.1 -p 5353 wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====._wireguard._udp.example.com. SRV +noall +answer +additional -wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====._wireguard._udp.example.com. 0 IN SRV 0 0 8888 wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====.example.com. -wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====.example.com. 0 IN A 198.51.100.1 +wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====._wireguard._udp.example.com. 0 IN SRV 0 0 8888 wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====._wireguard._udp.example.com. +wmrid55v4enhxqx2jstyoyvkicj5pihkb2tr7r42smiu3t5l4i5q====._wireguard._udp.example.com. 0 IN A 198.51.100.1 ``` Converting public keys to Base64 with coreutils: diff --git a/wgsd.go b/wgsd.go index a63529e..b8c199a 100644 --- a/wgsd.go +++ b/wgsd.go @@ -37,7 +37,8 @@ type wgctrlClient interface { const ( keyLen = 56 // the number of characters in a base32-encoded Wireguard public key spPrefix = "_wireguard._udp." - serviceInstanceLen = keyLen + len(".") + len(spPrefix) + spSubPrefix = "." + spPrefix + serviceInstanceLen = keyLen + len(spSubPrefix) ) func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter, @@ -114,15 +115,14 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter, Priority: 0, Weight: 0, Port: uint16(endpoint.Port), - Target: fmt.Sprintf("%s.%s", - strings.ToLower(pubKey), p.zone), + Target: strings.ToLower(pubKey) + spSubPrefix + p.zone, }) w.WriteMsg(m) // nolint: errcheck return dns.RcodeSuccess, nil } } return nxDomain(p.zone, w, r) - case len(name) == keyLen+1 && (qtype == dns.TypeA || + case len(name) == len(spSubPrefix)+keyLen && (qtype == dns.TypeA || qtype == dns.TypeAAAA): pubKey := name[:keyLen] for _, peer := range device.Peers { @@ -148,7 +148,7 @@ func getHostRR(pubKey, zone string, endpoint *net.UDPAddr) dns.RR { if endpoint == nil || endpoint.IP == nil { return nil } - name := fmt.Sprintf("%s.%s", strings.ToLower(pubKey), zone) + name := strings.ToLower(pubKey) + spSubPrefix + zone switch { case endpoint.IP.To4() != nil: return &dns.A{ diff --git a/wgsd_test.go b/wgsd_test.go index 9ef5e56..35569c5 100644 --- a/wgsd_test.go +++ b/wgsd_test.go @@ -70,10 +70,10 @@ func TestWGSD(t *testing.T) { Qtype: dns.TypeSRV, Rcode: dns.RcodeSuccess, Answer: []dns.RR{ - test.SRV(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN SRV 0 0 1 %s.example.com.", peer1b32, peer1b32)), + test.SRV(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN SRV 0 0 1 %s._wireguard._udp.example.com.", peer1b32, peer1b32)), }, Extra: []dns.RR{ - test.A(fmt.Sprintf("%s.example.com. 0 IN A %s", peer1b32, peer1.Endpoint.IP.String())), + test.A(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN A %s", peer1b32, peer1.Endpoint.IP.String())), }, }, { @@ -81,26 +81,26 @@ func TestWGSD(t *testing.T) { Qtype: dns.TypeSRV, Rcode: dns.RcodeSuccess, Answer: []dns.RR{ - test.SRV(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN SRV 0 0 2 %s.example.com.", peer2b32, peer2b32)), + test.SRV(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN SRV 0 0 2 %s._wireguard._udp.example.com.", peer2b32, peer2b32)), }, Extra: []dns.RR{ - test.AAAA(fmt.Sprintf("%s.example.com. 0 IN AAAA %s", peer2b32, peer2.Endpoint.IP.String())), + test.AAAA(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN AAAA %s", peer2b32, peer2.Endpoint.IP.String())), }, }, { - Qname: fmt.Sprintf("%s.example.com.", peer1b32), + Qname: fmt.Sprintf("%s._wireguard._udp.example.com.", peer1b32), Qtype: dns.TypeA, Rcode: dns.RcodeSuccess, Answer: []dns.RR{ - test.A(fmt.Sprintf("%s.example.com. 0 IN A %s", peer1b32, peer1.Endpoint.IP.String())), + test.A(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN A %s", peer1b32, peer1.Endpoint.IP.String())), }, }, { - Qname: fmt.Sprintf("%s.example.com.", peer2b32), + Qname: fmt.Sprintf("%s._wireguard._udp.example.com.", peer2b32), Qtype: dns.TypeAAAA, Rcode: dns.RcodeSuccess, Answer: []dns.RR{ - test.AAAA(fmt.Sprintf("%s.example.com. 0 IN AAAA %s", peer2b32, peer2.Endpoint.IP.String())), + test.AAAA(fmt.Sprintf("%s._wireguard._udp.example.com. 0 IN AAAA %s", peer2b32, peer2.Endpoint.IP.String())), }, }, {