Fix: clearer error and ipv6 string parse (#325)

This commit is contained in:
Comzyh 2019-09-27 10:33:37 +08:00 committed by Dreamacro
parent 904c354ee4
commit 045c3a3ad4
3 changed files with 33 additions and 19 deletions

View file

@ -115,6 +115,7 @@ func dialTimeout(network, address string, timeout time.Duration) (net.Conn, erro
type dialResult struct {
net.Conn
error
resolved bool
ipv6 bool
done bool
}
@ -142,6 +143,7 @@ func dialTimeout(network, address string, timeout time.Duration) (net.Conn, erro
if result.error != nil {
return
}
result.resolved = true
if ipv6 {
result.Conn, result.error = dialer.DialContext(ctx, "tcp6", net.JoinHostPort(ip.String(), port))
@ -160,14 +162,20 @@ func dialTimeout(network, address string, timeout time.Duration) (net.Conn, erro
return res.Conn, nil
}
if res.ipv6 {
if !res.ipv6 {
primary = res
} else {
fallback = res
}
if primary.done && fallback.done {
if primary.resolved {
return nil, primary.error
} else if fallback.resolved {
return nil, fallback.error
} else {
return nil, primary.error
}
}
}
}

View file

@ -3,10 +3,12 @@ package dns
import (
"errors"
"net"
"strings"
)
var (
errIPNotFound = errors.New("cannot found ip")
errIPVersion = errors.New("ip version error")
)
// ResolveIPv4 with a host, return ipv4
@ -18,8 +20,11 @@ func ResolveIPv4(host string) (net.IP, error) {
}
ip := net.ParseIP(host)
if ip4 := ip.To4(); ip4 != nil {
return ip4, nil
if ip != nil {
if !strings.Contains(host, ":") {
return ip, nil
}
return nil, errIPVersion
}
if DefaultResolver != nil {
@ -32,8 +37,8 @@ func ResolveIPv4(host string) (net.IP, error) {
}
for _, ip := range ipAddrs {
if ip4 := ip.To4(); ip4 != nil {
return ip4, nil
if len(ip) == net.IPv4len {
return ip, nil
}
}
@ -49,8 +54,11 @@ func ResolveIPv6(host string) (net.IP, error) {
}
ip := net.ParseIP(host)
if ip6 := ip.To16(); ip6 != nil {
return ip6, nil
if ip != nil {
if strings.Contains(host, ":") {
return ip, nil
}
return nil, errIPVersion
}
if DefaultResolver != nil {
@ -63,8 +71,8 @@ func ResolveIPv6(host string) (net.IP, error) {
}
for _, ip := range ipAddrs {
if ip6 := ip.To16(); ip6 != nil {
return ip6, nil
if len(ip) == net.IPv6len {
return ip, nil
}
}

View file

@ -221,13 +221,11 @@ 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 dnsType == D.TypeAAAA {
if ip6 := ip.To16(); ip6 != nil {
return ip6, nil
}
} else {
if ip4 := ip.To4(); ip4 != nil {
return ip4, nil
if ip != nil {
if dnsType == D.TypeAAAA && len(ip) == net.IPv6len {
return ip, nil
} else if dnsType == D.TypeA && len(ip) == net.IPv4len {
return ip, nil
}
}