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

View File

@ -3,9 +3,15 @@ proxy_hosts:
- domain: ws.local - domain: ws.local
containers: containers:
- container_name: web-socket-test - container_name: web-socket-test
- container_name: web-socket-test proxy_host: 10.201.0.128
proxy_host: localhost
proxy_port: 8010 proxy_port: 8010
proxy_timeout_seconds: 10 proxy_timeout_seconds: 10
proxy_load_seconds: 5
- domain: wp.local
containers:
- container_name: db
proxy_host: 10.201.0.128
proxy_port: 8888
proxy_timeout_seconds: 10
proxy_load_seconds: 5 proxy_load_seconds: 5

13
tests.py Normal file
View File

@ -0,0 +1,13 @@
import asyncio
import time
import websockets
async def main():
async with websockets.connect('ws://localhost:8010') as ws:
while True:
await ws.send("testsage")
server_message = ws.recv()
print(server_message)
asyncio.get_event_loop().run_until_complete(main())