import hashlib import struct import unittest from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from proxy.tg_ws_proxy import ( PROTO_TAG_ABRIDGED, PROTO_TAG_INTERMEDIATE, _generate_relay_init, _try_handshake, ) KEY = bytes(range(32)) IV = bytes(range(16)) SECRET = bytes.fromhex("0123456789abcdef0123456789abcdef") def _xor(left: bytes, right: bytes) -> bytes: return bytes(a ^ b for a, b in zip(left, right)) def _build_client_handshake(dc_raw: int, proto_tag: bytes) -> bytes: packet = bytearray(64) packet[8:40] = KEY packet[40:56] = IV dec_key = hashlib.sha256(KEY + SECRET).digest() decryptor = Cipher(algorithms.AES(dec_key), modes.CTR(IV)).encryptor() keystream = decryptor.update(b"\x00" * 64) plain_tail = proto_tag + struct.pack("