Fix: ipv6 dns crash

This commit is contained in:
Dreamacro 2020-02-17 20:11:46 +08:00
parent 7b48138ad0
commit df0ab6aa8e
2 changed files with 7 additions and 8 deletions

View file

@ -26,7 +26,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err
network = "tcp" network = "tcp"
} }
ip, err := c.r.ResolveIPv4(c.host) ip, err := c.r.ResolveIP(c.host)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -44,23 +44,20 @@ type Resolver struct {
cache *cache.Cache cache *cache.Cache
} }
// ResolveIP request with TypeA and TypeAAAA, priority return TypeAAAA // ResolveIP request with TypeA and TypeAAAA, priority return TypeA
func (r *Resolver) ResolveIP(host string) (ip net.IP, err error) { func (r *Resolver) ResolveIP(host string) (ip net.IP, err error) {
ch := make(chan net.IP) ch := make(chan net.IP, 1)
go func() { go func() {
defer close(ch) defer close(ch)
ip, err := r.resolveIP(host, D.TypeA) ip, err := r.resolveIP(host, D.TypeAAAA)
if err != nil { if err != nil {
return return
} }
ch <- ip ch <- ip
}() }()
ip, err = r.resolveIP(host, D.TypeAAAA) ip, err = r.resolveIP(host, D.TypeA)
if err == nil { if err == nil {
go func() {
<-ch
}()
return return
} }
@ -216,6 +213,8 @@ func (r *Resolver) resolveIP(host string, dnsType uint16) (ip net.IP, err error)
return ip, nil return ip, nil
} else if dnsType == D.TypeA && isIPv4 { } else if dnsType == D.TypeA && isIPv4 {
return ip, nil return ip, nil
} else {
return nil, resolver.ErrIPVersion
} }
} }