Make use of cloudflare 1.0.0.1 by default to resolve addresses
This commit is contained in:
parent
8867539d78
commit
cffa5b6b50
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user