From 8cbaa722b1746f77b3f32430d04123b9839490ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 15 Oct 2021 12:50:33 +0300 Subject: [PATCH] Parallerize request processing --- untls_proxy.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/untls_proxy.py b/untls_proxy.py index 1b865ab..46b93a8 100755 --- a/untls_proxy.py +++ b/untls_proxy.py @@ -122,6 +122,7 @@ import socket import ssl import sys import time +import threading def connect(host, port): try: @@ -229,6 +230,8 @@ def proxy(sock, host): # Remove headers that don't need forwarding headers = dict((key, value) for key, value in headers.items() if not key.startswith(b'proxy-')) + # TODO: connection: close + # Split url into its constituents fields = url.split(b'://', 1) if len(fields) != 2 or fields[0] not in (b'http', b'https'): @@ -373,6 +376,7 @@ def proxy(sock, host): del request_data + # TODO: Timeout # TODO: Un-https links print('', file=sys.stderr) while True: @@ -388,6 +392,16 @@ def proxy(sock, host): remote_sock.close() +class ProxyThread(threading.Thread): + def __init__(self, sock, host): + self.sock = sock + self.host = host + super().__init__() + + def run(self): + proxy(self.sock, self.host) + self.sock.close() + def listen(port): sockets = [] for res in socket.getaddrinfo(None, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): @@ -430,10 +444,8 @@ def listen(port): while True: for fd, _ in listening.poll(): - # TODO: Threads conn, (host, *_) = sock_by_fd[fd].accept() - proxy(conn, host) - conn.close() + ProxyThread(conn, host).start() if __name__ == '__main__': if len(sys.argv) != 3: