resolver/dns: support ipv6 addrs

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-08-29 16:53:53 +03:00
parent 9c695ac343
commit fb233374a0

View File

@ -42,29 +42,36 @@ func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
return records, nil return records, nil
} }
m := new(dns.Msg) for _, q := range []uint16{dns.TypeA, dns.TypeAAAA} {
m.SetQuestion(dns.Fqdn(host), dns.TypeA) m := new(dns.Msg)
rec, err := dns.ExchangeContext(context.Background(), m, r.Address) m.SetQuestion(dns.Fqdn(host), q)
if err != nil { rec, err := dns.ExchangeContext(context.Background(), m, r.Address)
return nil, err if err != nil {
} return nil, err
for _, answer := range rec.Answer {
h := answer.Header()
// check record type matches
if h.Rrtype != dns.TypeA {
continue
} }
arec, _ := answer.(*dns.A) var addr string
addr := arec.A.String() for _, answer := range rec.Answer {
h := answer.Header()
// check record type matches
switch h.Rrtype {
case dns.TypeA:
arec, _ := answer.(*dns.A)
addr = arec.A.String()
case dns.TypeAAAA:
arec, _ := answer.(*dns.AAAA)
addr = arec.AAAA.String()
default:
continue
}
// join resolved record with port // join resolved record with port
address := net.JoinHostPort(addr, port) address := net.JoinHostPort(addr, port)
// append to record set // append to record set
records = append(records, &resolver.Record{ records = append(records, &resolver.Record{
Address: address, Address: address,
}) })
}
} }
// no records returned so just best effort it // no records returned so just best effort it