diff --git a/dnscrypt-proxy/main.go b/dnscrypt-proxy/main.go index 82e59606..1606c744 100644 --- a/dnscrypt-proxy/main.go +++ b/dnscrypt-proxy/main.go @@ -45,6 +45,7 @@ func main() { if err := ConfigLoad(&app.proxy, svcFlag, ConfigFileName); err != nil { dlog.Fatal(err) } + app.proxy.xTransport = NewXTransport(app.proxy.timeout) dlog.Noticef("Starting dnscrypt-proxy %s", AppVersion) if len(*svcFlag) != 0 { @@ -75,7 +76,6 @@ func main() { func (app *App) Start(service service.Service) error { proxy := app.proxy - proxy.cachedIPs.cache = make(map[string]string) if err := InitPluginsGlobals(&proxy.pluginsGlobals, &proxy); err != nil { dlog.Fatal(err) } diff --git a/dnscrypt-proxy/proxy.go b/dnscrypt-proxy/proxy.go index ada5a041..8ce19100 100644 --- a/dnscrypt-proxy/proxy.go +++ b/dnscrypt-proxy/proxy.go @@ -2,13 +2,10 @@ package main import ( "bytes" - "context" "io/ioutil" "math/rand" "net" "net/http" - "strings" - "sync" "sync/atomic" "time" @@ -16,11 +13,6 @@ import ( "golang.org/x/crypto/curve25519" ) -type CachedIPs struct { - sync.RWMutex - cache map[string]string -} - type Proxy struct { proxyPublicKey [32]byte proxySecretKey [32]byte @@ -56,8 +48,7 @@ type Proxy struct { urlsToPrefetch []URLToPrefetch clientsCount uint32 maxClients uint32 - httpTransport *http.Transport - cachedIPs CachedIPs + xTransport *XTransport } func (proxy *Proxy) StartProxy() { @@ -69,34 +60,6 @@ func (proxy *Proxy) StartProxy() { for _, registeredServer := range proxy.registeredServers { proxy.serversInfo.registerServer(proxy, registeredServer.name, registeredServer.stamp) } - dialer := &net.Dialer{ - Timeout: proxy.timeout, - KeepAlive: proxy.timeout, - DualStack: true, - } - proxy.httpTransport = &http.Transport{ - DisableKeepAlives: false, - DisableCompression: true, - MaxIdleConns: 1, - IdleConnTimeout: proxy.timeout, - ResponseHeaderTimeout: proxy.timeout, - ExpectContinueTimeout: proxy.timeout, - MaxResponseHeaderBytes: 4096, - DialContext: func(ctx context.Context, network, addrStr string) (net.Conn, error) { - host := addrStr[:strings.LastIndex(addrStr, ":")] - ipOnly := host - proxy.cachedIPs.RLock() - cachedIP := proxy.cachedIPs.cache[host] - proxy.cachedIPs.RUnlock() - if len(cachedIP) > 0 { - ipOnly = cachedIP - } else { - dlog.Debugf("[%s] IP address was not cached", host) - } - addrStr = ipOnly + addrStr[strings.LastIndex(addrStr, ":"):] - return dialer.DialContext(ctx, network, addrStr) - }, - } for _, listenAddrStr := range proxy.listenAddresses { listenUDPAddr, err := net.ResolveUDPAddr("udp", listenAddrStr) if err != nil { @@ -328,7 +291,7 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str Body: ioutil.NopCloser(bytes.NewReader(query)), } client := http.Client{ - Transport: proxy.httpTransport, + Transport: proxy.xTransport.transport, Timeout: proxy.timeout, } resp, err := client.Do(req) diff --git a/dnscrypt-proxy/serversInfo.go b/dnscrypt-proxy/serversInfo.go index b85fc7f0..680c30f7 100644 --- a/dnscrypt-proxy/serversInfo.go +++ b/dnscrypt-proxy/serversInfo.go @@ -200,9 +200,9 @@ func (serversInfo *ServersInfo) fetchDoHServerInfo(proxy *Proxy, name string, st if len(stamp.serverAddrStr) > 0 { addrStr := stamp.serverAddrStr ipOnly := addrStr[:strings.LastIndex(addrStr, ":")] - proxy.cachedIPs.Lock() - proxy.cachedIPs.cache[stamp.providerName] = ipOnly - proxy.cachedIPs.Unlock() + proxy.xTransport.cachedIPs.Lock() + proxy.xTransport.cachedIPs.cache[stamp.providerName] = ipOnly + proxy.xTransport.cachedIPs.Unlock() } url := &url.URL{ Scheme: "https", @@ -210,7 +210,7 @@ func (serversInfo *ServersInfo) fetchDoHServerInfo(proxy *Proxy, name string, st Path: stamp.path, } client := http.Client{ - Transport: proxy.httpTransport, + Transport: proxy.xTransport.transport, Timeout: proxy.timeout, } preReq := &http.Request{