From e95720cf3a3ac214a6a9882e1cffdcedf17a010e Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 22 Jun 2023 09:43:20 +0200 Subject: [PATCH] tor: short circuit host lookup if connecting to IP With this commit we avoid calling LookupHost if we already have an IPv4 or IPv6 address, as we can return that directly. This avoids asking Tor to resolve an IPv6 address, which it cannot do. --- tor/tor.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tor/tor.go b/tor/tor.go index f2cb472e8..37d3fc289 100644 --- a/tor/tor.go +++ b/tor/tor.go @@ -220,12 +220,20 @@ func ResolveTCPAddr(address, socksAddr string) (*net.TCPAddr, error) { return nil, err } - ip, err := LookupHost(host, socksAddr) + p, err := strconv.Atoi(port) if err != nil { return nil, err } - p, err := strconv.Atoi(port) + // Do we already have an IP? Then we don't need to look up anything. + if ip := net.ParseIP(host); ip != nil { + return &net.TCPAddr{ + IP: ip, + Port: p, + }, nil + } + + ip, err := LookupHost(host, socksAddr) if err != nil { return nil, err }