-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_adapter.py
More file actions
63 lines (49 loc) · 1.67 KB
/
client_adapter.py
File metadata and controls
63 lines (49 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import asyncio
import websockets
LOCAL_HOST = "127.0.0.1"
LOCAL_PORT = 25565
WS_URL = "wss://DEIN-RELAY-URL_HIER/client"
async def handle_connection(reader: asyncio.StreamReader,
writer: asyncio.StreamWriter):
try:
async with websockets.connect(
WS_URL,
ping_interval=None,
max_size=None
) as ws:
print("[WS] Connected to relay")
async def tcp_to_ws():
try:
while True:
data = await reader.read(4096)
await ws.send(data)
except Exception as e:
print(f"[tcp_to_ws] Error: {e}")
await ws.close()
async def ws_to_tcp():
try:
async for message in ws:
data = message
writer.write(data)
await writer.drain()
except Exception as e:
print(f"[ws_to_tcp] Error: {e}")
finally:
writer.close()
await writer.wait_closed()
await asyncio.gather(tcp_to_ws(), ws_to_tcp())
except Exception as e:
print(f"[handle_minecraft_connection] Error: {e}")
writer.close()
await writer.wait_closed()
async def main():
server = await asyncio.start_server(
handle_connection,
LOCAL_HOST,
LOCAL_PORT
)
print(f"[INFO] {LOCAL_HOST}:{LOCAL_PORT}")
async with server:
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())