56 lines
1.2 KiB
Python
56 lines
1.2 KiB
Python
import binascii
|
|
import itertools
|
|
|
|
import proto.packet
|
|
from colordiff import Color, Diff
|
|
|
|
d_c = Diff(Color.GREEN, Color.RED)
|
|
d_s = Diff(Color.BLUE, Color.YELLOW)
|
|
|
|
|
|
def handle3(tag: str, data: str):
|
|
if data.startswith("0c") or data.startswith("0a"):
|
|
if tag == "server":
|
|
print(f"{Color.YELLOW}{data}{Color.RESET}")
|
|
elif tag == "client":
|
|
print(f"{Color.RED}{data}{Color.RESET}")
|
|
else:
|
|
if tag == "server":
|
|
d_s.consume(data)
|
|
elif tag == "client":
|
|
d_c.consume(data)
|
|
|
|
|
|
def handle2(tag: str, data: str):
|
|
if tag == "stdin":
|
|
# print(f"[stdin] {data}")
|
|
return
|
|
|
|
d_bytes = binascii.unhexlify(data)
|
|
if d_bytes[0] not in (8, 9):
|
|
return
|
|
|
|
print(f"[{tag}] {data}")
|
|
|
|
|
|
ignore = [
|
|
proto.packet.AckPacket,
|
|
proto.packet.PingPacket,
|
|
proto.packet.FinPacket,
|
|
]
|
|
|
|
|
|
def handle(tag: str, data: str):
|
|
if tag == "stdin":
|
|
print(f"{Color.GRAY}# {data}{Color.RESET}")
|
|
return
|
|
|
|
d_bytes = binascii.unhexlify(data)
|
|
pkt = proto.packet.Parser.parse(d_bytes)
|
|
|
|
for packet_type in ignore:
|
|
if isinstance(pkt, packet_type):
|
|
return
|
|
|
|
print(f"[{Color.BLUE if tag == 'server' else Color.GREEN}{tag}{Color.RESET}] {pkt}")
|