From 2e06a7992e7d692a2ade81dff01ecf2ff9c98e09 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 3 Jan 2023 14:49:57 +0000 Subject: [PATCH] util: improve Tor SOCKS detection see https://github.com/spesmilo/electrum/issues/7317#issuecomment-1369281075 I guess this should be at least as reliable as the old method - however the old method would log an error for the Tor process, and this does not. Suggested by Tor contributor: trinity-1686a. --- electrum/util.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/electrum/util.py b/electrum/util.py index a4d05d8d9..e2e5cdffe 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -1490,9 +1490,13 @@ def is_tor_socks_port(host: str, port: int) -> bool: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.settimeout(0.1) s.connect((host, port)) - # Tor responds uniquely to HTTP-like requests - s.send(b"GET\n") - if b"Tor is not an HTTP Proxy" in s.recv(1024): + # mimic "tor-resolve 0.0.0.0". + # see https://github.com/spesmilo/electrum/issues/7317#issuecomment-1369281075 + # > this is a socks5 handshake, followed by a socks RESOLVE request as defined in + # > [tor's socks extension spec](https://github.com/torproject/torspec/blob/7116c9cdaba248aae07a3f1d0e15d9dd102f62c5/socks-extensions.txt#L63), + # > resolving 0.0.0.0, which being an IP, tor resolves itself without needing to ask a relay. + s.send(b'\x05\x01\x00\x05\xf0\x00\x03\x070.0.0.0\x00\x00') + if s.recv(1024) == b'\x05\x00\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00': return True except socket.error: pass