mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 13:27:47 +00:00
Add string session storage
This commit is contained in:
parent
431a983d5b
commit
b04cf9ec92
@ -19,3 +19,4 @@
|
|||||||
from .session_storage_mixin import SessionStorageMixin
|
from .session_storage_mixin import SessionStorageMixin
|
||||||
from .base_session_storage import BaseSessionStorage, SessionDoesNotExist
|
from .base_session_storage import BaseSessionStorage, SessionDoesNotExist
|
||||||
from .json_session_storage import JsonSessionStorage
|
from .json_session_storage import JsonSessionStorage
|
||||||
|
from .string_session_storage import StringSessionStorage
|
||||||
|
38
pyrogram/client/session_storage/string_session_storage.py
Normal file
38
pyrogram/client/session_storage/string_session_storage.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import base64
|
||||||
|
import binascii
|
||||||
|
import struct
|
||||||
|
|
||||||
|
from . import BaseSessionStorage, SessionDoesNotExist
|
||||||
|
|
||||||
|
|
||||||
|
def StringSessionStorage(print_session: bool = False):
|
||||||
|
class StringSessionStorageClass(BaseSessionStorage):
|
||||||
|
"""
|
||||||
|
Packs session data as following (forcing little-endian byte order):
|
||||||
|
Char dc_id (1 byte, unsigned)
|
||||||
|
Boolean test_mode (1 byte)
|
||||||
|
Long long user_id (8 bytes, signed)
|
||||||
|
Bytes auth_key (256 bytes)
|
||||||
|
|
||||||
|
Uses Base64 encoding for printable representation
|
||||||
|
"""
|
||||||
|
PACK_FORMAT = '<B?q256s'
|
||||||
|
|
||||||
|
def load_session(self, session_string: str):
|
||||||
|
try:
|
||||||
|
decoded = base64.b64decode(session_string)
|
||||||
|
self.dc_id, self.test_mode, self.user_id, self.auth_key = struct.unpack(self.PACK_FORMAT, decoded)
|
||||||
|
except (struct.error, binascii.Error):
|
||||||
|
raise SessionDoesNotExist()
|
||||||
|
|
||||||
|
def save_session(self, session_string: str, sync=False):
|
||||||
|
if print_session and not sync:
|
||||||
|
packed = struct.pack(self.PACK_FORMAT, self.dc_id, self.test_mode, self.user_id, self.auth_key)
|
||||||
|
encoded = base64.b64encode(packed).decode('latin-1')
|
||||||
|
split = '\n'.join(['"{}"'.format(encoded[i: i + 50]) for i in range(0, len(encoded), 50)])
|
||||||
|
print('Created session string:\n{}'.format(split))
|
||||||
|
|
||||||
|
def sync_cleanup(self, session_string: str):
|
||||||
|
pass
|
||||||
|
|
||||||
|
return StringSessionStorageClass
|
Loading…
x
Reference in New Issue
Block a user