mirror of
https://github.com/basicswap/basicswap.git
synced 2025-01-03 17:29:26 +00:00
Avoid monkeypatching PySocks
This commit is contained in:
parent
f33629f2a5
commit
724e7f0ffc
3 changed files with 141 additions and 146 deletions
|
@ -15,6 +15,8 @@ import threading
|
|||
import traceback
|
||||
import subprocess
|
||||
|
||||
from sockshandler import SocksiPyHandler
|
||||
|
||||
import basicswap.config as cfg
|
||||
|
||||
from .rpc import (
|
||||
|
@ -172,6 +174,15 @@ class BaseApp:
|
|||
socket.getaddrinfo = self.default_socket_getaddrinfo
|
||||
socket.setdefaulttimeout(self.default_socket_timeout)
|
||||
|
||||
def readURL(self, url: str, timeout: int = 120, headers=None) -> bytes:
|
||||
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()
|
||||
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:
|
||||
|
|
|
@ -16,7 +16,6 @@ import random
|
|||
import shutil
|
||||
import string
|
||||
import struct
|
||||
import urllib.request
|
||||
import hashlib
|
||||
import secrets
|
||||
import datetime as dt
|
||||
|
@ -6397,8 +6396,6 @@ class BasicSwap(BaseApp):
|
|||
ticker_from = ci_from.chainparams()['ticker']
|
||||
ticker_to = ci_to.chainparams()['ticker']
|
||||
headers = {'Connection': 'close'}
|
||||
try:
|
||||
self.setConnectionParameters()
|
||||
rv = {}
|
||||
|
||||
if rate_sources.get('coingecko.com', True):
|
||||
|
@ -6406,8 +6403,7 @@ class BasicSwap(BaseApp):
|
|||
url = 'https://api.coingecko.com/api/v3/simple/price?ids={},{}&vs_currencies=usd,btc'.format(exchange_name_from, exchange_name_to)
|
||||
self.log.debug(f'lookupRates: {url}')
|
||||
start = time.time()
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
js = json.loads(urllib.request.urlopen(req, timeout=10).read())
|
||||
js = json.loads(self.readURL(url, timeout=10, headers=headers))
|
||||
js['time_taken'] = time.time() - start
|
||||
rate = float(js[exchange_name_from]['usd']) / float(js[exchange_name_to]['usd'])
|
||||
js['rate_inferred'] = ci_to.format_amount(rate, conv_int=True, r=1)
|
||||
|
@ -6442,8 +6438,7 @@ class BasicSwap(BaseApp):
|
|||
url = f'{bittrex_api_v3}/markets/{pair}/ticker'
|
||||
self.log.debug(f'lookupRates: {url}')
|
||||
start = time.time()
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
js = json.loads(urllib.request.urlopen(req, timeout=10).read())
|
||||
js = json.loads(self.readURL(url, timeout=10, headers=headers))
|
||||
js['time_taken'] = time.time() - start
|
||||
js['pair'] = pair
|
||||
try:
|
||||
|
@ -6460,8 +6455,7 @@ class BasicSwap(BaseApp):
|
|||
url = f'{bittrex_api_v3}/markets/{pair}/ticker'
|
||||
self.log.debug(f'lookupRates: {url}')
|
||||
start = time.time()
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
js = json.loads(urllib.request.urlopen(req, timeout=10).read())
|
||||
js = json.loads(self.readURL(url, timeout=10, headers=headers))
|
||||
js['time_taken'] = time.time() - start
|
||||
js['pair'] = pair
|
||||
js['rate_last'] = js['lastTradeRate']
|
||||
|
@ -6473,8 +6467,7 @@ class BasicSwap(BaseApp):
|
|||
url = f'{bittrex_api_v3}/markets/{pair}/ticker'
|
||||
self.log.debug(f'lookupRates: {url}')
|
||||
start = time.time()
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
js_from = json.loads(urllib.request.urlopen(req, timeout=10).read())
|
||||
js_from = json.loads(self.readURL(url, timeout=10, headers=headers))
|
||||
js_from['time_taken'] = time.time() - start
|
||||
js_from['pair'] = pair
|
||||
|
||||
|
@ -6482,8 +6475,7 @@ class BasicSwap(BaseApp):
|
|||
url = f'{bittrex_api_v3}/markets/{pair}/ticker'
|
||||
self.log.debug(f'lookupRates: {url}')
|
||||
start = time.time()
|
||||
req = urllib.request.Request(url, headers=headers)
|
||||
js_to = json.loads(urllib.request.urlopen(req, timeout=10).read())
|
||||
js_to = json.loads(self.readURL(url, timeout=10, headers=headers))
|
||||
js_to['time_taken'] = time.time() - start
|
||||
js_to['pair'] = pair
|
||||
|
||||
|
@ -6543,5 +6535,3 @@ class BasicSwap(BaseApp):
|
|||
return rv_array
|
||||
|
||||
return rv
|
||||
finally:
|
||||
self.popConnectionParameters()
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2019-2022 tecnovert
|
||||
# Copyright (c) 2019-2023 tecnovert
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import json
|
||||
import urllib.request
|
||||
|
||||
|
||||
class Explorer():
|
||||
|
@ -17,12 +16,7 @@ class Explorer():
|
|||
|
||||
def readURL(self, url):
|
||||
self.log.debug('Explorer url: {}'.format(url))
|
||||
try:
|
||||
self.swapclient.setConnectionParameters()
|
||||
req = urllib.request.Request(url)
|
||||
return urllib.request.urlopen(req).read()
|
||||
finally:
|
||||
self.swapclient.popConnectionParameters()
|
||||
return self.swapclient.readURL(url)
|
||||
|
||||
|
||||
class ExplorerInsight(Explorer):
|
||||
|
|
Loading…
Reference in a new issue