Parse socks_proxy arg with urllib.parse and support more parameters

This commit is contained in:
Lee Miller 2023-06-06 06:22:00 +03:00
parent f7e16eecfe
commit c385b43a09
Signed by: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
2 changed files with 18 additions and 6 deletions

View File

@ -544,11 +544,13 @@ class SocksConnection(Connection):
import socks import socks
proxy_type = socks.PROXY_TYPES[shared.socks_proxy.scheme.upper()]
try: try:
self.s = socks.create_connection( self.s = socks.create_connection(
(self.host, self.port), 30, None, socks.PROXY_TYPE_SOCKS5, (self.host, self.port), 30, None, proxy_type,
shared.socks_proxy[0], shared.socks_proxy[1], True, shared.socks_proxy.hostname, shared.socks_proxy.port, True,
None, None, None) shared.socks_proxy.username, shared.socks_proxy.password, None)
self.status = 'connected' self.status = 'connected'
logging.debug('Established SOCKS connection to %s', peer_str) logging.debug('Established SOCKS connection to %s', peer_str)
except socket.timeout: except socket.timeout:

View File

@ -5,8 +5,10 @@ import base64
import logging import logging
import multiprocessing import multiprocessing
import os import os
import re
import signal import signal
import socket import socket
from urllib import parse
try: try:
import socks import socks
@ -118,10 +120,11 @@ def parse_arguments(): # pylint: disable=too-many-branches,too-many-statements
if args.tor: if args.tor:
shared.tor = True shared.tor = True
if not args.socks_proxy: 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: if args.socks_proxy:
addr = args.socks_proxy.split(':') if not re.match(r'^.*://', args.socks_proxy):
shared.socks_proxy = (addr[0], int(addr[1])) args.socks_proxy = '//' + args.socks_proxy
shared.socks_proxy = parse.urlparse(args.socks_proxy, scheme='socks5')
def bootstrap_from_dns(): def bootstrap_from_dns():
@ -266,6 +269,13 @@ def main():
'Error while creating data directory in: %s', 'Error while creating data directory in: %s',
shared.data_directory, exc_info=True) 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: if shared.ip_enabled and not shared.trusted_peer:
bootstrap_from_dns() bootstrap_from_dns()