Skip to content

Commit

Permalink
network: re-detect is_proxy_tor every time we connect to a server
Browse files Browse the repository at this point in the history
This avoids some false negatives for is_proxy_tor.
(previously we only set is_proxy_tor when the proxy settings were changed)
In particular, consider scenario:
- Tor browser not running
- user sets "localhost:9150" as proxy
- detection sets network.is_proxy_tor to False
- user starts Tor browser
- network, due to retries, finds proxy working and connects to some servers
- network.is_proxy_tor remains False
  • Loading branch information
SomberNight committed Apr 25, 2024
1 parent 2a4c5d9 commit b9a2b0d
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions electrum/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,25 +648,28 @@ def _set_proxy(self, proxy: Optional[dict]):

self.logger.info(f'setting proxy {proxy}')
self.proxy = proxy

# reset is_proxy_tor to unknown, and re-detect it:
self.is_proxy_tor = None
self._detect_if_proxy_is_tor()

util.trigger_callback('proxy_set', self.proxy)

def _detect_if_proxy_is_tor(self) -> None:
def tor_probe_task(p):
assert p is not None
tor_proxy = util.is_tor_socks_port(p['host'], int(p['port']))
is_tor = util.is_tor_socks_port(p['host'], int(p['port']))
if self.proxy == p: # is this the proxy we probed?
self.logger.info(f'Proxy is {"" if tor_proxy else "not "}TOR')
self._tor_probe_done(tor_proxy)
if self.is_proxy_tor != is_tor:
self.logger.info(f'Proxy is {"" if is_tor else "not "}TOR')
self.is_proxy_tor = is_tor
util.trigger_callback('tor_probed', is_tor)

proxy = self.proxy
if proxy and proxy['mode'] == 'socks5':
t = threading.Thread(target=tor_probe_task, args=(proxy,), daemon=True)
t.start()

util.trigger_callback('proxy_set', self.proxy)

def _tor_probe_done(self, is_tor: bool):
self.is_proxy_tor = is_tor
util.trigger_callback('tor_probed', is_tor)

@log_exceptions
async def set_parameters(self, net_params: NetworkParameters):
proxy = net_params.proxy
Expand Down Expand Up @@ -904,6 +907,9 @@ async def _run_new_interface(self, server: ServerAddr):
self._has_ever_managed_to_connect_to_server = True
self._add_recent_server(server)
util.trigger_callback('network_updated')
# When the proxy settings were set, the proxy (if any) might have been unreachable,
# resulting in a false-negative for Tor-detection. Given we just connected to a server, re-test now.
self._detect_if_proxy_is_tor()

def check_interface_against_healthy_spread_of_connected_servers(self, iface_to_check: Interface) -> bool:
# main interface is exempt. this makes switching servers easier
Expand Down

0 comments on commit b9a2b0d

Please sign in to comment.