mirror of
https://github.com/jwhited/wgsd.git
synced 2025-01-18 22:09:34 +08:00
handle SRV queries
This commit is contained in:
parent
e6531c81ed
commit
8109291569
67
wgsd.go
67
wgsd.go
@ -51,13 +51,6 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
|
||||
name := strings.TrimSuffix(state.Name(), p.zone)
|
||||
qtype := state.QType()
|
||||
|
||||
// setup our reply message
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
m.Authoritative = true
|
||||
|
||||
switch {
|
||||
case name == spPrefix && qtype == dns.TypePTR:
|
||||
device, err := p.client.Device(p.device)
|
||||
if err != nil {
|
||||
return dns.RcodeServerFailure, nil
|
||||
@ -65,10 +58,18 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
|
||||
if len(device.Peers) == 0 {
|
||||
return nxdomain(p.zone, w, r)
|
||||
}
|
||||
|
||||
// setup our reply message
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
m.Authoritative = true
|
||||
|
||||
switch {
|
||||
case name == spPrefix && qtype == dns.TypePTR:
|
||||
for _, peer := range device.Peers {
|
||||
m.Answer = append(m.Answer, &dns.PTR{
|
||||
Hdr: dns.RR_Header{
|
||||
Name: fmt.Sprintf("%s%s", spPrefix, p.zone),
|
||||
Name: state.Name(),
|
||||
Rrtype: dns.TypePTR,
|
||||
Class: dns.ClassINET,
|
||||
Ttl: 0,
|
||||
@ -81,7 +82,55 @@ func (p *WGSD) ServeDNS(ctx context.Context, w dns.ResponseWriter,
|
||||
w.WriteMsg(m) // nolint: errcheck
|
||||
return dns.RcodeSuccess, nil
|
||||
case len(name) == serviceInstanceLen && qtype == dns.TypeSRV:
|
||||
// TODO: return SRV + A/AAAA of peer
|
||||
pubKey := name[:44]
|
||||
for _, peer := range device.Peers {
|
||||
if base64.StdEncoding.EncodeToString(peer.PublicKey[:]) == pubKey {
|
||||
endpoint := peer.Endpoint
|
||||
if endpoint.IP == nil {
|
||||
return nxdomain(p.zone, w, r)
|
||||
}
|
||||
srvTarget := fmt.Sprintf("%s.%s", pubKey, p.zone)
|
||||
m.Answer = append(m.Answer, &dns.SRV{
|
||||
Hdr: dns.RR_Header{
|
||||
Name: state.Name(),
|
||||
Rrtype: dns.TypeSRV,
|
||||
Class: dns.ClassINET,
|
||||
Ttl: 0,
|
||||
},
|
||||
Priority: 0,
|
||||
Weight: 0,
|
||||
Port: uint16(endpoint.Port),
|
||||
Target: srvTarget,
|
||||
})
|
||||
switch {
|
||||
case endpoint.IP.To4() != nil:
|
||||
m.Extra = append(m.Extra, &dns.A{
|
||||
Hdr: dns.RR_Header{
|
||||
Name: srvTarget,
|
||||
Rrtype: dns.TypeA,
|
||||
Class: dns.ClassINET,
|
||||
Ttl: 0,
|
||||
},
|
||||
A: endpoint.IP,
|
||||
})
|
||||
case endpoint.IP.To16() != nil:
|
||||
m.Extra = append(m.Extra, &dns.AAAA{
|
||||
Hdr: dns.RR_Header{
|
||||
Name: srvTarget,
|
||||
Rrtype: dns.TypeAAAA,
|
||||
Class: dns.ClassINET,
|
||||
Ttl: 0,
|
||||
},
|
||||
AAAA: endpoint.IP,
|
||||
})
|
||||
default:
|
||||
// TODO: this shouldn't happen
|
||||
}
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user