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 { type dialResult struct {
net.Conn net.Conn
error error
resolved bool
ipv6 bool ipv6 bool
done bool done bool
} }
@ -142,6 +143,7 @@ func dialTimeout(network, address string, timeout time.Duration) (net.Conn, erro
if result.error != nil { if result.error != nil {
return return
} }
result.resolved = true
if ipv6 { if ipv6 {
result.Conn, result.error = dialer.DialContext(ctx, "tcp6", net.JoinHostPort(ip.String(), port)) 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 return res.Conn, nil
} }
if res.ipv6 { if !res.ipv6 {
primary = res primary = res
} else { } else {
fallback = res fallback = res
} }
if primary.done && fallback.done { if primary.done && fallback.done {
if primary.resolved {
return nil, primary.error 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 ( import (
"errors" "errors"
"net" "net"
"strings"
) )
var ( var (
errIPNotFound = errors.New("cannot found ip") errIPNotFound = errors.New("cannot found ip")
errIPVersion = errors.New("ip version error")
) )
// ResolveIPv4 with a host, return ipv4 // ResolveIPv4 with a host, return ipv4
@ -18,8 +20,11 @@ func ResolveIPv4(host string) (net.IP, error) {
} }
ip := net.ParseIP(host) ip := net.ParseIP(host)
if ip4 := ip.To4(); ip4 != nil { if ip != nil {
return ip4, nil if !strings.Contains(host, ":") {
return ip, nil
}
return nil, errIPVersion
} }
if DefaultResolver != nil { if DefaultResolver != nil {
@ -32,8 +37,8 @@ func ResolveIPv4(host string) (net.IP, error) {
} }
for _, ip := range ipAddrs { for _, ip := range ipAddrs {
if ip4 := ip.To4(); ip4 != nil { if len(ip) == net.IPv4len {
return ip4, nil return ip, nil
} }
} }
@ -49,8 +54,11 @@ func ResolveIPv6(host string) (net.IP, error) {
} }
ip := net.ParseIP(host) ip := net.ParseIP(host)
if ip6 := ip.To16(); ip6 != nil { if ip != nil {
return ip6, nil if strings.Contains(host, ":") {
return ip, nil
}
return nil, errIPVersion
} }
if DefaultResolver != nil { if DefaultResolver != nil {
@ -63,8 +71,8 @@ func ResolveIPv6(host string) (net.IP, error) {
} }
for _, ip := range ipAddrs { for _, ip := range ipAddrs {
if ip6 := ip.To16(); ip6 != nil { if len(ip) == net.IPv6len {
return ip6, nil 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) { func (r *Resolver) resolveIP(host string, dnsType uint16) (ip net.IP, err error) {
ip = net.ParseIP(host) ip = net.ParseIP(host)
if dnsType == D.TypeAAAA { if ip != nil {
if ip6 := ip.To16(); ip6 != nil { if dnsType == D.TypeAAAA && len(ip) == net.IPv6len {
return ip6, nil return ip, nil
} } else if dnsType == D.TypeA && len(ip) == net.IPv4len {
} else { return ip, nil
if ip4 := ip.To4(); ip4 != nil {
return ip4, nil
} }
} }