dns-remap: rewrite dns answer handling

This commit is contained in:
Konstantin Demin 2024-06-04 10:58:37 +03:00
parent 856dca3154
commit 7a8867433f
Signed by: krd
GPG Key ID: 4D56F87A8BA65FD0

View File

@ -18,117 +18,114 @@ type DnsAnswer struct {
} }
func dnsRemap(qname string, qtype uint16, orig *dns.Msg) ([]PowerDnsAnswer, error) { func dnsRemap(qname string, qtype uint16, orig *dns.Msg) ([]PowerDnsAnswer, error) {
result := make([]PowerDnsAnswer, 0)
interim := make([]DnsAnswer, 0, len(orig.Answer)) interim := make([]DnsAnswer, 0, len(orig.Answer))
real_qnames := make([]string, 0)
needle := qname
name_seen := map[string]bool{
needle: true,
}
cname_dive := true
for {
if !cname_dive {
break
}
cname_dive = false
for _, rr := range orig.Answer { for _, rr := range orig.Answer {
if rr.Header().Name != qname { if rr.Header().Name != needle {
continue
}
if rr.Header().Rrtype != dns.TypeCNAME {
continue continue
} }
r := DnsAnswer{ cname_dive = true
Qname: qname,
Qtype: rr.Header().Rrtype,
Ttl: rr.Header().Ttl,
}
switch r.Qtype {
case dns.TypeA:
r.Addr = rr.(*dns.A).A
r.AddrLen = net.IPv4len
interim = append(interim, r)
case dns.TypeAAAA:
r.Addr = rr.(*dns.AAAA).AAAA
r.AddrLen = net.IPv6len
interim = append(interim, r)
case dns.TypeCNAME:
cname := rr.(*dns.CNAME) cname := rr.(*dns.CNAME)
real_qnames = append(real_qnames, cname.Target) _, seen := name_seen[cname.Target]
} if seen {
// CNAME loop?
return []PowerDnsAnswer{}, nil
} }
var wg sync.WaitGroup needle = cname.Target
var mtx_interim sync.Mutex name_seen[needle] = true
// reprocess answers due to CNAME break
for _, real_qname := range real_qnames { }
wg.Add(1) if cname_dive {
go func(real_name string) { continue
defer wg.Done() }
found_qname := false
for _, rr := range orig.Answer { for _, rr := range orig.Answer {
if rr.Header().Name != real_name { if rr.Header().Name != needle {
continue
}
t := rr.Header().Rrtype
switch t {
case dns.TypeA, dns.TypeAAAA:
// continue below
default:
continue continue
} }
found_qname = true
r := DnsAnswer{ r := DnsAnswer{
Qname: qname, Qname: qname,
Qtype: rr.Header().Rrtype, Qtype: t,
Ttl: rr.Header().Ttl, Ttl: rr.Header().Ttl,
} }
switch r.Qtype { switch r.Qtype {
case dns.TypeA: case dns.TypeA:
r.Addr = rr.(*dns.A).A r.Addr = rr.(*dns.A).A
r.AddrLen = net.IPv4len r.AddrLen = net.IPv4len
mtx_interim.Lock()
interim = append(interim, r)
mtx_interim.Unlock()
case dns.TypeAAAA: case dns.TypeAAAA:
r.Addr = rr.(*dns.AAAA).AAAA r.Addr = rr.(*dns.AAAA).AAAA
r.AddrLen = net.IPv6len r.AddrLen = net.IPv6len
}
mtx_interim.Lock()
interim = append(interim, r) interim = append(interim, r)
mtx_interim.Unlock()
} }
} }
if found_qname {
return
}
resp, err := dnsCustomResolve(real_name, dns.TypeANY) // fix missing A/AAAA records
if err != nil { if (len(interim) == 0) && ((needle != qname) || (qtype == dns.TypeANY)) {
return var wg sync.WaitGroup
var a, aaaa []PowerDnsAnswer
if qtype != dns.TypeAAAA {
wg.Add(1)
go func() {
defer wg.Done()
x, _ := dnsApi_lookup_int(needle, dns.TypeA)
if x != nil {
a = x.([]PowerDnsAnswer)
} }
if resp == nil { }()
return }
if qtype != dns.TypeA {
wg.Add(1)
go func() {
defer wg.Done()
x, _ := dnsApi_lookup_int(needle, dns.TypeAAAA)
if x != nil {
aaaa = x.([]PowerDnsAnswer)
}
}()
} }
for _, rr := range resp.Answer {
if rr.Header().Name != real_name {
continue
}
r := DnsAnswer{
Qname: qname,
Qtype: rr.Header().Rrtype,
Ttl: rr.Header().Ttl,
}
switch r.Qtype {
case dns.TypeA:
r.Addr = rr.(*dns.A).A
r.AddrLen = net.IPv4len
mtx_interim.Lock()
interim = append(interim, r)
mtx_interim.Unlock()
case dns.TypeAAAA:
r.Addr = rr.(*dns.AAAA).AAAA
r.AddrLen = net.IPv6len
mtx_interim.Lock()
interim = append(interim, r)
mtx_interim.Unlock()
}
}
}(real_qname)
}
wg.Wait() wg.Wait()
result := make([]PowerDnsAnswer, 0, len(interim)) if a != nil {
// nothing to do result = append(result, a...)
if len(interim) == 0 { }
if aaaa != nil {
result = append(result, aaaa...)
}
// HACK: replace qname
for i := range result {
result[i].Qname = qname
}
return result, nil return result, nil
} }
@ -139,7 +136,6 @@ func dnsRemap(qname string, qtype uint16, orig *dns.Msg) ([]PowerDnsAnswer, erro
for _, r := range interim { for _, r := range interim {
switch r.AddrLen { switch r.AddrLen {
case net.IPv4len, net.IPv6len: case net.IPv4len, net.IPv6len:
break
default: default:
continue continue
} }