From be1e99ea3289f1bf70f85f3cc528283c7fd4b540 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 27 Jan 2018 16:48:22 +0100 Subject: [PATCH] DoH: send a dummy query before measuring the RTT to ignore the handshake --- dnscrypt-proxy/main.go | 5 ++--- dnscrypt-proxy/serversInfo.go | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/dnscrypt-proxy/main.go b/dnscrypt-proxy/main.go index 994173b0..2d857847 100644 --- a/dnscrypt-proxy/main.go +++ b/dnscrypt-proxy/main.go @@ -399,9 +399,8 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str Timeout: proxy.timeout, } resp, err := client.Do(req) - if err == nil && resp != nil && (resp.StatusCode < 200 || resp.StatusCode > 299) { - return - } else if err != nil || resp == nil { + if (err == nil && resp != nil && (resp.StatusCode < 200 || resp.StatusCode > 299)) || + err != nil || resp == nil { return } response, err = ioutil.ReadAll(resp.Body) diff --git a/dnscrypt-proxy/serversInfo.go b/dnscrypt-proxy/serversInfo.go index a0cbc08d..1f5e925f 100644 --- a/dnscrypt-proxy/serversInfo.go +++ b/dnscrypt-proxy/serversInfo.go @@ -201,6 +201,19 @@ func (serversInfo *ServersInfo) fetchDoHServerInfo(proxy *Proxy, name string, st Host: stamp.providerName, Path: stamp.path, } + client := http.Client{ + Transport: proxy.httpTransport, + Timeout: proxy.timeout, + } + preReq := &http.Request{ + Method: "HEAD", + URL: url, + Close: false, + Host: stamp.providerName, + } + if _, err := client.Do(preReq); err != nil { + return ServerInfo{}, err + } body := ioutil.NopCloser(bytes.NewReader([]byte{ 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, })) @@ -216,13 +229,9 @@ func (serversInfo *ServersInfo) fetchDoHServerInfo(proxy *Proxy, name string, st Host: stamp.providerName, Body: body, } - client := http.Client{ - Transport: proxy.httpTransport, - Timeout: proxy.timeout, - } start := time.Now() resp, err := client.Do(req) - elapsed := time.Since(start) + rtt := time.Since(start) if err == nil && resp != nil && (resp.StatusCode < 200 || resp.StatusCode > 299) { return ServerInfo{}, fmt.Errorf("Webserver returned code %d", resp.StatusCode) } else if err != nil { @@ -237,13 +246,15 @@ func (serversInfo *ServersInfo) fetchDoHServerInfo(proxy *Proxy, name string, st if len(respBody) < MinDNSPacketSize || len(respBody) > MaxDNSPacketSize { return ServerInfo{}, errors.New("Webserver returned an unexpected response") } + dlog.Noticef("[%s] OK (DoH) - rtt: %dms", name, rtt.Nanoseconds()/1000000) + serverInfo := ServerInfo{ Proto: StampProtoTypeDoH, Name: name, Timeout: proxy.timeout, URL: url, HostName: stamp.providerName, - initialRtt: int(elapsed.Nanoseconds() / 1000000), + initialRtt: int(rtt.Nanoseconds() / 1000000), } return serverInfo, nil }