From cdfb9132adb3eaa03d3935e7e4005555a1bae4a0 Mon Sep 17 00:00:00 2001 From: gerlofvanek <gerlof@particl.io> Date: Wed, 11 Sep 2024 12:55:20 +0200 Subject: [PATCH] use readURL / fix LINTING errors. --- basicswap/base.py | 35 +++++++++++++-------------------- basicswap/basicswap.py | 2 +- basicswap/templates/offers.html | 1 + basicswap/ui/page_tor.py | 3 +++ 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/basicswap/base.py b/basicswap/base.py index 2b117eb..edda36c 100644 --- a/basicswap/base.py +++ b/basicswap/base.py @@ -179,27 +179,20 @@ class BaseApp: return False def readURL(self, url: str, timeout: int = 120, headers={}) -> bytes: - use_tor = self.is_tor_available() - try: - if use_tor: - proxy_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, self.tor_proxy_host, self.tor_proxy_port) - opener = urllib.request.build_opener(proxy_handler) - else: - opener = urllib.request.build_opener() - - opener.addheaders = [(key, value) for key, value in headers.items()] - request = urllib.request.Request(url) - - with opener.open(request, timeout=timeout) as response: - return response.read() - except urllib.error.URLError as e: - if isinstance(e.reason, ConnectionRefusedError) and use_tor: - error_msg = f"Connection refused. Tor proxy might not be running. Error: {str(e)}" - else: - error_msg = f"URLError: {str(e)}" - except Exception as e: - error_msg = f"Unexpected error: {str(e)}" - return json.dumps({"Error": error_msg}).encode() + open_handler = None + if self.use_tor_proxy: + open_handler = SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, self.tor_proxy_host, self.tor_proxy_port) + opener = urllib.request.build_opener(open_handler) if self.use_tor_proxy else urllib.request.build_opener() + if headers is None: + opener.addheaders = [('User-agent', 'Mozilla/5.0')] + request = urllib.request.Request(url, headers=headers) + return opener.open(request, timeout=timeout).read() + + def logException(self, message) -> None: + self.log.error(message) + if self.debug: + self.log.error(traceback.format_exc()) + def torControl(self, query): try: diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 77d59d9..daf4aee 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -7716,7 +7716,7 @@ class BasicSwap(BaseApp): exchange_name_to = ci_to.getExchangeName('coingecko.com') ticker_from = ci_from.chainparams()['ticker'] ticker_to = ci_to.chainparams()['ticker'] - headers = {'Connection': 'close'} + headers = {'User-Agent': 'Mozilla/5.0', 'Connection': 'close'} rv = {} if rate_sources.get('coingecko.com', True): diff --git a/basicswap/templates/offers.html b/basicswap/templates/offers.html index c0d97f1..8c08aa8 100644 --- a/basicswap/templates/offers.html +++ b/basicswap/templates/offers.html @@ -49,6 +49,7 @@ <span id="cache-status" class="mr-4 text-sm text-gray-600 dark:text-gray-300"></span> <span id="tor-status" class="mr-4 text-sm {% if tor_established %}text-green-500{% else %}text-red-500{% endif %}"> Tor <> API: {% if tor_established %}Connected{% else %}Not Connected{% endif %} + <a href="https://academy.particl.io/en/latest/basicswap-guides/basicswapguides_tor.html" target="_blank" rel="noopener noreferrer" class="underline">(?)</a> </span> <div id="tooltip-volume" role="tooltip" class="inline-block absolute invisible z-10 py-2 px-3 text-sm font-medium text-white bg-blue-500 rounded-lg shadow-sm opacity-0 transition-opacity duration-300 tooltip"> Toggle Coin Volume diff --git a/basicswap/ui/page_tor.py b/basicswap/ui/page_tor.py index ce9503b..b4ea655 100644 --- a/basicswap/ui/page_tor.py +++ b/basicswap/ui/page_tor.py @@ -3,6 +3,7 @@ # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. + def extract_data(bytes_in): if bytes_in is None: return None @@ -16,10 +17,12 @@ def extract_data(bytes_in): return None return str_in[start: end] + def get_tor_established_state(swap_client): rv = swap_client.torControl('GETINFO status/circuit-established') return extract_data(rv) + def page_tor(self, url_split, post_string): swap_client = self.server.swap_client summary = swap_client.getSummary()