From 3c5a74828568ff81e7ff3b67d1c00c72cf7297e7 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Fri, 11 Oct 2024 10:35:46 +0200 Subject: [PATCH] network: add oneserver/auto_connect both enabled checks, avoid connecting to random/multiple servers if oneserver is enabled. --- electrum/network.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/electrum/network.py b/electrum/network.py index 9a990a08718b..5e3bbda721b1 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -517,6 +517,12 @@ def get_parameters(self) -> NetworkParameters: def _init_parameters_from_config(self) -> None: dns_hacks.configure_dns_resolver() self.auto_connect = self.config.NETWORK_AUTO_CONNECT + if self.auto_connect and self.config.NETWORK_ONESERVER: + # enabling both oneserver and auto_connect doesn't really make sense + # assume oneserver is enabled for privacy reasons, disable auto_connect + self.logger.warning(f'both "oneserver" and "auto_connect" options enabled, disabling "auto_connect".') + self.auto_connect = False + self._set_default_server() self._set_proxy(deserialize_proxy(self.config.NETWORK_PROXY, self.config.NETWORK_PROXY_USER, self.config.NETWORK_PROXY_PASSWORD)) @@ -639,7 +645,12 @@ def _set_default_server(self) -> None: self.logger.warning(f'failed to parse server-string ({server!r}); falling back to localhost:1:s.') self.default_server = ServerAddr.from_str("localhost:1:s") else: - self.default_server = pick_random_server(allowed_protocols=self._allowed_protocols) + # if oneserver is enabled but no server specified then don't pick a random server + if self.config.NETWORK_ONESERVER: + self.logger.warning(f'"oneserver" option enabled, but no "server" defined; falling back to localhost:1:s.') + self.default_server = ServerAddr.from_str("localhost:1:s") + else: + self.default_server = pick_random_server(allowed_protocols=self._allowed_protocols) assert isinstance(self.default_server, ServerAddr), f"invalid type for default_server: {self.default_server!r}" def _set_proxy(self, proxy: Optional[dict]): @@ -684,7 +695,14 @@ async def set_parameters(self, net_params: NetworkParameters): int(proxy['port']) except Exception: return - self.config.NETWORK_AUTO_CONNECT = net_params.auto_connect + auto_connect = net_params.auto_connect + if auto_connect and net_params.oneserver: + # enabling both oneserver and auto_connect doesn't really make sense + # assume oneserver is enabled for privacy reasons, disable auto_connect + self.logger.warning(f'both "oneserver" and "auto_connect" options enabled, disabling "auto_connect".') + auto_connect = False + + self.config.NETWORK_AUTO_CONNECT = auto_connect self.config.NETWORK_ONESERVER = net_params.oneserver self.config.NETWORK_PROXY = proxy_str self.config.NETWORK_PROXY_USER = proxy_user