Skip to content

Commit 335a8b6

Browse files
committed
network: add oneserver/auto_connect both enabled checks, avoid connecting to random/multiple servers
if oneserver is enabled.
1 parent da16613 commit 335a8b6

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

electrum/network.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ def get_parameters(self) -> NetworkParameters:
517517
def _init_parameters_from_config(self) -> None:
518518
dns_hacks.configure_dns_resolver()
519519
self.auto_connect = self.config.NETWORK_AUTO_CONNECT
520+
if self.auto_connect and self.config.NETWORK_ONESERVER:
521+
# enabling both oneserver and auto_connect doesn't really make sense
522+
# assume oneserver is enabled for privacy reasons, disable auto_connect
523+
self.logger.warning(f'both "oneserver" and "auto_connect" options enabled, disabling "auto_connect".')
524+
self.auto_connect = False
525+
520526
self._set_default_server()
521527
self._set_proxy(deserialize_proxy(self.config.NETWORK_PROXY, self.config.NETWORK_PROXY_USER,
522528
self.config.NETWORK_PROXY_PASSWORD))
@@ -639,7 +645,12 @@ def _set_default_server(self) -> None:
639645
self.logger.warning(f'failed to parse server-string ({server!r}); falling back to localhost:1:s.')
640646
self.default_server = ServerAddr.from_str("localhost:1:s")
641647
else:
642-
self.default_server = pick_random_server(allowed_protocols=self._allowed_protocols)
648+
# if oneserver is enabled but no server specified then don't pick a random server
649+
if self.config.NETWORK_ONESERVER:
650+
self.logger.warning(f'"oneserver" option enabled, but no "server" defined; falling back to localhost:1:s.')
651+
self.default_server = ServerAddr.from_str("localhost:1:s")
652+
else:
653+
self.default_server = pick_random_server(allowed_protocols=self._allowed_protocols)
643654
assert isinstance(self.default_server, ServerAddr), f"invalid type for default_server: {self.default_server!r}"
644655

645656
def _set_proxy(self, proxy: Optional[dict]):
@@ -684,7 +695,14 @@ async def set_parameters(self, net_params: NetworkParameters):
684695
int(proxy['port'])
685696
except Exception:
686697
return
687-
self.config.NETWORK_AUTO_CONNECT = net_params.auto_connect
698+
auto_connect = net_params.auto_connect
699+
if auto_connect and net_params.oneserver:
700+
# enabling both oneserver and auto_connect doesn't really make sense
701+
# assume oneserver is enabled for privacy reasons, disable auto_connect
702+
self.logger.warning(f'both "oneserver" and "auto_connect" options enabled, disabling "auto_connect".')
703+
auto_connect = False
704+
705+
self.config.NETWORK_AUTO_CONNECT = auto_connect
688706
self.config.NETWORK_ONESERVER = net_params.oneserver
689707
self.config.NETWORK_PROXY = proxy_str
690708
self.config.NETWORK_PROXY_USER = proxy_user

electrum/wizard.py

-2
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,6 @@ def do_configure_server(self, wizard_data: dict):
712712
raise Exception('failed to parse server %s' % wizard_data['server'])
713713
except Exception:
714714
return
715-
else:
716-
oneserver = False
717715
net_params = net_params._replace(server=server, auto_connect=wizard_data['autoconnect'], oneserver=oneserver)
718716
self._daemon.network.run_from_another_thread(self._daemon.network.set_parameters(net_params))
719717

0 commit comments

Comments
 (0)