1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00

Make use of cloudflare 1.0.0.1 by default to resolve addresses

This commit is contained in:
Asim Aslam 2019-11-12 15:46:30 +00:00
parent 8867539d78
commit cffa5b6b50

View File

@ -2,13 +2,18 @@
package dns
import (
"context"
"net"
"github.com/micro/go-micro/network/resolver"
"github.com/miekg/dns"
)
// Resolver is a DNS network resolve
type Resolver struct{}
type Resolver struct {
// The resolver address to use
Address string
}
// Resolve assumes ID is a domain name e.g micro.mu
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
@ -22,14 +27,29 @@ func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
host = "localhost"
}
addrs, err := net.LookupHost(host)
if len(r.Address) == 0 {
r.Address = "1.0.0.1:53"
}
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(host), dns.TypeA)
rec, err := dns.ExchangeContext(context.Background(), m, r.Address)
if err != nil {
return nil, err
}
records := make([]*resolver.Record, 0, len(addrs))
var records []*resolver.Record
for _, answer := range rec.Answer {
h := answer.Header()
// check record type matches
if h.Rrtype != dns.TypeA {
continue
}
arec, _ := answer.(*dns.A)
addr := arec.A.String()
for _, addr := range addrs {
// join resolved record with port
address := net.JoinHostPort(addr, port)
// append to record set