From 937c1e63e25b1a5fe9216c7f9309aac75a88fe57 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 10 Aug 2022 22:24:36 +0200 Subject: [PATCH] Revert "xtransport layer to netip and immediate dependencies (#2159)" This reverts commit baee50f1dce05f54796db6dd5092d8673914a434. --- dnscrypt-proxy/config.go | 2 +- dnscrypt-proxy/serversInfo.go | 6 ++--- dnscrypt-proxy/xtransport.go | 49 ++++++++++++++++------------------- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/dnscrypt-proxy/config.go b/dnscrypt-proxy/config.go index e25ee622..7f784f04 100644 --- a/dnscrypt-proxy/config.go +++ b/dnscrypt-proxy/config.go @@ -931,7 +931,7 @@ func cdLocal() { func isIPAndPort(addrStr string) error { host, port := ExtractHostAndPort(addrStr, -1) - if _, err := ParseIP(host); err != nil { + if ip := ParseIP(host); ip == nil { return fmt.Errorf("Host does not parse as IP '%s'", addrStr) } else if port == -1 { return fmt.Errorf("Port missing '%s'", addrStr) diff --git a/dnscrypt-proxy/serversInfo.go b/dnscrypt-proxy/serversInfo.go index c5631f1e..ae284557 100644 --- a/dnscrypt-proxy/serversInfo.go +++ b/dnscrypt-proxy/serversInfo.go @@ -260,7 +260,7 @@ func (serversInfo *ServersInfo) estimatorUpdate(currentActive int) { if activeCount == serversCount { return } - candidate := rand.Intn(serversCount-activeCount) + activeCount + candidate := rand.Intn(serversCount-activeCount)+activeCount candidateRtt, currentActiveRtt := serversInfo.inner[candidate].rtt.Value(), serversInfo.inner[currentActive].rtt.Value() if currentActiveRtt < 0 { currentActiveRtt = candidateRtt @@ -529,7 +529,7 @@ func route(proxy *Proxy, name string, serverProto stamps.StampProtoType) (*Relay } if len(relayCandidateStamp.ServerAddrStr) > 0 { ipOnly, _ := ExtractHostAndPort(relayCandidateStamp.ServerAddrStr, -1) - if ip, err := ParseIP(ipOnly); err != nil { + if ip := ParseIP(ipOnly); ip != nil { host, _ := ExtractHostAndPort(relayCandidateStamp.ProviderName, -1) proxy.xTransport.saveCachedIP(host, ip, -1*time.Second) } @@ -665,7 +665,7 @@ func fetchDoHServerInfo(proxy *Proxy, name string, stamp stamps.ServerStamp, isN // in order to fingerprint clients across multiple IP addresses. if len(stamp.ServerAddrStr) > 0 { ipOnly, _ := ExtractHostAndPort(stamp.ServerAddrStr, -1) - if ip, err := ParseIP(ipOnly); err != nil { + if ip := ParseIP(ipOnly); ip != nil { host, _ := ExtractHostAndPort(stamp.ProviderName, -1) proxy.xTransport.saveCachedIP(host, ip, -1*time.Second) } diff --git a/dnscrypt-proxy/xtransport.go b/dnscrypt-proxy/xtransport.go index ed1ed22b..aa45aabc 100644 --- a/dnscrypt-proxy/xtransport.go +++ b/dnscrypt-proxy/xtransport.go @@ -14,7 +14,6 @@ import ( "math/rand" "net" "net/http" - "net/netip" "net/url" "strconv" "strings" @@ -39,7 +38,7 @@ const ( ) type CachedIPItem struct { - ip netip.Addr + ip net.IP expiration *time.Time } @@ -93,13 +92,13 @@ func NewXTransport() *XTransport { return &xTransport } -func ParseIP(ipStr string) (netip.Addr, error) { - return netip.ParseAddr(strings.TrimRight(strings.TrimLeft(ipStr, "["), "]")) +func ParseIP(ipStr string) net.IP { + return net.ParseIP(strings.TrimRight(strings.TrimLeft(ipStr, "["), "]")) } // If ttl < 0, never expire // Otherwise, ttl is set to max(ttl, MinResolverIPTTL) -func (xTransport *XTransport) saveCachedIP(host string, ip netip.Addr, ttl time.Duration) { +func (xTransport *XTransport) saveCachedIP(host string, ip net.IP, ttl time.Duration) { item := &CachedIPItem{ip: ip, expiration: nil} if ttl >= 0 { if ttl < MinResolverIPTTL { @@ -113,8 +112,8 @@ func (xTransport *XTransport) saveCachedIP(host string, ip netip.Addr, ttl time. xTransport.cachedIPs.Unlock() } -func (xTransport *XTransport) loadCachedIP(host string) (ip netip.Addr, expired bool) { - ip, expired = netip.IPv4Unspecified(), false +func (xTransport *XTransport) loadCachedIP(host string) (ip net.IP, expired bool) { + ip, expired = nil, false xTransport.cachedIPs.RLock() item, ok := xTransport.cachedIPs.cache[host] xTransport.cachedIPs.RUnlock() @@ -149,9 +148,9 @@ func (xTransport *XTransport) rebuildTransport() { // resolveAndUpdateCache() is always called in `Fetch()` before the `Dial()` // method is used, so that a cached entry must be present at this point. cachedIP, _ := xTransport.loadCachedIP(host) - if cachedIP != netip.IPv4Unspecified() { - if cachedIP.Is4() { - ipOnly = cachedIP.String() + if cachedIP != nil { + if ipv4 := cachedIP.To4(); ipv4 != nil { + ipOnly = ipv4.String() } else { ipOnly = "[" + cachedIP.String() + "]" } @@ -229,23 +228,23 @@ func (xTransport *XTransport) rebuildTransport() { } } -func (xTransport *XTransport) resolveUsingSystem(host string) (ip netip.Addr, ttl time.Duration, err error) { +func (xTransport *XTransport) resolveUsingSystem(host string) (ip net.IP, ttl time.Duration, err error) { ttl = SystemResolverIPTTL var foundIPs []string foundIPs, err = net.LookupHost(host) if err != nil { return } - ips := make([]netip.Addr, 0) + ips := make([]net.IP, 0) for _, ip := range foundIPs { - if foundIP, err := netip.ParseAddr(ip); err == nil { + if foundIP := net.ParseIP(ip); foundIP != nil { if xTransport.useIPv4 { - if foundIP.Is4() { + if ipv4 := foundIP.To4(); ipv4 != nil { ips = append(ips, foundIP) } } if xTransport.useIPv6 { - if foundIP.Is6() || foundIP.Is4In6() { + if ipv6 := foundIP.To16(); ipv6 != nil { ips = append(ips, foundIP) } } @@ -260,7 +259,7 @@ func (xTransport *XTransport) resolveUsingSystem(host string) (ip netip.Addr, tt func (xTransport *XTransport) resolveUsingResolver( proto, host string, resolver string, -) (ip netip.Addr, ttl time.Duration, err error) { +) (ip net.IP, ttl time.Duration, err error) { dnsClient := dns.Client{Net: proto} if xTransport.useIPv4 { msg := dns.Msg{} @@ -276,7 +275,7 @@ func (xTransport *XTransport) resolveUsingResolver( } if len(answers) > 0 { answer := answers[rand.Intn(len(answers))] - ip, _ = netip.ParseAddr(answer.(*dns.A).A.String()) + ip = answer.(*dns.A).A ttl = time.Duration(answer.Header().Ttl) * time.Second return } @@ -296,7 +295,7 @@ func (xTransport *XTransport) resolveUsingResolver( } if len(answers) > 0 { answer := answers[rand.Intn(len(answers))] - ip, _ = netip.ParseAddr(answer.(*dns.AAAA).AAAA.String()) + ip = answer.(*dns.AAAA).AAAA ttl = time.Duration(answer.Header().Ttl) * time.Second return } @@ -308,7 +307,7 @@ func (xTransport *XTransport) resolveUsingResolver( func (xTransport *XTransport) resolveUsingResolvers( proto, host string, resolvers []string, -) (ip netip.Addr, ttl time.Duration, err error) { +) (ip net.IP, ttl time.Duration, err error) { for i, resolver := range resolvers { ip, ttl, err = xTransport.resolveUsingResolver(proto, host, resolver) if err == nil { @@ -328,18 +327,16 @@ func (xTransport *XTransport) resolveAndUpdateCache(host string) error { if xTransport.proxyDialer != nil || xTransport.httpProxyFunction != nil { return nil } - _, err := ParseIP(host) - if err != nil { + if ParseIP(host) != nil { return nil } - cachedIP, expired := xTransport.loadCachedIP(host) - if cachedIP != netip.IPv4Unspecified() && !expired { + if cachedIP != nil && !expired { return nil } - var foundIP netip.Addr + var foundIP net.IP var ttl time.Duration - + var err error if !xTransport.ignoreSystemDNS { foundIP, ttl, err = xTransport.resolveUsingSystem(host) } @@ -372,7 +369,7 @@ func (xTransport *XTransport) resolveAndUpdateCache(host string) error { ttl = MinResolverIPTTL } if err != nil { - if cachedIP != netip.IPv4Unspecified() { + if cachedIP != nil { dlog.Noticef("Using stale [%v] cached address for a grace period", host) foundIP = cachedIP ttl = ExpiredCachedIPGraceTTL