diff --git a/.gitignore b/.gitignore index d414a20..c87ba3b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ __pycache__/ venv/ state packets.txt +packets.txt.gz packets*.txt +packets*.txt.gz diff --git a/bta_proxy/dpi.py b/bta_proxy/dpi.py index 5f95561..9eb868f 100644 --- a/bta_proxy/dpi.py +++ b/bta_proxy/dpi.py @@ -4,6 +4,7 @@ import time from asyncio import get_event_loop from typing import TextIO from json import dumps +from gzip import open as open_gzip from bta_proxy.datainputstream import AsyncDataInputStream from bta_proxy.packets import * @@ -24,9 +25,11 @@ async def inspect_client(queue: Queue, addr: tuple[str, int]): last_time = time.time() - f = open("packets-%s-%d-client.txt" % addr, "w") + f = open_gzip("packets-%s-%d-client.txt.gz" % addr, "wt") get_event_loop().create_task(queue_writer(queue, stream_queue, f)) + stats: dict[int, int] = {} + try: while True: try: @@ -37,6 +40,8 @@ async def inspect_client(queue: Queue, addr: tuple[str, int]): delta = now - last_time last_time = now + stats[pkt.packet_id] = stats.get(pkt.packet_id, 0) + 1 + match pkt.packet_id: case Packet10Flying.packet_id: continue @@ -46,11 +51,14 @@ async def inspect_client(queue: Queue, addr: tuple[str, int]): continue case Packet13LookMove.packet_id: continue - case Packet255KickDisconnect.packet_id: - break case _: print(f"C {delta*1000:+8.1f}ms {pkt}") + if pkt.packet_id == Packet255KickDisconnect.packet_id: + break finally: + print("[C] Closing output file") + for pkt_id, count in sorted(stats.items(), key=lambda kv: kv[1], reverse=True): + print(f"[C] {Packet.REGISTRY[pkt_id].__name__}: {count}") f.close() @@ -60,9 +68,11 @@ async def inspect_server(queue: Queue, addr: tuple[str, int]): last_time = time.time() - f = open("packets-%s-%d-server.txt" % addr, "w") + f = open_gzip("packets-%s-%d-server.txt.gz" % addr, "wt") get_event_loop().create_task(queue_writer(queue, stream_queue, f)) + stats: dict[int, int] = {} + try: while True: try: @@ -73,11 +83,17 @@ async def inspect_server(queue: Queue, addr: tuple[str, int]): delta = now - last_time last_time = now + stats[pkt.packet_id] = stats.get(pkt.packet_id, 0) + 1 + match pkt.packet_id: + case Packet53BlockChange.packet_id: + continue case Packet50PreChunk.packet_id: continue case Packet51MapChunk.packet_id: continue + case Packet34EntityTeleport.packet_id: + continue case Packet28EntityVelocity.packet_id: continue case Packet31RelEntityMove.packet_id: @@ -88,7 +104,12 @@ async def inspect_server(queue: Queue, addr: tuple[str, int]): continue case Packet73WeatherStatus.packet_id: continue + case Packet52MultiBlockChange.packet_id: + continue case _: print(f"S {delta*1000:+8.1f}ms {pkt}") finally: + print("[S] Closing output file") + for pkt_id, count in sorted(stats.items(), key=lambda kv: kv[1], reverse=True): + print(f"[S] {Packet.REGISTRY[pkt_id].__name__}: {count}") f.close() diff --git a/bta_proxy/entitydata.py b/bta_proxy/entitydata.py index 0c6474d..889b01b 100644 --- a/bta_proxy/entitydata.py +++ b/bta_proxy/entitydata.py @@ -26,7 +26,6 @@ class EntityData: async def read_from(cls, dis: AsyncDataInputStream) -> list[DataItem]: items = [] while (data := await dis.read()) != 0x7F: - print(f"========= EntityData.read_from {data=} ({(data & 0xE0) >> 5})") item_type = DataItemType((data & 0xE0) >> 5) item_id: int = data & 0x1F match item_type: diff --git a/bta_proxy/proxy.py b/bta_proxy/proxy.py index 734b289..184fa71 100644 --- a/bta_proxy/proxy.py +++ b/bta_proxy/proxy.py @@ -1,4 +1,3 @@ -from asyncio.protocols import Protocol from asyncio.queues import Queue from asyncio import AbstractEventLoop, get_event_loop from asyncio.streams import StreamReader, StreamWriter, open_connection @@ -21,6 +20,7 @@ class BTAProxy: queue.put_nowait(packet) writer.write(packet) finally: + queue.put_nowait(None) writer.close() async def handle_client(self, cli_reader: StreamReader, cli_writer: StreamWriter):