Browse Source

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.
master
SomberNight 3 years ago
parent
commit
2e06a7992e
No known key found for this signature in database
GPG Key ID: B33B5F232C6271E9
  1. 10
      electrum/util.py

10
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: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.1) s.settimeout(0.1)
s.connect((host, port)) s.connect((host, port))
# Tor responds uniquely to HTTP-like requests # mimic "tor-resolve 0.0.0.0".
s.send(b"GET\n") # see https://github.com/spesmilo/electrum/issues/7317#issuecomment-1369281075
if b"Tor is not an HTTP Proxy" in s.recv(1024): # > 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 return True
except socket.error: except socket.error:
pass pass

Loading…
Cancel
Save