From dad34f61ec4a88c0a842173d32fa271f31931e6e Mon Sep 17 00:00:00 2001 From: hkc Date: Mon, 28 Aug 2023 01:51:35 +0300 Subject: [PATCH] I'm eepy sorry no commit message 4u --- bta_proxy/dpi.py | 42 +++++++++++++-------- bta_proxy/itemstack.py | 3 ++ bta_proxy/packets/__init__.py | 1 + bta_proxy/packets/packet102windowclick.py | 2 +- bta_proxy/packets/packet5playerinventory.py | 8 ++-- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/bta_proxy/dpi.py b/bta_proxy/dpi.py index 3a7cdae..5f95561 100644 --- a/bta_proxy/dpi.py +++ b/bta_proxy/dpi.py @@ -1,16 +1,31 @@ from asyncio.queues import Queue import time +from asyncio import get_event_loop +from typing import TextIO +from json import dumps from bta_proxy.datainputstream import AsyncDataInputStream from bta_proxy.packets import * +async def queue_writer(queue_in: Queue, queue_out: Queue, fp: TextIO): + packet: bytes + while (packet := await queue_in.get()) != b'': + fp.write(dumps({ + "t": time.time(), + "s": len(packet), + "b": packet.hex() + }) + "\n") + queue_out.put_nowait(packet) + async def inspect_client(queue: Queue, addr: tuple[str, int]): - dis = AsyncDataInputStream(queue) + stream_queue = Queue() + dis = AsyncDataInputStream(stream_queue) + last_time = time.time() - # f = open("packets-%s-%d-client.txt" % addr, "w") - f = None + f = open("packets-%s-%d-client.txt" % addr, "w") + get_event_loop().create_task(queue_writer(queue, stream_queue, f)) try: while True: @@ -22,9 +37,6 @@ async def inspect_client(queue: Queue, addr: tuple[str, int]): delta = now - last_time last_time = now - if f: - print(f"{delta*1000:+8.1f}ms {pkt}", file=f) - match pkt.packet_id: case Packet10Flying.packet_id: continue @@ -39,15 +51,17 @@ async def inspect_client(queue: Queue, addr: tuple[str, int]): case _: print(f"C {delta*1000:+8.1f}ms {pkt}") finally: - if f: - f.close() + f.close() + async def inspect_server(queue: Queue, addr: tuple[str, int]): - dis = AsyncDataInputStream(queue) + stream_queue = Queue() + dis = AsyncDataInputStream(stream_queue) + last_time = time.time() - # f = open("packets-%s-%d-server.txt" % addr, "w") - f = None + f = open("packets-%s-%d-server.txt" % addr, "w") + get_event_loop().create_task(queue_writer(queue, stream_queue, f)) try: while True: @@ -59,9 +73,6 @@ async def inspect_server(queue: Queue, addr: tuple[str, int]): delta = now - last_time last_time = now - if f: - print(f"{delta*1000:+8.1f}ms {pkt}", file=f) - match pkt.packet_id: case Packet50PreChunk.packet_id: continue @@ -80,5 +91,4 @@ async def inspect_server(queue: Queue, addr: tuple[str, int]): case _: print(f"S {delta*1000:+8.1f}ms {pkt}") finally: - if f: - f.close() + f.close() diff --git a/bta_proxy/itemstack.py b/bta_proxy/itemstack.py index 209762b..1ed611a 100644 --- a/bta_proxy/itemstack.py +++ b/bta_proxy/itemstack.py @@ -25,6 +25,9 @@ class ItemStack: data = stream.read_ushort() return cls(item_id, count, data) + def __bool__(self): + return self.item_id > 0 and self.count > 0 + def __repr__(self): if self.tag: return f'' diff --git a/bta_proxy/packets/__init__.py b/bta_proxy/packets/__init__.py index 6f39f2d..1f6580a 100644 --- a/bta_proxy/packets/__init__.py +++ b/bta_proxy/packets/__init__.py @@ -79,3 +79,4 @@ from .packet142openflagwindow import Packet142OpenFlagWindow from .packet108sethotbaroffset import Packet108SetHotbarOffset from .packet5playerinventory import Packet5PlayerInventory from .packet5playerinventory import Packet5PlayerInventory +from .packet5playerinventory import Packet5PlayerInventory diff --git a/bta_proxy/packets/packet102windowclick.py b/bta_proxy/packets/packet102windowclick.py index 082d7f5..d154ad2 100644 --- a/bta_proxy/packets/packet102windowclick.py +++ b/bta_proxy/packets/packet102windowclick.py @@ -5,7 +5,7 @@ class Packet102WindowClick(Packet, packet_id=102): ('window_id', 'byte'), ('action', 'byte'), ('size', 'byte'), - ('args', ('bytes', 'size')), + ('args', ('list', 'size', 'ubyte')), ('action_id', 'short'), ('item', 'itemstack_optional') ] diff --git a/bta_proxy/packets/packet5playerinventory.py b/bta_proxy/packets/packet5playerinventory.py index 0f6771a..6513dad 100644 --- a/bta_proxy/packets/packet5playerinventory.py +++ b/bta_proxy/packets/packet5playerinventory.py @@ -1,9 +1,11 @@ from .base import Packet class Packet5PlayerInventory(Packet, packet_id=5): - __slots__ = ('entity_id', 'item', 'data') + __slots__ = ('entity_id', 'slot', 'item', 'data') FIELDS = [ ('entity_id', 'int'), - ('item', 'itemstack'), - ('data', 'nbt'), + ('slot', 'short'), + ('item', 'itemstack_optional'), + ('size?item', 'short'), + ('data?item', ('bytes', 'size')) ]