|
@@ -4,6 +4,7 @@ import asyncio
|
|
|
import contextlib
|
|
import contextlib
|
|
|
from dataclasses import dataclass, field
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
|
|
|
|
+from .logging_utils import log_print as print
|
|
|
from .protocol import AUTH, PING, PONG, STATUS_ERR, STATUS_OK, UDP_RECV, UDP_SEND, Frame, decode_json, encode_json, read_frame, write_frame
|
|
from .protocol import AUTH, PING, PONG, STATUS_ERR, STATUS_OK, UDP_RECV, UDP_SEND, Frame, decode_json, encode_json, read_frame, write_frame
|
|
|
|
|
|
|
|
|
|
|
|
@@ -145,10 +146,19 @@ class UdpRelayServer:
|
|
|
async def start(self, host: str, port: int) -> None:
|
|
async def start(self, host: str, port: int) -> None:
|
|
|
async def accept(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
|
async def accept(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
|
|
try:
|
|
try:
|
|
|
|
|
+ peer = writer.get_extra_info("peername")
|
|
|
|
|
+ if peer:
|
|
|
|
|
+ print(f"[relay] udp client connected peer={peer[0]}:{peer[1]}")
|
|
|
await UdpRelayChannel(reader, writer, self.token).run()
|
|
await UdpRelayChannel(reader, writer, self.token).run()
|
|
|
except (asyncio.IncompleteReadError, ConnectionResetError, BrokenPipeError, OSError):
|
|
except (asyncio.IncompleteReadError, ConnectionResetError, BrokenPipeError, OSError):
|
|
|
pass
|
|
pass
|
|
|
|
|
+ finally:
|
|
|
|
|
+ with contextlib.suppress(Exception):
|
|
|
|
|
+ peer = writer.get_extra_info("peername")
|
|
|
|
|
+ if peer:
|
|
|
|
|
+ print(f"[relay] udp client closed peer={peer[0]}:{peer[1]}")
|
|
|
|
|
|
|
|
|
|
+ print(f"[relay] udp server listening on {host}:{port}")
|
|
|
server = await asyncio.start_server(accept, host, port)
|
|
server = await asyncio.start_server(accept, host, port)
|
|
|
async with server:
|
|
async with server:
|
|
|
await server.serve_forever()
|
|
await server.serve_forever()
|