2024-08-27 19:44:15 +00:00
|
|
|
import asyncio
|
|
|
|
import socket
|
|
|
|
import websockets
|
|
|
|
import hashlib
|
|
|
|
|
2024-08-27 20:26:35 +00:00
|
|
|
import conn
|
|
|
|
|
2024-08-27 19:44:15 +00:00
|
|
|
HOST = '192.168.21.30'
|
|
|
|
PORT = 6443
|
|
|
|
WEBSOCKET_URL = 'ws://localhost:9999/data'
|
|
|
|
|
|
|
|
async def handle_client(tcpreader, tcpwriter, ws):
|
2024-08-27 20:26:35 +00:00
|
|
|
sockets = {}
|
|
|
|
|
2024-08-27 19:44:15 +00:00
|
|
|
print(f"New client connected: {tcpreader} {tcpwriter}")
|
|
|
|
try:
|
|
|
|
# Forwarding data from client to WebSocket
|
|
|
|
async def tcp_to_websocket():
|
|
|
|
print("tcp_to_websocket...")
|
|
|
|
while True:
|
|
|
|
data = await tcpreader.read(2024)
|
2024-08-27 20:26:35 +00:00
|
|
|
c = conn.Conn(0, data)
|
|
|
|
socketid = c.socketid
|
|
|
|
print(f'TCP@{socketid}>WS: ', hashlib.md5(data).hexdigest())
|
2024-08-27 19:44:15 +00:00
|
|
|
if not data:
|
|
|
|
break
|
2024-08-27 20:26:35 +00:00
|
|
|
await ws.send(c.to_ws_bytes())
|
2024-08-27 19:44:15 +00:00
|
|
|
|
|
|
|
# Forwarding data from WebSocket to client
|
|
|
|
async def websocket_to_tcp():
|
|
|
|
print("websocket_to_tcp...")
|
|
|
|
while True:
|
|
|
|
message = await ws.recv()
|
2024-08-27 20:26:35 +00:00
|
|
|
c = conn.Conn.from_ws_bytes(message)
|
|
|
|
socketid = c.socketid
|
|
|
|
print(f'WS>TCP@{socketid}: ', hashlib.md5(message).hexdigest())
|
|
|
|
tcpwriter.write(c.data)
|
2024-08-27 19:44:15 +00:00
|
|
|
await tcpwriter.drain()
|
|
|
|
|
|
|
|
print("Running both tasks concurrently...")
|
|
|
|
# Run both tasks concurrently
|
|
|
|
await asyncio.gather(tcp_to_websocket(), websocket_to_tcp())
|
|
|
|
print('done')
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
print(f"ASYNCIOD Error: {e}")
|
|
|
|
|
|
|
|
|
2024-08-27 20:42:31 +00:00
|
|
|
async def handle_socket_read(socketid, tcpreader, ws):
|
|
|
|
try:
|
|
|
|
print(f"New socket: {socketid}. Waiting on recv")
|
|
|
|
print(tcpreader)
|
|
|
|
while True:
|
|
|
|
data = await tcpreader.read(2024)
|
|
|
|
print('GOT DATA', data)
|
|
|
|
if data == b'':
|
|
|
|
print(f"Connection closed: {socketid}")
|
|
|
|
break
|
|
|
|
|
|
|
|
print(f'TCP@{socketid}>WS: RAW', data)
|
|
|
|
c = conn.Conn(socketid, data)
|
|
|
|
print(f'TCP@{socketid}>WS: ', hashlib.md5(data).hexdigest())
|
|
|
|
await ws.send(c.to_ws_bytes())
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Error: {e}")
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
async def handle_ws_incoming(ws, sockets):
|
|
|
|
data = await ws.recv()
|
|
|
|
|
|
|
|
c = conn.Conn.from_ws_bytes(data)
|
|
|
|
socketid = c.socketid
|
|
|
|
|
|
|
|
if socketid not in sockets:
|
|
|
|
print(f"New socket: {socketid}")
|
|
|
|
tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT)
|
|
|
|
sockets[socketid] = (tcpreader, tcpwriter)
|
|
|
|
print(f'TCPR: {tcpreader}')
|
|
|
|
asyncio.create_task(handle_socket_read(socketid, tcpreader, ws))
|
|
|
|
else:
|
|
|
|
tcpreader, tcpwriter = sockets[socketid]
|
2024-08-27 19:44:15 +00:00
|
|
|
|
2024-08-27 20:42:31 +00:00
|
|
|
print(f'WS@{socketid}>TCP: ', hashlib.md5(data).hexdigest())
|
|
|
|
print(tcpwriter)
|
|
|
|
|
|
|
|
tcpwriter.write(c.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2024-08-27 19:44:15 +00:00
|
|
|
tcpreader, tcpwriter = await asyncio.open_connection(HOST, PORT)
|
|
|
|
|
|
|
|
ws = await websockets.connect(WEBSOCKET_URL)
|
|
|
|
|
2024-08-27 20:42:31 +00:00
|
|
|
|
|
|
|
sockets = {}
|
|
|
|
while True:
|
|
|
|
await handle_ws_incoming(ws, sockets)
|
|
|
|
|
|
|
|
|
|
|
|
#await handle_client(tcpreader, tcpwriter, ws)
|
2024-08-27 19:44:15 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|
|
|
|
|
|
|
|
|
|
|
|
|