From df0ab6aa8ede696d9bd2bae17f4e944b70db528a Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Mon, 17 Feb 2020 20:11:46 +0800 Subject: [PATCH] Fix: ipv6 dns crash --- dns/client.go | 2 +- dns/resolver.go | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/dns/client.go b/dns/client.go index 1f1e6c6f..6341b657 100644 --- a/dns/client.go +++ b/dns/client.go @@ -26,7 +26,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err network = "tcp" } - ip, err := c.r.ResolveIPv4(c.host) + ip, err := c.r.ResolveIP(c.host) if err != nil { return nil, err } diff --git a/dns/resolver.go b/dns/resolver.go index 480818c9..7d5fbc9c 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -44,23 +44,20 @@ type Resolver struct { 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) { - ch := make(chan net.IP) + ch := make(chan net.IP, 1) go func() { defer close(ch) - ip, err := r.resolveIP(host, D.TypeA) + ip, err := r.resolveIP(host, D.TypeAAAA) if err != nil { return } ch <- ip }() - ip, err = r.resolveIP(host, D.TypeAAAA) + ip, err = r.resolveIP(host, D.TypeA) if err == nil { - go func() { - <-ch - }() return } @@ -216,6 +213,8 @@ func (r *Resolver) resolveIP(host string, dnsType uint16) (ip net.IP, err error) return ip, nil } else if dnsType == D.TypeA && isIPv4 { return ip, nil + } else { + return nil, resolver.ErrIPVersion } }