From cd42e9832c4caae5824a76f93325210667049329 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 12 Apr 2023 22:06:21 +0800 Subject: [PATCH] chore: resolver priority return TypeA in ResolveIP (not effected LookupIP) --- component/dialer/dialer.go | 13 +------------ component/resolver/resolver.go | 22 ++++++++++++++++----- dns/resolver.go | 35 ---------------------------------- 3 files changed, 18 insertions(+), 52 deletions(-) diff --git a/component/dialer/dialer.go b/component/dialer/dialer.go index f6dfaf86..5a55cd22 100644 --- a/component/dialer/dialer.go +++ b/component/dialer/dialer.go @@ -157,7 +157,7 @@ func concurrentDualStackDialContext(ctx context.Context, network string, ips []n } func dualStackDialContext(ctx context.Context, dialFn dialFunc, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) { - ipv4s, ipv6s := sortationAddr(ips) + ipv4s, ipv6s := resolver.SortationAddr(ips) preferIPVersion := opt.prefer fallbackTicker := time.NewTicker(fallbackTimeout) @@ -309,17 +309,6 @@ func parseAddr(ctx context.Context, network, address string, preferResolver reso return ips, port, nil } -func sortationAddr(ips []netip.Addr) (ipv4s, ipv6s []netip.Addr) { - for _, v := range ips { - if v.Is4() { // 4in6 parse was in parseAddr - ipv4s = append(ipv4s, v) - } else { - ipv6s = append(ipv6s, v) - } - } - return -} - type Dialer struct { Opt option } diff --git a/component/resolver/resolver.go b/component/resolver/resolver.go index 3b5426d8..6be6a95f 100644 --- a/component/resolver/resolver.go +++ b/component/resolver/resolver.go @@ -44,9 +44,6 @@ type Resolver interface { LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error) LookupIPv4(ctx context.Context, host string) (ips []netip.Addr, err error) LookupIPv6(ctx context.Context, host string) (ips []netip.Addr, err error) - ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error) - ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error) - ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error) ExchangeContext(ctx context.Context, m *dns.Msg) (msg *dns.Msg, err error) Invalid() bool } @@ -201,10 +198,14 @@ func ResolveIPWithResolver(ctx context.Context, host string, r Resolver) (netip. } else if len(ips) == 0 { return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host) } - return ips[fastrand.Intn(len(ips))], nil + ipv4s, ipv6s := SortationAddr(ips) + if len(ipv4s) > 0 { + return ipv4s[fastrand.Intn(len(ipv4s))], nil + } + return ipv6s[fastrand.Intn(len(ipv6s))], nil } -// ResolveIP with a host, return ip +// ResolveIP with a host, return ip and priority return TypeA func ResolveIP(ctx context.Context, host string) (netip.Addr, error) { return ResolveIPWithResolver(ctx, host, DefaultResolver) } @@ -265,3 +266,14 @@ func LookupIPProxyServerHost(ctx context.Context, host string) ([]netip.Addr, er } return LookupIP(ctx, host) } + +func SortationAddr(ips []netip.Addr) (ipv4s, ipv6s []netip.Addr) { + for _, v := range ips { + if v.Unmap().Is4() { + ipv4s = append(ipv4s, v) + } else { + ipv6s = append(ipv6s, v) + } + } + return +} diff --git a/dns/resolver.go b/dns/resolver.go index 467e9f14..df8ed3d1 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -3,7 +3,6 @@ package dns import ( "context" "errors" - "fmt" "net/netip" "strings" "time" @@ -20,7 +19,6 @@ import ( "github.com/Dreamacro/clash/log" D "github.com/miekg/dns" - "github.com/zhangyunhao116/fastrand" "golang.org/x/sync/singleflight" ) @@ -119,49 +117,16 @@ func (r *Resolver) LookupIP(ctx context.Context, host string) (ips []netip.Addr, return ips, nil } -// ResolveIP request with TypeA and TypeAAAA, priority return TypeA -func (r *Resolver) ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error) { - ips, err := r.LookupIPPrimaryIPv4(ctx, host) - if err != nil { - return netip.Addr{}, err - } else if len(ips) == 0 { - return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host) - } - return ips[fastrand.Intn(len(ips))], nil -} - // LookupIPv4 request with TypeA func (r *Resolver) LookupIPv4(ctx context.Context, host string) ([]netip.Addr, error) { return r.lookupIP(ctx, host, D.TypeA) } -// ResolveIPv4 request with TypeA -func (r *Resolver) ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error) { - ips, err := r.lookupIP(ctx, host, D.TypeA) - if err != nil { - return netip.Addr{}, err - } else if len(ips) == 0 { - return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host) - } - return ips[fastrand.Intn(len(ips))], nil -} - // LookupIPv6 request with TypeAAAA func (r *Resolver) LookupIPv6(ctx context.Context, host string) ([]netip.Addr, error) { return r.lookupIP(ctx, host, D.TypeAAAA) } -// ResolveIPv6 request with TypeAAAA -func (r *Resolver) ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error) { - ips, err := r.lookupIP(ctx, host, D.TypeAAAA) - if err != nil { - return netip.Addr{}, err - } else if len(ips) == 0 { - return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host) - } - return ips[fastrand.Intn(len(ips))], nil -} - func (r *Resolver) shouldIPFallback(ip netip.Addr) bool { for _, filter := range r.fallbackIPFilters { if filter.Match(ip) {