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 package dns
import ( import (
"context"
"net" "net"
"github.com/micro/go-micro/network/resolver" "github.com/micro/go-micro/network/resolver"
"github.com/miekg/dns"
) )
// Resolver is a DNS network resolve // 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 // Resolve assumes ID is a domain name e.g micro.mu
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) { func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
@ -22,14 +27,29 @@ func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
host = "localhost" 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 { if err != nil {
return nil, err 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 // join resolved record with port
address := net.JoinHostPort(addr, port) address := net.JoinHostPort(addr, port)
// append to record set // append to record set