mirror of
https://github.com/DNSCrypt/dnscrypt-proxy.git
synced 2025-04-05 06:07:36 +03:00
Revert "xtransport layer to netip and immediate dependencies (#2159)"
This reverts commit baee50f1dc
.
This commit is contained in:
parent
e124623ffc
commit
937c1e63e2
3 changed files with 27 additions and 30 deletions
|
@ -931,7 +931,7 @@ func cdLocal() {
|
||||||
|
|
||||||
func isIPAndPort(addrStr string) error {
|
func isIPAndPort(addrStr string) error {
|
||||||
host, port := ExtractHostAndPort(addrStr, -1)
|
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)
|
return fmt.Errorf("Host does not parse as IP '%s'", addrStr)
|
||||||
} else if port == -1 {
|
} else if port == -1 {
|
||||||
return fmt.Errorf("Port missing '%s'", addrStr)
|
return fmt.Errorf("Port missing '%s'", addrStr)
|
||||||
|
|
|
@ -529,7 +529,7 @@ func route(proxy *Proxy, name string, serverProto stamps.StampProtoType) (*Relay
|
||||||
}
|
}
|
||||||
if len(relayCandidateStamp.ServerAddrStr) > 0 {
|
if len(relayCandidateStamp.ServerAddrStr) > 0 {
|
||||||
ipOnly, _ := ExtractHostAndPort(relayCandidateStamp.ServerAddrStr, -1)
|
ipOnly, _ := ExtractHostAndPort(relayCandidateStamp.ServerAddrStr, -1)
|
||||||
if ip, err := ParseIP(ipOnly); err != nil {
|
if ip := ParseIP(ipOnly); ip != nil {
|
||||||
host, _ := ExtractHostAndPort(relayCandidateStamp.ProviderName, -1)
|
host, _ := ExtractHostAndPort(relayCandidateStamp.ProviderName, -1)
|
||||||
proxy.xTransport.saveCachedIP(host, ip, -1*time.Second)
|
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.
|
// in order to fingerprint clients across multiple IP addresses.
|
||||||
if len(stamp.ServerAddrStr) > 0 {
|
if len(stamp.ServerAddrStr) > 0 {
|
||||||
ipOnly, _ := ExtractHostAndPort(stamp.ServerAddrStr, -1)
|
ipOnly, _ := ExtractHostAndPort(stamp.ServerAddrStr, -1)
|
||||||
if ip, err := ParseIP(ipOnly); err != nil {
|
if ip := ParseIP(ipOnly); ip != nil {
|
||||||
host, _ := ExtractHostAndPort(stamp.ProviderName, -1)
|
host, _ := ExtractHostAndPort(stamp.ProviderName, -1)
|
||||||
proxy.xTransport.saveCachedIP(host, ip, -1*time.Second)
|
proxy.xTransport.saveCachedIP(host, ip, -1*time.Second)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,6 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -39,7 +38,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type CachedIPItem struct {
|
type CachedIPItem struct {
|
||||||
ip netip.Addr
|
ip net.IP
|
||||||
expiration *time.Time
|
expiration *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,13 +92,13 @@ func NewXTransport() *XTransport {
|
||||||
return &xTransport
|
return &xTransport
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseIP(ipStr string) (netip.Addr, error) {
|
func ParseIP(ipStr string) net.IP {
|
||||||
return netip.ParseAddr(strings.TrimRight(strings.TrimLeft(ipStr, "["), "]"))
|
return net.ParseIP(strings.TrimRight(strings.TrimLeft(ipStr, "["), "]"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// If ttl < 0, never expire
|
// If ttl < 0, never expire
|
||||||
// Otherwise, ttl is set to max(ttl, MinResolverIPTTL)
|
// 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}
|
item := &CachedIPItem{ip: ip, expiration: nil}
|
||||||
if ttl >= 0 {
|
if ttl >= 0 {
|
||||||
if ttl < MinResolverIPTTL {
|
if ttl < MinResolverIPTTL {
|
||||||
|
@ -113,8 +112,8 @@ func (xTransport *XTransport) saveCachedIP(host string, ip netip.Addr, ttl time.
|
||||||
xTransport.cachedIPs.Unlock()
|
xTransport.cachedIPs.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (xTransport *XTransport) loadCachedIP(host string) (ip netip.Addr, expired bool) {
|
func (xTransport *XTransport) loadCachedIP(host string) (ip net.IP, expired bool) {
|
||||||
ip, expired = netip.IPv4Unspecified(), false
|
ip, expired = nil, false
|
||||||
xTransport.cachedIPs.RLock()
|
xTransport.cachedIPs.RLock()
|
||||||
item, ok := xTransport.cachedIPs.cache[host]
|
item, ok := xTransport.cachedIPs.cache[host]
|
||||||
xTransport.cachedIPs.RUnlock()
|
xTransport.cachedIPs.RUnlock()
|
||||||
|
@ -149,9 +148,9 @@ func (xTransport *XTransport) rebuildTransport() {
|
||||||
// resolveAndUpdateCache() is always called in `Fetch()` before the `Dial()`
|
// resolveAndUpdateCache() is always called in `Fetch()` before the `Dial()`
|
||||||
// method is used, so that a cached entry must be present at this point.
|
// method is used, so that a cached entry must be present at this point.
|
||||||
cachedIP, _ := xTransport.loadCachedIP(host)
|
cachedIP, _ := xTransport.loadCachedIP(host)
|
||||||
if cachedIP != netip.IPv4Unspecified() {
|
if cachedIP != nil {
|
||||||
if cachedIP.Is4() {
|
if ipv4 := cachedIP.To4(); ipv4 != nil {
|
||||||
ipOnly = cachedIP.String()
|
ipOnly = ipv4.String()
|
||||||
} else {
|
} else {
|
||||||
ipOnly = "[" + cachedIP.String() + "]"
|
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
|
ttl = SystemResolverIPTTL
|
||||||
var foundIPs []string
|
var foundIPs []string
|
||||||
foundIPs, err = net.LookupHost(host)
|
foundIPs, err = net.LookupHost(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ips := make([]netip.Addr, 0)
|
ips := make([]net.IP, 0)
|
||||||
for _, ip := range foundIPs {
|
for _, ip := range foundIPs {
|
||||||
if foundIP, err := netip.ParseAddr(ip); err == nil {
|
if foundIP := net.ParseIP(ip); foundIP != nil {
|
||||||
if xTransport.useIPv4 {
|
if xTransport.useIPv4 {
|
||||||
if foundIP.Is4() {
|
if ipv4 := foundIP.To4(); ipv4 != nil {
|
||||||
ips = append(ips, foundIP)
|
ips = append(ips, foundIP)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if xTransport.useIPv6 {
|
if xTransport.useIPv6 {
|
||||||
if foundIP.Is6() || foundIP.Is4In6() {
|
if ipv6 := foundIP.To16(); ipv6 != nil {
|
||||||
ips = append(ips, foundIP)
|
ips = append(ips, foundIP)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,7 +259,7 @@ func (xTransport *XTransport) resolveUsingSystem(host string) (ip netip.Addr, tt
|
||||||
func (xTransport *XTransport) resolveUsingResolver(
|
func (xTransport *XTransport) resolveUsingResolver(
|
||||||
proto, host string,
|
proto, host string,
|
||||||
resolver string,
|
resolver string,
|
||||||
) (ip netip.Addr, ttl time.Duration, err error) {
|
) (ip net.IP, ttl time.Duration, err error) {
|
||||||
dnsClient := dns.Client{Net: proto}
|
dnsClient := dns.Client{Net: proto}
|
||||||
if xTransport.useIPv4 {
|
if xTransport.useIPv4 {
|
||||||
msg := dns.Msg{}
|
msg := dns.Msg{}
|
||||||
|
@ -276,7 +275,7 @@ func (xTransport *XTransport) resolveUsingResolver(
|
||||||
}
|
}
|
||||||
if len(answers) > 0 {
|
if len(answers) > 0 {
|
||||||
answer := answers[rand.Intn(len(answers))]
|
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
|
ttl = time.Duration(answer.Header().Ttl) * time.Second
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -296,7 +295,7 @@ func (xTransport *XTransport) resolveUsingResolver(
|
||||||
}
|
}
|
||||||
if len(answers) > 0 {
|
if len(answers) > 0 {
|
||||||
answer := answers[rand.Intn(len(answers))]
|
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
|
ttl = time.Duration(answer.Header().Ttl) * time.Second
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -308,7 +307,7 @@ func (xTransport *XTransport) resolveUsingResolver(
|
||||||
func (xTransport *XTransport) resolveUsingResolvers(
|
func (xTransport *XTransport) resolveUsingResolvers(
|
||||||
proto, host string,
|
proto, host string,
|
||||||
resolvers []string,
|
resolvers []string,
|
||||||
) (ip netip.Addr, ttl time.Duration, err error) {
|
) (ip net.IP, ttl time.Duration, err error) {
|
||||||
for i, resolver := range resolvers {
|
for i, resolver := range resolvers {
|
||||||
ip, ttl, err = xTransport.resolveUsingResolver(proto, host, resolver)
|
ip, ttl, err = xTransport.resolveUsingResolver(proto, host, resolver)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -328,18 +327,16 @@ func (xTransport *XTransport) resolveAndUpdateCache(host string) error {
|
||||||
if xTransport.proxyDialer != nil || xTransport.httpProxyFunction != nil {
|
if xTransport.proxyDialer != nil || xTransport.httpProxyFunction != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, err := ParseIP(host)
|
if ParseIP(host) != nil {
|
||||||
if err != nil {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedIP, expired := xTransport.loadCachedIP(host)
|
cachedIP, expired := xTransport.loadCachedIP(host)
|
||||||
if cachedIP != netip.IPv4Unspecified() && !expired {
|
if cachedIP != nil && !expired {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var foundIP netip.Addr
|
var foundIP net.IP
|
||||||
var ttl time.Duration
|
var ttl time.Duration
|
||||||
|
var err error
|
||||||
if !xTransport.ignoreSystemDNS {
|
if !xTransport.ignoreSystemDNS {
|
||||||
foundIP, ttl, err = xTransport.resolveUsingSystem(host)
|
foundIP, ttl, err = xTransport.resolveUsingSystem(host)
|
||||||
}
|
}
|
||||||
|
@ -372,7 +369,7 @@ func (xTransport *XTransport) resolveAndUpdateCache(host string) error {
|
||||||
ttl = MinResolverIPTTL
|
ttl = MinResolverIPTTL
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cachedIP != netip.IPv4Unspecified() {
|
if cachedIP != nil {
|
||||||
dlog.Noticef("Using stale [%v] cached address for a grace period", host)
|
dlog.Noticef("Using stale [%v] cached address for a grace period", host)
|
||||||
foundIP = cachedIP
|
foundIP = cachedIP
|
||||||
ttl = ExpiredCachedIPGraceTTL
|
ttl = ExpiredCachedIPGraceTTL
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue