From 7f221afeff84cb32b05d4f98d9477cc796aecc8d Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 28 Mar 2018 11:52:04 +0200 Subject: [PATCH] Don't assume that DoH servers use port 443 --- dnscrypt-proxy/common.go | 12 +++++++++++- dnscrypt-proxy/config.go | 9 ++++----- dnscrypt-proxy/stamps.go | 8 +++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/dnscrypt-proxy/common.go b/dnscrypt-proxy/common.go index 9faf8da9..eef8885c 100644 --- a/dnscrypt-proxy/common.go +++ b/dnscrypt-proxy/common.go @@ -64,7 +64,7 @@ func ReadPrefixed(conn *net.TCPConn) ([]byte, error) { } } if pos >= 2+packetLength { - return buf[2:2+packetLength], nil + return buf[2 : 2+packetLength], nil } } } @@ -124,3 +124,13 @@ func StringQuote(str string) string { str = strconv.QuoteToGraphic(str) return str[1 : len(str)-1] } + +func ExtractPort(str string, defaultPort int) int { + port := defaultPort + if idx := strings.LastIndex(str, ":"); idx >= 0 && idx < len(str)-1 { + if portX, err := strconv.Atoi(str[idx+1:]); err == nil { + port = portX + } + } + return port +} diff --git a/dnscrypt-proxy/config.go b/dnscrypt-proxy/config.go index 6fc057de..ed07089e 100644 --- a/dnscrypt-proxy/config.go +++ b/dnscrypt-proxy/config.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path/filepath" - "strconv" "strings" "time" @@ -311,10 +310,10 @@ func (config *Config) printRegisteredServers(proxy *Proxy, jsonOutput bool) { var summary []ServerSummary for _, registeredServer := range proxy.registeredServers { addrStr, port := registeredServer.stamp.serverAddrStr, DefaultPort - if idx := strings.LastIndex(addrStr, ":"); idx >= 0 && idx < len(addrStr)-1 { - if portX, err := strconv.Atoi(addrStr[idx+1:]); err == nil { - port = portX - } + port = ExtractPort(addrStr, port) + if registeredServer.stamp.proto == StampProtoTypeDoH && len(registeredServer.stamp.providerName) > 0 { + providerName := registeredServer.stamp.providerName + port = ExtractPort(providerName, port) } serverSummary := ServerSummary{ Name: registeredServer.name, diff --git a/dnscrypt-proxy/stamps.go b/dnscrypt-proxy/stamps.go index eabd6552..c7388ad6 100644 --- a/dnscrypt-proxy/stamps.go +++ b/dnscrypt-proxy/stamps.go @@ -143,9 +143,6 @@ func newDoHServerStamp(bin []byte) (ServerStamp, error) { pos++ stamp.serverAddrStr = string(bin[pos : pos+len]) pos += len - if net.ParseIP(strings.TrimRight(strings.TrimLeft(stamp.serverAddrStr, "["), "]")) != nil { - stamp.serverAddrStr = fmt.Sprintf("%s:%d", stamp.serverAddrStr, DefaultPort) - } for { vlen := int(bin[pos]) @@ -182,6 +179,11 @@ func newDoHServerStamp(bin []byte) (ServerStamp, error) { if pos != binLen { return stamp, errors.New("Invalid stamp (garbage after end)") } + + if net.ParseIP(strings.TrimRight(strings.TrimLeft(stamp.serverAddrStr, "["), "]")) != nil { + stamp.serverAddrStr = fmt.Sprintf("%s:%d", stamp.serverAddrStr, DefaultPort) + } + return stamp, nil }