support ipv6 for dns resolver (#389)
This commit is contained in:
parent
d1f192cfdd
commit
b9ed9ae219
30
resolver.go
30
resolver.go
@ -136,6 +136,7 @@ type resolver struct {
|
|||||||
domain string
|
domain string
|
||||||
stopped chan struct{}
|
stopped chan struct{}
|
||||||
mux sync.RWMutex
|
mux sync.RWMutex
|
||||||
|
prefer string // ipv4 or ipv6
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewResolver create a new Resolver with the given name servers and resolution timeout.
|
// NewResolver create a new Resolver with the given name servers and resolution timeout.
|
||||||
@ -211,18 +212,36 @@ func (r *resolver) Resolve(host string) (ips []net.IP, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*resolver) resolve(ex Exchanger, host string) (ips []net.IP, ttl time.Duration, err error) {
|
func (r *resolver) resolve(ex Exchanger, host string) (ips []net.IP, ttl time.Duration, err error) {
|
||||||
if ex == nil {
|
if ex == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
if r.prefer == "ipv6" { // prefer ipv6
|
||||||
|
query := dns.Msg{}
|
||||||
|
query.SetQuestion(dns.Fqdn(host), dns.TypeAAAA)
|
||||||
|
ips, ttl, err = r.resolveIPs(ctx, ex, &query)
|
||||||
|
if err != nil || len(ips) > 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
query := dns.Msg{}
|
query := dns.Msg{}
|
||||||
query.SetQuestion(dns.Fqdn(host), dns.TypeA)
|
query.SetQuestion(dns.Fqdn(host), dns.TypeA)
|
||||||
mr, err := ex.Exchange(context.Background(), &query)
|
return r.resolveIPs(ctx, ex, &query)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*resolver) resolveIPs(ctx context.Context, ex Exchanger, query *dns.Msg) (ips []net.IP, ttl time.Duration, err error) {
|
||||||
|
mr, err := ex.Exchange(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, ans := range mr.Answer {
|
for _, ans := range mr.Answer {
|
||||||
|
if ar, _ := ans.(*dns.AAAA); ar != nil {
|
||||||
|
ips = append(ips, ar.AAAA)
|
||||||
|
ttl = time.Duration(ar.Header().Ttl) * time.Second
|
||||||
|
}
|
||||||
if ar, _ := ans.(*dns.A); ar != nil {
|
if ar, _ := ans.(*dns.A); ar != nil {
|
||||||
ips = append(ips, ar.A)
|
ips = append(ips, ar.A)
|
||||||
ttl = time.Duration(ar.Header().Ttl) * time.Second
|
ttl = time.Duration(ar.Header().Ttl) * time.Second
|
||||||
@ -271,7 +290,7 @@ func (r *resolver) storeCache(name string, ips []net.IP, ttl time.Duration) {
|
|||||||
|
|
||||||
func (r *resolver) Reload(rd io.Reader) error {
|
func (r *resolver) Reload(rd io.Reader) error {
|
||||||
var ttl, timeout, period time.Duration
|
var ttl, timeout, period time.Duration
|
||||||
var domain string
|
var domain, prefer string
|
||||||
var nss []NameServer
|
var nss []NameServer
|
||||||
|
|
||||||
if rd == nil || r.Stopped() {
|
if rd == nil || r.Stopped() {
|
||||||
@ -304,6 +323,10 @@ func (r *resolver) Reload(rd io.Reader) error {
|
|||||||
domain = ss[1]
|
domain = ss[1]
|
||||||
}
|
}
|
||||||
case "search", "sortlist", "options": // we don't support these features in /etc/resolv.conf
|
case "search", "sortlist", "options": // we don't support these features in /etc/resolv.conf
|
||||||
|
case "prefer":
|
||||||
|
if len(ss) > 1 {
|
||||||
|
prefer = strings.ToLower(ss[1])
|
||||||
|
}
|
||||||
case "nameserver": // nameserver option, compatible with /etc/resolv.conf
|
case "nameserver": // nameserver option, compatible with /etc/resolv.conf
|
||||||
if len(ss) <= 1 {
|
if len(ss) <= 1 {
|
||||||
break
|
break
|
||||||
@ -345,6 +368,7 @@ func (r *resolver) Reload(rd io.Reader) error {
|
|||||||
r.TTL = ttl
|
r.TTL = ttl
|
||||||
r.domain = domain
|
r.domain = domain
|
||||||
r.period = period
|
r.period = period
|
||||||
|
r.prefer = prefer
|
||||||
r.Servers = nss
|
r.Servers = nss
|
||||||
r.mux.Unlock()
|
r.mux.Unlock()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user