Fix: don't resolve AAAA record when ipv6 is false and use go dns resolver

This commit is contained in:
gVisor bot 2021-04-01 18:03:30 +08:00
parent b2cbe36f0f
commit 4e68b6cd3d
2 changed files with 18 additions and 17 deletions

View file

@ -1,9 +1,11 @@
package resolver package resolver
import ( import (
"context"
"errors" "errors"
"net" "net"
"strings" "strings"
"time"
"github.com/Dreamacro/clash/component/trie" "github.com/Dreamacro/clash/component/trie"
) )
@ -18,6 +20,9 @@ var (
// DefaultHosts aim to resolve hosts // DefaultHosts aim to resolve hosts
DefaultHosts = trie.New() DefaultHosts = trie.New()
// DefaultDNSTimeout defined the default dns request timeout
DefaultDNSTimeout = time.Second * 5
) )
var ( var (
@ -52,18 +57,16 @@ func ResolveIPv4(host string) (net.IP, error) {
return DefaultResolver.ResolveIPv4(host) return DefaultResolver.ResolveIPv4(host)
} }
ipAddrs, err := net.LookupIP(host) ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
defer cancel()
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
if err != nil { if err != nil {
return nil, err return nil, err
} else if len(ipAddrs) == 0 {
return nil, ErrIPNotFound
} }
for _, ip := range ipAddrs { return ipAddrs[0], nil
if ip4 := ip.To4(); ip4 != nil {
return ip4, nil
}
}
return nil, ErrIPNotFound
} }
// ResolveIPv6 with a host, return ipv6 // ResolveIPv6 with a host, return ipv6
@ -90,18 +93,16 @@ func ResolveIPv6(host string) (net.IP, error) {
return DefaultResolver.ResolveIPv6(host) return DefaultResolver.ResolveIPv6(host)
} }
ipAddrs, err := net.LookupIP(host) ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
defer cancel()
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
if err != nil { if err != nil {
return nil, err return nil, err
} else if len(ipAddrs) == 0 {
return nil, ErrIPNotFound
} }
for _, ip := range ipAddrs { return ipAddrs[0], nil
if ip.To4() == nil {
return ip, nil
}
}
return nil, ErrIPNotFound
} }
// ResolveIP with a host, return ip // ResolveIP with a host, return ip

View file

@ -145,7 +145,7 @@ func (r *Resolver) exchangeWithoutCache(m *D.Msg) (msg *D.Msg, err error) {
} }
func (r *Resolver) batchExchange(clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) { func (r *Resolver) batchExchange(clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) {
fast, ctx := picker.WithTimeout(context.Background(), time.Second*5) fast, ctx := picker.WithTimeout(context.Background(), resolver.DefaultDNSTimeout)
for _, client := range clients { for _, client := range clients {
r := client r := client
fast.Go(func() (interface{}, error) { fast.Go(func() (interface{}, error) {