Не работает проксирование HTTPS трафика
У меня есть код:
import socket
from threading import Thread
class Proxy:
def __init__(self, host, port, request_size_limit=4096):
self.request_size_limit = request_size_limit
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind((host, port))
self.server_socket.listen()
self.accept_connections()
def accept_connections(self):
while True:
clsock, addr = self.server_socket.accept()
user = UserHandler(self.request_size_limit)
Thread(target=user.client_request, args=(clsock,)).start()
class UserHandler:
def __init__(self, request_size_limit):
self.request_size_limit = request_size_limit
def client_request(self, client_socket):
request = client_socket.recv(self.request_size_limit)
if(request.decode().split(' ')[0] == 'CONNECT'):
address = request.decode().split(' ')[1].split(':')[0]
client_socket.send(b'HTTP/1.1 200 Connection established\r\n\r\n')
request = client_socket.recv(self.request_size_limit)
self.proxy_request(address, 443, request, client_socket)
else:
url = UserHandler.get_url(request)
address, path = UserHandler.separate_url(url)
request = UserHandler.prepare_request(request, address)
self.proxy_request(address, 80, request, client_socket)
client_socket.close()
def proxy_request(self, remote_address, remote_port, request, client_socket, tls=False):
remote_connect_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_connect_socket.connect((remote_address, remote_port))
remote_connect_socket.send(request)
while True:
read = remote_connect_socket.recv(self.request_size_limit)
if len(read) == 0:
break
client_socket.send(read)
remote_connect_socket.close()
@staticmethod
def get_url(request):
return request.splitlines()[0].decode().split(' ')[1]
@staticmethod
def separate_url(url):
splitted = url.split('/')
return splitted[2], url.split(splitted[2])[1]
@staticmethod
def prepare_request(request, addr):
request_lines = request.splitlines()
request_line_split = request_lines[0].decode().split(' ')
request_lines[0] = bytes(f'{request_line_split[0]} {request_line_split[1].split(addr)[1]} {request_line_split[2]}', encoding='UTF-8')
return b'\r\n'.join(request_lines + [b''])
Proxy('0.0.0.0', 8008)
Проксирование обычного HTTP трафика работает отлично, но при попытке проксировании HTTPS браузер просто перестает загружать страницы, в чем проблема и как её исправить?