Untitled
unknown
plain_text
2 years ago
1.8 kB
7
Indexable
def connect_tunnel(self): try: # Connect to the external proxy remote_socket = socket.create_connection((PROXY_HOST, PROXY_PORT)) # Send the CONNECT request with Proxy-Authorization headers to the external proxy connect_request = f"CONNECT {self.path} HTTP/1.1\r\nHost: {self.path}\r\nProxy-Authorization: Basic {base64.b64encode(f'{PROXY_USERNAME}:{PROXY_PASSWORD}'.encode()).decode()}\r\n\r\n" remote_socket.sendall(connect_request.encode()) # Receive the response from the proxy response = remote_socket.recv(8192) # Check if the connection was successful if b"200 OK" in response: self.send_response(200, 'Connection established') self.end_headers() else: print("Failed to establish a connection through the external proxy.") self.send_response(502, 'Bad Gateway') self.end_headers() return # Relay between the client and the proxy self._relay(remote_socket) except Exception as e: print(e) finally: if 'remote_socket' in locals(): remote_socket.close() def _relay(self, remote_socket): connection = self.connection while True: rlist, _, _ = select.select([connection, remote_socket], [], []) if connection in rlist: data = connection.recv(8192) if not data: break remote_socket.sendall(data) if remote_socket in rlist: data = remote_socket.recv(8192) if not data: break connection.sendall(data)
Editor is loading...