import logging
import struct
from ..network.enums import types as TYPE
from .. import glob
logger = logging.getLogger(__name__)
def handle(client: "Client", data: bytes) -> bytes:
challenge = (client.ip_uint ^ glob.challenge_short) & TYPE.MAX_USHORT
challenge_solution = challenge ^ 0x6969 # 0x6969 being client_version?
challenge_answer = struct.unpack_from(b">H", data, 1)[0]
if challenge_answer != challenge_solution:
return struct.pack(b">BH", 0x1A, challenge)
client.state = 1 # CHALLENGE_PASSED
return b"\x19\x00" # 0x19: Valid_challenge; leading 0x00 byte due to some routers shitting itself if only 1 byte is sent/received