Additional Tryes

This commit is contained in:
JonatanRek
2025-02-26 09:41:44 +01:00
parent 30df8f46d1
commit 500cc003fe
3 changed files with 33 additions and 4 deletions

12
app.py
View File

@@ -11,6 +11,8 @@ import asyncio
import websockets
import hashlib
import base64
from websockets.server import WebSocketServerProtocol
# Define the target server to proxy requests to
class ProxyHandler(http.server.BaseHTTPRequestHandler):
def __init__(self, configuration, docker_client):
@@ -138,21 +140,24 @@ class ProxyHandler(http.server.BaseHTTPRequestHandler):
server_ws = None
try:
client_connection = self.connection
# Establish server connection to backend
server_ws = await websockets.connect(f"ws://{target_host}:{target_port}")
print("connected")
# Bridge function to handle message forwarding
async def bridge_websockets():
try:
while True:
# Wait for a message from the client
client_message = await client_connection.recv()
print(f">: {client_message}")
# Send it to the server
await server_ws.send(client_message)
# Wait for a message from the server
server_message = await server_ws.recv()
# Send it to the client
await client_connection.send(server_message)
except websockets.exceptions.ConnectionClosed as e:
print(f"WebSocket connection closed: {e}")
except Exception as e:
@@ -181,6 +186,11 @@ class ProxyHandler(http.server.BaseHTTPRequestHandler):
self.send_header("Sec-WebSocket-Accept", accept_val)
self.end_headers()
# Upgrade the connection to a WebSocket connection
self.websocket = WebSocketServerProtocol()
self.websocket.connection_made(self.connection)
self.websocket.connection_open()
loop = asyncio.new_event_loop()
threading.Thread(target=loop.run_until_complete, args=(self.websocket_proxy(target_host, target_port),)).start()