From dd7d08482dc7f2363e0ac7af5eaea287625fb3a8 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Tue, 6 Jun 2023 06:22:00 +0300 Subject: [PATCH] Parse socks_proxy arg with urllib.parse and support more parameters --- minode/connection.py | 8 +++++--- minode/main.py | 16 +++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/minode/connection.py b/minode/connection.py index 8729ba3..42e1501 100644 --- a/minode/connection.py +++ b/minode/connection.py @@ -534,11 +534,13 @@ class SocksConnection(Connection): import socks + proxy_type = socks.PROXY_TYPES[shared.socks_proxy.scheme.upper()] + try: self.s = socks.create_connection( - (self.host, self.port), 30, None, socks.PROXY_TYPE_SOCKS5, - shared.socks_proxy[0], shared.socks_proxy[1], True, - None, None, None) + (self.host, self.port), 30, None, proxy_type, + shared.socks_proxy.hostname, shared.socks_proxy.port, True, + shared.socks_proxy.username, shared.socks_proxy.password, None) self.status = 'connected' logging.debug('Established SOCKS connection to %s', peer_str) except socket.timeout: diff --git a/minode/main.py b/minode/main.py index fa1faf9..bacecba 100644 --- a/minode/main.py +++ b/minode/main.py @@ -5,8 +5,10 @@ import base64 import logging import multiprocessing import os +import re import signal import socket +from urllib import parse try: import socks @@ -118,10 +120,11 @@ def parse_arguments(): # pylint: disable=too-many-branches,too-many-statements if args.tor: shared.tor = True if not args.socks_proxy: - shared.socks_proxy = ('127.0.0.1', 9050) + args.socks_proxy = '127.0.0.1:9050' if args.socks_proxy: - addr = args.socks_proxy.split(':') - shared.socks_proxy = (addr[0], int(addr[1])) + if not re.match(r'^.*://', args.socks_proxy): + args.socks_proxy = '//' + args.socks_proxy + shared.socks_proxy = parse.urlparse(args.socks_proxy, scheme='socks5') def bootstrap_from_dns(): @@ -262,6 +265,13 @@ def main(): 'Error while creating data directory in: %s', shared.data_directory, exc_info=True) + if shared.socks_proxy: + try: + socks.PROXY_TYPES[shared.socks_proxy.scheme.upper()] + except KeyError: + logging.error('Unsupported proxy schema!') + return + if shared.ip_enabled and not shared.trusted_peer: bootstrap_from_dns()