handle A/AAAA queries

This commit is contained in:
Jordan Whited 2020-05-12 17:35:05 -07:00
parent 8109291569
commit 8f7de25397

71
wgsd.go
View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net"
"strings" "strings"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
@ -56,7 +57,7 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
return dns.RcodeServerFailure, nil return dns.RcodeServerFailure, nil
} }
if len(device.Peers) == 0 { if len(device.Peers) == 0 {
return nxdomain(p.zone, w, r) return nxDomain(p.zone, w, r)
} }
// setup our reply message // setup our reply message
@ -86,10 +87,11 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
for _, peer := range device.Peers { for _, peer := range device.Peers {
if base64.StdEncoding.EncodeToString(peer.PublicKey[:]) == pubKey { if base64.StdEncoding.EncodeToString(peer.PublicKey[:]) == pubKey {
endpoint := peer.Endpoint endpoint := peer.Endpoint
if endpoint.IP == nil { hostRR := getHostRR(pubKey, p.zone, endpoint)
return nxdomain(p.zone, w, r) if hostRR == nil {
return nxDomain(p.zone, w, r)
} }
srvTarget := fmt.Sprintf("%s.%s", pubKey, p.zone) m.Extra = append(m.Extra, hostRR)
m.Answer = append(m.Answer, &dns.SRV{ m.Answer = append(m.Answer, &dns.SRV{
Hdr: dns.RR_Header{ Hdr: dns.RR_Header{
Name: state.Name(), Name: state.Name(),
@ -100,49 +102,66 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
Priority: 0, Priority: 0,
Weight: 0, Weight: 0,
Port: uint16(endpoint.Port), Port: uint16(endpoint.Port),
Target: srvTarget, Target: fmt.Sprintf("%s.%s", pubKey, p.zone),
}) })
w.WriteMsg(m) // nolint: errcheck
return dns.RcodeSuccess, nil
}
}
return nxDomain(p.zone, w, r)
case len(name) == keyLen+len(".") && (qtype == dns.TypeA ||
qtype == dns.TypeAAAA):
pubKey := name[:44]
for _, peer := range device.Peers {
if base64.StdEncoding.EncodeToString(peer.PublicKey[:]) == pubKey {
endpoint := peer.Endpoint
hostRR := getHostRR(pubKey, p.zone, endpoint)
if hostRR == nil {
return nxDomain(p.zone, w, r)
}
w.WriteMsg(m) // nolint: errcheck
return dns.RcodeSuccess, nil
}
}
return nxDomain(p.zone, w, r)
default:
return nxDomain(p.zone, w, r)
}
}
func getHostRR(pubKey, zone string, endpoint *net.UDPAddr) dns.RR {
if endpoint.IP == nil {
return nil
}
name := fmt.Sprintf("%s.%s", pubKey, zone)
switch { switch {
case endpoint.IP.To4() != nil: case endpoint.IP.To4() != nil:
m.Extra = append(m.Extra, &dns.A{ return &dns.A{
Hdr: dns.RR_Header{ Hdr: dns.RR_Header{
Name: srvTarget, Name: name,
Rrtype: dns.TypeA, Rrtype: dns.TypeA,
Class: dns.ClassINET, Class: dns.ClassINET,
Ttl: 0, Ttl: 0,
}, },
A: endpoint.IP, A: endpoint.IP,
}) }
case endpoint.IP.To16() != nil: case endpoint.IP.To16() != nil:
m.Extra = append(m.Extra, &dns.AAAA{ return &dns.AAAA{
Hdr: dns.RR_Header{ Hdr: dns.RR_Header{
Name: srvTarget, Name: name,
Rrtype: dns.TypeAAAA, Rrtype: dns.TypeAAAA,
Class: dns.ClassINET, Class: dns.ClassINET,
Ttl: 0, Ttl: 0,
}, },
AAAA: endpoint.IP, AAAA: endpoint.IP,
}) }
default: default:
// TODO: this shouldn't happen // TODO: this shouldn't happen
return nil
} }
w.WriteMsg(m) // nolint: errcheck
return dns.RcodeSuccess, nil
}
}
return nxdomain(p.zone, w, r)
case len(name) == keyLen+len(".") && (qtype == dns.TypeA ||
qtype == dns.TypeAAAA):
// TODO: return A/AAAA for of peer
default:
return nxdomain(p.zone, w, r)
}
w.WriteMsg(m) // nolint: errcheck
return dns.RcodeSuccess, nil
} }
func nxdomain(name string, w dns.ResponseWriter, r *dns.Msg) (int, error) { func nxDomain(name string, w dns.ResponseWriter, r *dns.Msg) (int, error) {
m := new(dns.Msg) m := new(dns.Msg)
m.SetReply(r) m.SetReply(r)
m.Authoritative = true m.Authoritative = true