mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-29 12:53:44 +03:00
Support custom encoding for StringSession
This commit is contained in:
parent
aa9bc9080f
commit
8e62f797bb
|
@ -15,7 +15,15 @@ class StringSession(MemorySession):
|
||||||
It is thought to be used where you don't want to create any on-disk
|
It is thought to be used where you don't want to create any on-disk
|
||||||
files but would still like to be able to save and load existing sessions
|
files but would still like to be able to save and load existing sessions
|
||||||
by other means.
|
by other means.
|
||||||
|
|
||||||
|
You can use custom `encode` and `decode` functions, if present:
|
||||||
|
|
||||||
|
* `encode` definition must be ``def encode(value: bytes) -> str:``.
|
||||||
|
* `decode` definition must be ``def decode(value: str) -> bytes:``.
|
||||||
"""
|
"""
|
||||||
|
encode = lambda x: base64.urlsafe_b64encode(x).decode('ascii')
|
||||||
|
decode = base64.urlsafe_b64decode
|
||||||
|
|
||||||
def __init__(self, string=None):
|
def __init__(self, string=None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
if string:
|
if string:
|
||||||
|
@ -25,7 +33,7 @@ class StringSession(MemorySession):
|
||||||
string = string[1:]
|
string = string[1:]
|
||||||
ip_len = 4 if len(string) == 352 else 16
|
ip_len = 4 if len(string) == 352 else 16
|
||||||
self._dc_id, ip, self._port, key = struct.unpack(
|
self._dc_id, ip, self._port, key = struct.unpack(
|
||||||
'>B{}sH256s'.format(ip_len), base64.urlsafe_b64decode(string))
|
'>B{}sH256s'.format(ip_len), StringSession.decode(string))
|
||||||
|
|
||||||
self._server_address = ipaddress.ip_address(ip).compressed
|
self._server_address = ipaddress.ip_address(ip).compressed
|
||||||
if any(key):
|
if any(key):
|
||||||
|
@ -36,10 +44,10 @@ class StringSession(MemorySession):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
ip = ipaddress.ip_address(self._server_address).packed
|
ip = ipaddress.ip_address(self._server_address).packed
|
||||||
return CURRENT_VERSION + base64.urlsafe_b64encode(struct.pack(
|
return CURRENT_VERSION + StringSession.encode(struct.pack(
|
||||||
'>B{}sH256s'.format(len(ip)),
|
'>B{}sH256s'.format(len(ip)),
|
||||||
self._dc_id,
|
self._dc_id,
|
||||||
ip,
|
ip,
|
||||||
self._port,
|
self._port,
|
||||||
self._auth_key.key
|
self._auth_key.key
|
||||||
)).decode('ascii')
|
))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user