bta-proxy/bta_proxy/dpi.py

116 lines
3.7 KiB
Python
Raw Normal View History

2023-08-25 21:49:10 +03:00
from asyncio.queues import Queue
2023-08-26 01:12:19 +03:00
import time
2023-08-28 01:51:35 +03:00
from asyncio import get_event_loop
from typing import TextIO
from json import dumps
2023-08-28 17:42:35 +03:00
from gzip import open as open_gzip
2023-08-25 21:49:10 +03:00
from bta_proxy.datainputstream import AsyncDataInputStream
2023-08-25 23:11:36 +03:00
from bta_proxy.packets import *
2023-08-25 21:49:10 +03:00
2023-08-28 01:51:35 +03:00
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)
2023-08-25 21:49:10 +03:00
async def inspect_client(queue: Queue, addr: tuple[str, int]):
2023-08-28 01:51:35 +03:00
stream_queue = Queue()
dis = AsyncDataInputStream(stream_queue)
2023-08-26 01:12:19 +03:00
last_time = time.time()
2023-08-26 12:33:31 +03:00
2023-08-28 17:42:35 +03:00
f = open_gzip("packets-%s-%d-client.txt.gz" % addr, "wt")
2023-08-28 01:51:35 +03:00
get_event_loop().create_task(queue_writer(queue, stream_queue, f))
2023-08-28 17:42:35 +03:00
stats: dict[int, int] = {}
try:
2023-08-26 12:33:31 +03:00
while True:
try:
pkt = await Packet.read_packet(dis)
except EOFError:
2023-08-25 23:11:36 +03:00
break
2023-08-26 12:33:31 +03:00
now = time.time()
delta = now - last_time
last_time = now
2023-08-28 17:42:35 +03:00
stats[pkt.packet_id] = stats.get(pkt.packet_id, 0) + 1
2023-08-26 12:33:31 +03:00
match pkt.packet_id:
case Packet10Flying.packet_id:
continue
case Packet11PlayerPosition.packet_id:
continue
case Packet12PlayerLook.packet_id:
continue
case Packet13LookMove.packet_id:
continue
case _:
print(f"C {delta*1000:+8.1f}ms {pkt}")
2023-08-28 17:42:35 +03:00
if pkt.packet_id == Packet255KickDisconnect.packet_id:
break
finally:
2023-08-28 17:42:35 +03:00
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}")
2023-08-28 01:51:35 +03:00
f.close()
2023-08-25 21:49:10 +03:00
async def inspect_server(queue: Queue, addr: tuple[str, int]):
2023-08-28 01:51:35 +03:00
stream_queue = Queue()
dis = AsyncDataInputStream(stream_queue)
2023-08-26 01:12:19 +03:00
last_time = time.time()
2023-08-26 12:33:31 +03:00
2023-08-28 17:42:35 +03:00
f = open_gzip("packets-%s-%d-server.txt.gz" % addr, "wt")
2023-08-28 01:51:35 +03:00
get_event_loop().create_task(queue_writer(queue, stream_queue, f))
2023-08-28 17:42:35 +03:00
stats: dict[int, int] = {}
try:
2023-08-26 12:33:31 +03:00
while True:
try:
pkt = await Packet.read_packet(dis)
except EOFError:
break
now = time.time()
delta = now - last_time
last_time = now
2023-08-28 17:42:35 +03:00
stats[pkt.packet_id] = stats.get(pkt.packet_id, 0) + 1
2023-08-26 12:33:31 +03:00
match pkt.packet_id:
2023-08-28 17:42:35 +03:00
case Packet53BlockChange.packet_id:
continue
2023-08-26 12:33:31 +03:00
case Packet50PreChunk.packet_id:
continue
case Packet51MapChunk.packet_id:
continue
2023-08-28 17:42:35 +03:00
case Packet34EntityTeleport.packet_id:
continue
2023-08-26 16:45:22 +03:00
case Packet28EntityVelocity.packet_id:
continue
2023-08-26 12:33:31 +03:00
case Packet31RelEntityMove.packet_id:
continue
case Packet32EntityLook.packet_id:
continue
case Packet33RelEntityMoveLook.packet_id:
continue
2023-08-26 16:45:22 +03:00
case Packet73WeatherStatus.packet_id:
continue
2023-08-28 17:42:35 +03:00
case Packet52MultiBlockChange.packet_id:
continue
2023-08-26 12:33:31 +03:00
case _:
print(f"S {delta*1000:+8.1f}ms {pkt}")
finally:
2023-08-28 17:42:35 +03:00
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}")
2023-08-28 01:51:35 +03:00
f.close()