import struct from .crypt import add_v5_server_checkcode, decrypt_v4, decrypt_v5, v3_header_checkcode from .packets import Command, Version class Header: # what every version's client packet header comes down to (the fields a version doesn't have stay 0) __slots__ = ('version', 'command', 'seq1', 'seq2', 'uin', 'session_id') version: int command: int seq1: int seq2: int uin: int session_id: int def __init__(self, version: int, command: int, seq1: int, seq2: int, uin: int, session_id: int = 0) -> None: self.version = version self.command = command self.seq1 = seq1 self.seq2 = seq2 self.uin = uin self.session_id = session_id def __repr__(self) -> str: return ''.format(self.version, self.command, self.seq1, self.seq2, self.uin) def version_of(packet: bytes) -> int | None: if len(packet) < 2: return None return struct.unpack_from(' tuple[Header, bytes] | None: # client packet -> (header, data); None for anything too short to be one version = version_of(packet) if version == Version.V5: # u16 version, u32 zero, u32 UIN, then (encrypted) u32 session ID, u16 command, u16 seq1, u16 seq2, u32 checkcode if len(packet) < 24: return None plain = decrypt_v5(packet) uin, session_id, command, seq1, seq2 = struct.unpack_from(' bytes: # server packet for a client of `version` if version == Version.V5: # u16 version, u8 zero, u32 session ID, u16 command, u16 seq1, u16 seq2, u32 UIN, u32 checkcode; not encrypted packet = bytearray(struct.pack('