-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_adapter.py
More file actions
63 lines (51 loc) · 1.75 KB
/
server_adapter.py
File metadata and controls
63 lines (51 loc) · 1.75 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
MC_SERVER_HOST = "127.0.0.1"
MC_SERVER_PORT = 25565
WS_URL = "wss://DEIN-RELAY-URL_HIER/host"
async def run_tunnel():
reader, writer = await asyncio.open_connection(MC_SERVER_HOST, MC_SERVER_PORT)
print("[TCP] Connected to Minecraft-Server")
try:
async with websockets.connect(
WS_URL,
ping_interval=None,
max_size=None
) as ws:
print("[WS] Connected with 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 websockets.ConnectionClosed:
print("[WS] Relay connection closed")
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"[run_tunnel] Error: {e}")
writer.close()
await writer.wait_closed()
async def main():
while True:
try:
await run_tunnel()
except Exception as e:
print(f"[MAIN] Error: {e}")
print("[MAIN] Retry...")
await asyncio.sleep(5)
if __name__ == "__main__":
asyncio.run(main())