From d7da532111364b2a7c0fc96469c1d8a72811bf75 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Thu, 8 Feb 2024 22:40:14 +0200 Subject: [PATCH] prepare: Deduplicate Monero tor config --- basicswap/basicswap.py | 8 +++++--- bin/basicswap_prepare.py | 40 +++++++++++++++++++++++++++------------- bin/basicswap_run.py | 4 ++-- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index d80d1c7..07eff82 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -513,17 +513,19 @@ class BasicSwap(BaseApp): self.coin_clients[coin]['rpcpassword'] = chain_client_settings.get('rpcpassword', '') def getXMRTrustedDaemon(self, coin, node_host: str) -> bool: + coin = Coins(coin) # Errors for invalid coin value chain_client_settings = self.getChainClientSettings(coin) trusted_daemon_setting = chain_client_settings.get('trusted_daemon', 'auto') + self.log.debug(f'\'trusted_daemon\' setting for {getCoinName(coin)}: {trusted_daemon_setting}.') if isinstance(trusted_daemon_setting, bool): return trusted_daemon_setting if trusted_daemon_setting == 'auto': return is_private_ip_address(node_host) - ci = self.ci(coin) - self.log.warning(f'Unknown \'trusted_daemon\' setting for {ci.coin_name()}: {trusted_daemon_setting}.') + self.log.warning(f'Unknown \'trusted_daemon\' setting for {getCoinName(coin)}: {trusted_daemon_setting}.') return False def getXMRWalletProxy(self, coin, node_host: str) -> (Optional[str], Optional[int]): + coin = Coins(coin) # Errors for invalid coin value chain_client_settings = self.getChainClientSettings(coin) proxy_host = None proxy_port = None @@ -550,7 +552,7 @@ class BasicSwap(BaseApp): def get_rpc_func(rpcport, daemon_login, rpchost): - proxy_host, proxy_port = self.getXMRWalletProxy(chain_client_settings, rpchost) + proxy_host, proxy_port = self.getXMRWalletProxy(coin, rpchost) if proxy_host: self.log.info(f'Connecting through proxy at {proxy_host}.') diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index f0ba3a0..4e83cf4 100755 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -202,6 +202,20 @@ WALLET_ENCRYPTION_PWD = os.getenv('WALLET_ENCRYPTION_PWD', '') use_tor_proxy: bool = False +monerod_proxy_config = [ + f'proxy={TOR_PROXY_HOST}:{TOR_PROXY_PORT}', + 'proxy-allow-dns-leaks=0', + 'no-igd=1', # Disable UPnP port mapping + 'hide-my-port=1', # Don't share the p2p port + 'p2p-bind-ip=127.0.0.1', # Don't broadcast ip + 'in-peers=0', # Changes "error" in log to "incoming connections disabled" +] + +monero_wallet_rpc_proxy_config = [ + 'daemon-ssl-allow-any-cert=1', +] + + default_socket = socket.socket default_socket_timeout = socket.getdefaulttimeout() default_socket_getaddrinfo = socket.getaddrinfo @@ -814,8 +828,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): fp.write('prune-blockchain=1\n') if tor_control_password is not None: - fp.write('proxy-allow-dns-leaks=0\n') - fp.write('no-igd=1\n') + for opt_line in monerod_proxy_config: + fp.write(opt_line + '\n') if XMR_RPC_USER != '': fp.write(f'rpc-login={XMR_RPC_USER}:{XMR_RPC_PWD}\n') @@ -846,7 +860,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}): if tor_control_password is not None: if not core_settings['manage_daemon']: - fp.write(f'proxy={TOR_PROXY_HOST}:{TOR_PROXY_PORT}\n') + for opt_line in monero_wallet_rpc_proxy_config: + fp.write(opt_line + '\n') return core_conf_path = os.path.join(data_dir, coin + '.conf') @@ -989,30 +1004,29 @@ def modify_tor_config(settings, coin, tor_control_password=None, enable=False, e shutil.copyfile(core_conf_path, core_conf_path + '.last') shutil.copyfile(wallet_conf_path, wallet_conf_path + '.last') - daemon_tor_settings = ('proxy=', 'proxy-allow-dns-leaks=', 'no-igd=') with open(core_conf_path, 'w') as fp: with open(core_conf_path + '.last') as fp_in: # Disable tor first for line in fp_in: - skip_line = False - for setting in daemon_tor_settings: + skip_line: bool = False + for opt_line in monerod_proxy_config: + setting: str = opt_line[0: opt_line.find('=') + 1] if line.startswith(setting): skip_line = True break if not skip_line: fp.write(line) if enable: - fp.write(f'proxy={TOR_PROXY_HOST}:{TOR_PROXY_PORT}\n') - fp.write('proxy-allow-dns-leaks=0\n') - fp.write('no-igd=1\n') + for opt_line in monerod_proxy_config: + fp.write(opt_line + '\n') - wallet_tor_settings = ('proxy=', 'daemon-ssl-allow-any-cert=',) with open(wallet_conf_path, 'w') as fp: with open(wallet_conf_path + '.last') as fp_in: # Disable tor first for line in fp_in: skip_line = False - for setting in wallet_tor_settings: + for opt_line in monero_wallet_rpc_proxy_config + ['proxy=',]: + setting: str = opt_line[0: opt_line.find('=') + 1] if line.startswith(setting): skip_line = True break @@ -1020,8 +1034,8 @@ def modify_tor_config(settings, coin, tor_control_password=None, enable=False, e fp.write(line) if enable: if not coin_settings['manage_daemon']: - fp.write(f'proxy={TOR_PROXY_HOST}:{TOR_PROXY_PORT}\n') - fp.write('daemon-ssl-allow-any-cert=1\n') + for opt_line in monero_wallet_rpc_proxy_config: + fp.write(opt_line + '\n') coin_settings['trusted_daemon'] = extra_opts.get('trust_remote_node', 'auto') return diff --git a/bin/basicswap_run.py b/bin/basicswap_run.py index cdbcee1..59db07d 100755 --- a/bin/basicswap_run.py +++ b/bin/basicswap_run.py @@ -176,11 +176,11 @@ def runClient(fp, data_dir, chain, start_only_coins): if v['manage_wallet_daemon'] is True: swap_client.log.info(f'Starting {display_name} wallet daemon') daemon_addr = '{}:{}'.format(v['rpchost'], v['rpcport']) - trusted_daemon: bool = swap_client.getXMRTrustedDaemon(c, v['rpchost']) + trusted_daemon: bool = swap_client.getXMRTrustedDaemon(coin_id, v['rpchost']) opts = ['--daemon-address', daemon_addr, ] proxy_log_str = '' - proxy_host, proxy_port = swap_client.getXMRWalletProxy(c, v['rpchost']) + proxy_host, proxy_port = swap_client.getXMRWalletProxy(coin_id, v['rpchost']) if proxy_host: proxy_log_str = ' through proxy' opts += ['--proxy', f'{proxy_host}:{proxy_port}', ]