From 89e590b968e27459af18bed6aab8057b94b7bbb6 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 17 Mar 2021 15:03:18 +0100 Subject: [PATCH] Move the "unknown constructor found" logging logic --- pyrogram/crypto/mtproto.py | 11 ++++++++++- pyrogram/raw/core/tl_object.py | 11 +---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pyrogram/crypto/mtproto.py b/pyrogram/crypto/mtproto.py index b67b49f1..bbe9d27a 100644 --- a/pyrogram/crypto/mtproto.py +++ b/pyrogram/crypto/mtproto.py @@ -60,7 +60,16 @@ def unpack(b: BytesIO, session_id: bytes, auth_key: bytes, auth_key_id: bytes) - # https://core.telegram.org/mtproto/security_guidelines#checking-session-id assert data.read(8) == session_id - message = Message.read(data) + try: + message = Message.read(data) + except KeyError as e: + left = data.read().hex() + + left = [left[i:i + 64] for i in range(0, len(left), 64)] + left = [[left[i:i + 8] for i in range(0, len(left), 8)] for left in left] + left = "\n".join(" ".join(x for x in left) for left in left) + + raise ValueError(f"Unknown constructor found: {hex(e.args[0])}\n{left}") # https://core.telegram.org/mtproto/security_guidelines#checking-sha256-hash-value-of-msg-key # https://core.telegram.org/mtproto/security_guidelines#checking-message-length diff --git a/pyrogram/raw/core/tl_object.py b/pyrogram/raw/core/tl_object.py index db1d3bfc..3b87d72c 100644 --- a/pyrogram/raw/core/tl_object.py +++ b/pyrogram/raw/core/tl_object.py @@ -30,16 +30,7 @@ class TLObject: @classmethod def read(cls, data: BytesIO, *args: Any) -> Any: - try: - return cast(TLObject, objects[int.from_bytes(data.read(4), "little")]).read(data, *args) - except KeyError as e: - left = data.read().hex() - - left = [left[i:i + 64] for i in range(0, len(left), 64)] - left = [[left[i:i + 8] for i in range(0, len(left), 8)] for left in left] - left = "\n".join(" ".join(x for x in left) for left in left) - - raise ValueError(f"Unknown constructor found: {hex(e.args[0])}\n{left}") + return cast(TLObject, objects[int.from_bytes(data.read(4), "little")]).read(data, *args) def write(self, *args: Any) -> bytes: pass