Start moving things to a custom transport

This commit is contained in:
Frank Denis 2018-01-30 13:29:47 +01:00
parent 367f7fd675
commit 24c21d5eb2
3 changed files with 7 additions and 44 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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{