Last active
May 4, 2023 11:20
-
-
Save divyam234/127aae273a424f1e41c77eeae99503bf to your computer and use it in GitHub Desktop.
Telethon , Pyrogram and GramJS string sessions convertors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
import base64 | |
import ipaddress | |
dc_maps = { | |
1: {'serverAddress': 'pluto.web.telegram.org', 'ipAddress': '149.154.175.53'}, | |
2: {'serverAddress': 'venus.web.telegram.org', 'ipAddress': '149.154.167.51'}, | |
3: {'serverAddress': 'aurora.web.telegram.org', 'ipAddress': '149.154.175.100'}, | |
4: {'serverAddress': 'vesta.web.telegram.org', 'ipAddress': '149.154.167.91'}, | |
5: {'serverAddress': 'flora.web.telegram.org', 'ipAddress': '91.108.56.130'} | |
} | |
def base64_original_length(session): | |
padding = len( | |
list(filter(lambda c: c == '\n' or c == '\r' or c == '=', session))) | |
return int((3 * (len(session) / 4)) - padding) | |
def unpack_pyrogram_session(session_string): | |
dc_id, api_id, test_mode, auth_key, user_id, is_bot = struct.unpack( | |
">BI?256sQ?", | |
base64.urlsafe_b64decode(session_string + "=" * (-len(session_string) % 4))) | |
return dc_id, auth_key | |
def unpack_gramjs_session(session_string, web=True): | |
session_string = session_string[1:] | |
if web: | |
server_length = base64_original_length(session_string) - 261 | |
dc_id, server_address_length, server_address, port, auth_key = struct.unpack( | |
">BH{}sH256s".format(server_length), | |
base64.urlsafe_b64decode(session_string) | |
) | |
else: | |
# Same as Telethon | |
server_length = 4 if len(session_string) == 352 else 16 | |
dc_id, server_address, port, auth_key = struct.unpack( | |
">B{}sH256s".format(server_length), | |
base64.urlsafe_b64decode(session_string) | |
) | |
return dc_id, server_address, port, auth_key | |
def unpack_telethon_session(session_string): | |
return unpack_gramjs_session(session_string, False) | |
def gramjs_to_pyrogram(gramjs_session, id, bot, api_id, test_mode=False, web=True): | |
dc_id, _, _, auth_key = unpack_gramjs_session(gramjs_session, web) | |
packed = struct.pack( | |
">BI?256sQ?", | |
dc_id, | |
api_id, | |
test_mode, | |
auth_key, | |
id, | |
bot | |
) | |
return base64.urlsafe_b64encode(packed).decode().rstrip("=") | |
def telethon_to_pyrogram(telethon_session, tg_id, bot, api_id, test_mode=False): | |
dc_id, _, _, auth_key = unpack_telethon_session(telethon_session) | |
packed = struct.pack( | |
">BI?256sQ?", | |
dc_id, | |
api_id, | |
test_mode, | |
auth_key, | |
tg_id, | |
bot | |
) | |
return base64.urlsafe_b64encode(packed).decode().rstrip("=") | |
def pyrogram_to_gramjs(pyro_session, web=True): | |
dc_id, auth_key = unpack_pyrogram_session(pyro_session) | |
if web: | |
server_address = dc_maps[dc_id]['serverAddress'].encode() | |
return ( | |
"1" | |
+ base64.urlsafe_b64encode( | |
struct.pack( | |
">BH{}sH256s".format(len(server_address)), | |
dc_id, | |
len(server_address), | |
server_address, | |
443, | |
auth_key, | |
) | |
).decode("ascii") | |
) | |
else: | |
server_address = ipaddress.ip_address( | |
dc_maps[dc_id]['ipAddress']).packed | |
return ( | |
"1" | |
+ base64.urlsafe_b64encode( | |
struct.pack( | |
">B{}sH256s".format(len(server_address)), | |
dc_id, | |
server_address, | |
443, | |
auth_key, | |
) | |
).decode("ascii") | |
) | |
def pyrogram_to_telthon(pyro_session): | |
return pyrogram_to_gramjs(pyro_session, False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment