From 9743d1eefd2736c3116de749b0e42b4557d91803 Mon Sep 17 00:00:00 2001 From: ConfusedCharacter Date: Fri, 16 Feb 2024 12:13:04 +0330 Subject: [PATCH] add custom secret support --- telethon/network/connection/tcpmtproxy.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/telethon/network/connection/tcpmtproxy.py b/telethon/network/connection/tcpmtproxy.py index 69a43bce..82fe859d 100644 --- a/telethon/network/connection/tcpmtproxy.py +++ b/telethon/network/connection/tcpmtproxy.py @@ -1,5 +1,7 @@ +import binascii import asyncio import hashlib +import base64 import os from .connection import ObfuscatedConnection @@ -98,7 +100,7 @@ class TcpMTProxy(ObfuscatedConnection): def __init__(self, ip, port, dc_id, *, loggers, proxy=None, local_addr=None): # connect to proxy's host and port instead of telegram's ones proxy_host, proxy_port = self.address_info(proxy) - self._secret = bytes.fromhex(proxy[2]) + self._secret = TcpMTProxy.normilize_secret(proxy[2]) super().__init__( proxy_host, proxy_port, dc_id, loggers=loggers) @@ -130,6 +132,21 @@ class TcpMTProxy(ObfuscatedConnection): raise ValueError("No proxy info specified for MTProxy connection") return proxy_info[:2] + @staticmethod + def normilize_secret(secret): + if secret[:2] in ["ee","dd"]: # Remove extra bytes + secret = secret[2:] + + try: + secret_bytes = bytes.fromhex(secret) + except: + try: + secret_bytes = base64.b64decode(secret.encode()) + except binascii.Error: + secret = secret + "==" + secret_bytes = base64.b64decode(secret.encode()) + + return secret_bytes[:16] # Remove the domain from the secret (until domain support is added) class ConnectionTcpMTProxyAbridged(TcpMTProxy): """ @@ -149,4 +166,4 @@ class ConnectionTcpMTProxyRandomizedIntermediate(TcpMTProxy): """ Connect to proxy using randomized intermediate protocol (dd-secrets) """ - packet_codec = RandomizedIntermediatePacketCodec + packet_codec = RandomizedIntermediatePacketCodec \ No newline at end of file