use readURL / fix LINTING errors.

This commit is contained in:
gerlofvanek 2024-09-11 12:55:20 +02:00
parent b6d29a33d2
commit cdfb9132ad
4 changed files with 19 additions and 22 deletions

View file

@ -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:

View file

@ -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):

View file

@ -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

View file

@ -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()