Avoid monkeypatching PySocks

This commit is contained in:
tecnovert
2023-02-26 22:42:44 +02:00
parent f33629f2a5
commit 724e7f0ffc
3 changed files with 141 additions and 146 deletions

View File

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