From c38469330dac9f2dd23f76a234957a51808e17db Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Fri, 27 Sep 2019 15:26:07 +0800 Subject: [PATCH] Fix: ip version check --- dns/iputil.go | 6 +++--- dns/resolver.go | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/dns/iputil.go b/dns/iputil.go index 7967b44a..3c34cf16 100644 --- a/dns/iputil.go +++ b/dns/iputil.go @@ -37,8 +37,8 @@ func ResolveIPv4(host string) (net.IP, error) { } for _, ip := range ipAddrs { - if len(ip) == net.IPv4len { - return ip, nil + if ip4 := ip.To4(); ip4 != nil { + return ip4, nil } } @@ -71,7 +71,7 @@ func ResolveIPv6(host string) (net.IP, error) { } for _, ip := range ipAddrs { - if len(ip) == net.IPv6len { + if ip.To4() == nil { return ip, nil } } diff --git a/dns/resolver.go b/dns/resolver.go index 0ba23420..2276f789 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -222,9 +222,10 @@ func (r *Resolver) fallbackExchange(m *D.Msg) (msg *D.Msg, err error) { func (r *Resolver) resolveIP(host string, dnsType uint16) (ip net.IP, err error) { ip = net.ParseIP(host) if ip != nil { - if dnsType == D.TypeAAAA && len(ip) == net.IPv6len { + isIPv4 := ip.To4() != nil + if dnsType == D.TypeAAAA && !isIPv4 { return ip, nil - } else if dnsType == D.TypeA && len(ip) == net.IPv4len { + } else if dnsType == D.TypeA && isIPv4 { return ip, nil } }