prepare: Deduplicate Monero tor config

2024-05-20_merge
tecnovert 11 months ago
parent fa35102794
commit d7da532111
  1. 8
      basicswap/basicswap.py
  2. 40
      bin/basicswap_prepare.py
  3. 4
      bin/basicswap_run.py

@ -513,17 +513,19 @@ class BasicSwap(BaseApp):
self.coin_clients[coin]['rpcpassword'] = chain_client_settings.get('rpcpassword', '') self.coin_clients[coin]['rpcpassword'] = chain_client_settings.get('rpcpassword', '')
def getXMRTrustedDaemon(self, coin, node_host: str) -> bool: def getXMRTrustedDaemon(self, coin, node_host: str) -> bool:
coin = Coins(coin) # Errors for invalid coin value
chain_client_settings = self.getChainClientSettings(coin) chain_client_settings = self.getChainClientSettings(coin)
trusted_daemon_setting = chain_client_settings.get('trusted_daemon', 'auto') 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): if isinstance(trusted_daemon_setting, bool):
return trusted_daemon_setting return trusted_daemon_setting
if trusted_daemon_setting == 'auto': if trusted_daemon_setting == 'auto':
return is_private_ip_address(node_host) return is_private_ip_address(node_host)
ci = self.ci(coin) self.log.warning(f'Unknown \'trusted_daemon\' setting for {getCoinName(coin)}: {trusted_daemon_setting}.')
self.log.warning(f'Unknown \'trusted_daemon\' setting for {ci.coin_name()}: {trusted_daemon_setting}.')
return False return False
def getXMRWalletProxy(self, coin, node_host: str) -> (Optional[str], Optional[int]): 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) chain_client_settings = self.getChainClientSettings(coin)
proxy_host = None proxy_host = None
proxy_port = None proxy_port = None
@ -550,7 +552,7 @@ class BasicSwap(BaseApp):
def get_rpc_func(rpcport, daemon_login, rpchost): 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: if proxy_host:
self.log.info(f'Connecting through proxy at {proxy_host}.') self.log.info(f'Connecting through proxy at {proxy_host}.')

@ -202,6 +202,20 @@ WALLET_ENCRYPTION_PWD = os.getenv('WALLET_ENCRYPTION_PWD', '')
use_tor_proxy: bool = False 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 = socket.socket
default_socket_timeout = socket.getdefaulttimeout() default_socket_timeout = socket.getdefaulttimeout()
default_socket_getaddrinfo = socket.getaddrinfo default_socket_getaddrinfo = socket.getaddrinfo
@ -814,8 +828,8 @@ def prepareDataDir(coin, settings, chain, particl_mnemonic, extra_opts={}):
fp.write('prune-blockchain=1\n') fp.write('prune-blockchain=1\n')
if tor_control_password is not None: if tor_control_password is not None:
fp.write('proxy-allow-dns-leaks=0\n') for opt_line in monerod_proxy_config:
fp.write('no-igd=1\n') fp.write(opt_line + '\n')
if XMR_RPC_USER != '': if XMR_RPC_USER != '':
fp.write(f'rpc-login={XMR_RPC_USER}:{XMR_RPC_PWD}\n') 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 tor_control_password is not None:
if not core_settings['manage_daemon']: 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 return
core_conf_path = os.path.join(data_dir, coin + '.conf') 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(core_conf_path, core_conf_path + '.last')
shutil.copyfile(wallet_conf_path, wallet_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, 'w') as fp:
with open(core_conf_path + '.last') as fp_in: with open(core_conf_path + '.last') as fp_in:
# Disable tor first # Disable tor first
for line in fp_in: for line in fp_in:
skip_line = False skip_line: bool = False
for setting in daemon_tor_settings: for opt_line in monerod_proxy_config:
setting: str = opt_line[0: opt_line.find('=') + 1]
if line.startswith(setting): if line.startswith(setting):
skip_line = True skip_line = True
break break
if not skip_line: if not skip_line:
fp.write(line) fp.write(line)
if enable: if enable:
fp.write(f'proxy={TOR_PROXY_HOST}:{TOR_PROXY_PORT}\n') for opt_line in monerod_proxy_config:
fp.write('proxy-allow-dns-leaks=0\n') fp.write(opt_line + '\n')
fp.write('no-igd=1\n')
wallet_tor_settings = ('proxy=', 'daemon-ssl-allow-any-cert=',)
with open(wallet_conf_path, 'w') as fp: with open(wallet_conf_path, 'w') as fp:
with open(wallet_conf_path + '.last') as fp_in: with open(wallet_conf_path + '.last') as fp_in:
# Disable tor first # Disable tor first
for line in fp_in: for line in fp_in:
skip_line = False 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): if line.startswith(setting):
skip_line = True skip_line = True
break break
@ -1020,8 +1034,8 @@ def modify_tor_config(settings, coin, tor_control_password=None, enable=False, e
fp.write(line) fp.write(line)
if enable: if enable:
if not coin_settings['manage_daemon']: if not coin_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('daemon-ssl-allow-any-cert=1\n') fp.write(opt_line + '\n')
coin_settings['trusted_daemon'] = extra_opts.get('trust_remote_node', 'auto') coin_settings['trusted_daemon'] = extra_opts.get('trust_remote_node', 'auto')
return return

@ -176,11 +176,11 @@ def runClient(fp, data_dir, chain, start_only_coins):
if v['manage_wallet_daemon'] is True: if v['manage_wallet_daemon'] is True:
swap_client.log.info(f'Starting {display_name} wallet daemon') swap_client.log.info(f'Starting {display_name} wallet daemon')
daemon_addr = '{}:{}'.format(v['rpchost'], v['rpcport']) 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, ] opts = ['--daemon-address', daemon_addr, ]
proxy_log_str = '' 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: if proxy_host:
proxy_log_str = ' through proxy' proxy_log_str = ' through proxy'
opts += ['--proxy', f'{proxy_host}:{proxy_port}', ] opts += ['--proxy', f'{proxy_host}:{proxy_port}', ]

Loading…
Cancel
Save