Skip to content

Commit

Permalink
network: add oneserver/auto_connect both enabled checks, avoid connec…
Browse files Browse the repository at this point in the history
…ting to random/multiple servers

if oneserver is enabled.
  • Loading branch information
accumulator committed Oct 11, 2024
1 parent da16613 commit 3c5a748
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions electrum/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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]):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3c5a748

Please sign in to comment.