diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 53e5772..88f2618 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -59,6 +59,7 @@ from .util.address import ( decodeAddress, pubkeyToAddress, ) +from basicswap.util.network import is_private_ip_address from .chainparams import ( Coins, chainparams, @@ -521,10 +522,23 @@ class BasicSwap(BaseApp): rpcport: int = coin_settings['rpcport'] timeout: int = coin_settings['rpctimeout'] - proxy_host: str = self.tor_proxy_host if self.use_tor_proxy else None - proxy_port: int = self.tor_proxy_port if self.use_tor_proxy else None - if proxy_host: - self.log.info(f'Connecting through proxy at {proxy_host}.') + def get_rpc_func(rpcport, daemon_login, rpchost): + + proxy_host = None + 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 if coin_settings.get('rpcuser', '') != '': @@ -533,7 +547,7 @@ class BasicSwap(BaseApp): if current_daemon_url in remote_daemon_urls: self.log.info(f'Trying last used url {rpchost}:{rpcport}.') 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'] return True except Exception as e: @@ -543,7 +557,7 @@ class BasicSwap(BaseApp): self.log.info(f'Trying url {url}.') try: 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'] coin_settings['rpchost'] = rpchost coin_settings['rpcport'] = rpcport diff --git a/basicswap/interface/xmr.py b/basicswap/interface/xmr.py index 99246ac..265da44 100644 --- a/basicswap/interface/xmr.py +++ b/basicswap/interface/xmr.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2020-2023 tecnovert +# Copyright (c) 2020-2024 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. @@ -27,16 +27,16 @@ from coincurve.dleag import ( from basicswap.interface import ( Curves) from basicswap.util import ( - i2b, + i2b, b2i, b2h, dumpj, ensure, make_int, TemporaryError) +from basicswap.util.network import ( + is_private_ip_address) from basicswap.rpc_xmr import ( make_xmr_rpc_func, make_xmr_rpc2_func) -from basicswap.util import ( - b2i, b2h) from basicswap.chainparams import XMR_COIN, CoinInterface, Coins @@ -102,9 +102,17 @@ class XMRInterface(CoinInterface): manage_daemon: bool = chain_client_settings['manage_daemon'] if swap_client.use_tor_proxy: if manage_daemon is False: - proxy_host = swap_client.tor_proxy_host - proxy_port = swap_client.tor_proxy_port - self._log.info(f'Connecting to remote {self.coin_name()} daemon at {rpchost} through proxy at {proxy_host}.') + log_str: str = '' + have_cc_tor_opt = 'use_tor' in chain_client_settings + 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: self._log.info(f'Not connecting to local {self.coin_name()} daemon through proxy.') elif manage_daemon is False: