Don't connect to XMR nodes at private ips over tor by default.

2024-05-20_merge
tecnovert 11 months ago
parent 5ceaab57d1
commit 14298d022a
  1. 26
      basicswap/basicswap.py
  2. 22
      basicswap/interface/xmr.py

@ -59,6 +59,7 @@ from .util.address import (
decodeAddress, decodeAddress,
pubkeyToAddress, pubkeyToAddress,
) )
from basicswap.util.network import is_private_ip_address
from .chainparams import ( from .chainparams import (
Coins, Coins,
chainparams, chainparams,
@ -521,10 +522,23 @@ class BasicSwap(BaseApp):
rpcport: int = coin_settings['rpcport'] rpcport: int = coin_settings['rpcport']
timeout: int = coin_settings['rpctimeout'] timeout: int = coin_settings['rpctimeout']
proxy_host: str = self.tor_proxy_host if self.use_tor_proxy else None def get_rpc_func(rpcport, daemon_login, rpchost):
proxy_port: int = self.tor_proxy_port if self.use_tor_proxy else None
if proxy_host: proxy_host = None
self.log.info(f'Connecting through proxy at {proxy_host}.') proxy_port = None
if self.use_tor_proxy:
have_cc_tor_opt = 'use_tor' in chain_client_settings
if have_cc_tor_opt and chain_client_settings['use_tor'] is False:
self.log.warning('use_tor is true for system but false for XMR.')
elif have_cc_tor_opt is False and is_private_ip_address(rpchost):
self.log.warning(f'Not using proxy for XMR node at private ip address {rpchost}.')
else:
proxy_host = self.tor_proxy_host
proxy_port = self.tor_proxy_port
if proxy_host:
self.log.info(f'Connecting through proxy at {proxy_host}.')
return make_xmr_rpc2_func(rpcport, daemon_login, rpchost, proxy_host=proxy_host, proxy_port=proxy_port)
daemon_login = None daemon_login = None
if coin_settings.get('rpcuser', '') != '': if coin_settings.get('rpcuser', '') != '':
@ -533,7 +547,7 @@ class BasicSwap(BaseApp):
if current_daemon_url in remote_daemon_urls: if current_daemon_url in remote_daemon_urls:
self.log.info(f'Trying last used url {rpchost}:{rpcport}.') self.log.info(f'Trying last used url {rpchost}:{rpcport}.')
try: try:
rpc2 = make_xmr_rpc2_func(rpcport, daemon_login, rpchost, proxy_host=proxy_host, proxy_port=proxy_port) rpc2 = get_rpc_func(rpcport, daemon_login, rpchost)
test = rpc2('get_height', timeout=timeout)['height'] test = rpc2('get_height', timeout=timeout)['height']
return True return True
except Exception as e: except Exception as e:
@ -543,7 +557,7 @@ class BasicSwap(BaseApp):
self.log.info(f'Trying url {url}.') self.log.info(f'Trying url {url}.')
try: try:
rpchost, rpcport = url.rsplit(':', 1) rpchost, rpcport = url.rsplit(':', 1)
rpc2 = make_xmr_rpc2_func(rpcport, daemon_login, rpchost, proxy_host=proxy_host, proxy_port=proxy_port) rpc2 = get_rpc_func(rpcport, daemon_login, rpchost)
test = rpc2('get_height', timeout=timeout)['height'] test = rpc2('get_height', timeout=timeout)['height']
coin_settings['rpchost'] = rpchost coin_settings['rpchost'] = rpchost
coin_settings['rpcport'] = rpcport coin_settings['rpcport'] = rpcport

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright (c) 2020-2023 tecnovert # Copyright (c) 2020-2024 tecnovert
# Distributed under the MIT software license, see the accompanying # Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php. # file LICENSE or http://www.opensource.org/licenses/mit-license.php.
@ -27,16 +27,16 @@ from coincurve.dleag import (
from basicswap.interface import ( from basicswap.interface import (
Curves) Curves)
from basicswap.util import ( from basicswap.util import (
i2b, i2b, b2i, b2h,
dumpj, dumpj,
ensure, ensure,
make_int, make_int,
TemporaryError) TemporaryError)
from basicswap.util.network import (
is_private_ip_address)
from basicswap.rpc_xmr import ( from basicswap.rpc_xmr import (
make_xmr_rpc_func, make_xmr_rpc_func,
make_xmr_rpc2_func) make_xmr_rpc2_func)
from basicswap.util import (
b2i, b2h)
from basicswap.chainparams import XMR_COIN, CoinInterface, Coins from basicswap.chainparams import XMR_COIN, CoinInterface, Coins
@ -102,9 +102,17 @@ class XMRInterface(CoinInterface):
manage_daemon: bool = chain_client_settings['manage_daemon'] manage_daemon: bool = chain_client_settings['manage_daemon']
if swap_client.use_tor_proxy: if swap_client.use_tor_proxy:
if manage_daemon is False: if manage_daemon is False:
proxy_host = swap_client.tor_proxy_host log_str: str = ''
proxy_port = swap_client.tor_proxy_port have_cc_tor_opt = 'use_tor' in chain_client_settings
self._log.info(f'Connecting to remote {self.coin_name()} daemon at {rpchost} through proxy at {proxy_host}.') if have_cc_tor_opt and chain_client_settings['use_tor'] is False:
log_str = ' bypassing proxy (use_tor false for XMR)'
elif have_cc_tor_opt is False and is_private_ip_address(rpchost):
log_str = ' bypassing proxy (private ip address)'
else:
proxy_host = swap_client.tor_proxy_host
proxy_port = swap_client.tor_proxy_port
log_str = f' through proxy at {proxy_host}'
self._log.info(f'Connecting to remote {self.coin_name()} daemon at {rpchost}{log_str}.')
else: else:
self._log.info(f'Not connecting to local {self.coin_name()} daemon through proxy.') self._log.info(f'Not connecting to local {self.coin_name()} daemon through proxy.')
elif manage_daemon is False: elif manage_daemon is False:

Loading…
Cancel
Save