Merge pull request #194 from vbanos/socksproxy

Thank you, @vbanos!
This commit is contained in:
Barbara Miller 2023-10-17 09:18:11 -07:00 committed by GitHub
commit 848c089afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View File

@ -207,6 +207,15 @@ def _build_arg_parser(prog='warcprox', show_hidden=False):
default=None, help=( default=None, help=(
'host:port of tor socks proxy, used only to connect to ' 'host:port of tor socks proxy, used only to connect to '
'.onion sites')) '.onion sites'))
arg_parser.add_argument(
'--socks-proxy', dest='socks_proxy',
default=None, help='host:port of socks proxy, used for all traffic if activated')
arg_parser.add_argument(
'--socks-proxy-username', dest='socks_proxy_username',
default=None, help='optional socks proxy username')
arg_parser.add_argument(
'--socks-proxy-password', dest='socks_proxy_password',
default=None, help='optional socks proxy password')
hidden.add_argument( hidden.add_argument(
'--socket-timeout', dest='socket_timeout', type=float, default=60, '--socket-timeout', dest='socket_timeout', type=float, default=60,
help=suppress( help=suppress(

View File

@ -256,6 +256,10 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler):
_tmp_file_max_memory_size = 512 * 1024 _tmp_file_max_memory_size = 512 * 1024
onion_tor_socks_proxy_host = None onion_tor_socks_proxy_host = None
onion_tor_socks_proxy_port = None onion_tor_socks_proxy_port = None
socks_proxy_host = None
socks_proxy_port = None
socks_proxy_username = None
socks_proxy_password = None
def __init__(self, request, client_address, server): def __init__(self, request, client_address, server):
threading.current_thread().name = 'MitmProxyHandler(tid={},started={},client={}:{})'.format(warcprox.gettid(), datetime.datetime.utcnow().isoformat(), client_address[0], client_address[1]) threading.current_thread().name = 'MitmProxyHandler(tid={},started={},client={}:{})'.format(warcprox.gettid(), datetime.datetime.utcnow().isoformat(), client_address[0], client_address[1])
@ -314,6 +318,18 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler):
port=self.onion_tor_socks_proxy_port, rdns=True) port=self.onion_tor_socks_proxy_port, rdns=True)
self._remote_server_conn.sock.settimeout(self._socket_timeout) self._remote_server_conn.sock.settimeout(self._socket_timeout)
self._remote_server_conn.sock.connect((self.hostname, int(self.port))) self._remote_server_conn.sock.connect((self.hostname, int(self.port)))
elif self.socks_proxy_host and self.socks_proxy_port:
self.logger.info(
"using socks proxy at %s:%s to connect to %s",
self.socks_proxy_host, self.socks_proxy_port, self.hostname)
self._remote_server_conn.sock = socks.socksocket()
self._remote_server_conn.sock.set_proxy(
socks.SOCKS5, addr=self.socks_proxy_host,
port=self.socks_proxy_port, rdns=True,
username=self.socks_proxy_username,
password=self.socks_proxy_password)
self._remote_server_conn.sock.settimeout(self._socket_timeout)
self._remote_server_conn.sock.connect((self.hostname, int(self.port)))
else: else:
self._remote_server_conn.connect() self._remote_server_conn.connect()
remote_ip = self._remote_server_conn.sock.getpeername()[0] remote_ip = self._remote_server_conn.sock.getpeername()[0]
@ -822,6 +838,14 @@ class SingleThreadedMitmProxy(http_server.HTTPServer):
except ValueError: except ValueError:
MitmProxyHandlerClass.onion_tor_socks_proxy_host = options.onion_tor_socks_proxy MitmProxyHandlerClass.onion_tor_socks_proxy_host = options.onion_tor_socks_proxy
MitmProxyHandlerClass.onion_tor_socks_proxy_port = None MitmProxyHandlerClass.onion_tor_socks_proxy_port = None
if options.socks_proxy:
host, port = options.socks_proxy.split(':')
MitmProxyHandlerClass.socks_proxy_host = host
MitmProxyHandlerClass.socks_proxy_port = int(port)
if options.socks_proxy_username:
MitmProxyHandlerClass.socks_proxy_username = options.socks_proxy_username
if options.socks_proxy_password:
MitmProxyHandlerClass.socks_proxy_password = options.socks_proxy_password
if options.socket_timeout: if options.socket_timeout:
MitmProxyHandlerClass._socket_timeout = options.socket_timeout MitmProxyHandlerClass._socket_timeout = options.socket_timeout