2018-08-05 17:24:34 +03:00
|
|
|
import base64
|
|
|
|
import ipaddress
|
|
|
|
import struct
|
|
|
|
|
2019-05-06 09:55:24 +03:00
|
|
|
from .abstract import Session
|
2018-08-05 17:24:34 +03:00
|
|
|
from .memory import MemorySession
|
|
|
|
from ..crypto import AuthKey
|
|
|
|
|
2019-02-10 13:10:41 +03:00
|
|
|
_STRUCT_PREFORMAT = '>B{}sH256s'
|
|
|
|
|
2018-08-05 17:24:34 +03:00
|
|
|
CURRENT_VERSION = '1'
|
|
|
|
|
|
|
|
|
2022-05-18 15:53:04 +03:00
|
|
|
class _AsyncString(str):
|
|
|
|
"""
|
|
|
|
Ugly hack to enable awaiting strings.
|
|
|
|
"""
|
|
|
|
def __await__(self):
|
|
|
|
yield
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2018-08-05 17:24:34 +03:00
|
|
|
class StringSession(MemorySession):
|
|
|
|
"""
|
2019-02-10 13:10:41 +03:00
|
|
|
This session file can be easily saved and loaded as a string. According
|
|
|
|
to the initial design, it contains only the data that is necessary for
|
|
|
|
successful connection and authentication, so takeout ID is not stored.
|
2018-08-05 17:24:34 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
by other means.
|
2018-08-22 17:11:51 +03:00
|
|
|
|
|
|
|
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:``.
|
2018-08-05 17:24:34 +03:00
|
|
|
"""
|
2019-05-03 14:59:17 +03:00
|
|
|
def __init__(self, string: str = None):
|
2018-08-05 17:24:34 +03:00
|
|
|
super().__init__()
|
2018-08-05 20:45:56 +03:00
|
|
|
if string:
|
|
|
|
if string[0] != CURRENT_VERSION:
|
|
|
|
raise ValueError('Not a valid string')
|
|
|
|
|
|
|
|
string = string[1:]
|
|
|
|
ip_len = 4 if len(string) == 352 else 16
|
|
|
|
self._dc_id, ip, self._port, key = struct.unpack(
|
2019-05-06 09:55:24 +03:00
|
|
|
_STRUCT_PREFORMAT.format(ip_len), StringSession.decode(string))
|
2018-08-05 20:45:56 +03:00
|
|
|
|
|
|
|
self._server_address = ipaddress.ip_address(ip).compressed
|
|
|
|
if any(key):
|
|
|
|
self._auth_key = AuthKey(key)
|
2018-08-05 17:24:34 +03:00
|
|
|
|
2019-05-03 14:59:17 +03:00
|
|
|
@staticmethod
|
|
|
|
def encode(x: bytes) -> str:
|
|
|
|
return base64.urlsafe_b64encode(x).decode('ascii')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def decode(x: str) -> bytes:
|
|
|
|
return base64.urlsafe_b64decode(x)
|
|
|
|
|
2019-05-06 09:55:24 +03:00
|
|
|
def save(self: Session):
|
2022-05-18 15:53:04 +03:00
|
|
|
# save should be async but that would break code which relies on sync StringSession.
|
|
|
|
# Return a type which behaves like a string for all intents and purposes, but can be awaited.
|
|
|
|
# The await is necessary for the library to save all sessions in the same way.
|
2019-05-06 09:55:24 +03:00
|
|
|
if not self.auth_key:
|
2022-05-18 15:53:04 +03:00
|
|
|
return _AsyncString('')
|
2018-08-05 17:24:34 +03:00
|
|
|
|
2019-05-06 09:55:24 +03:00
|
|
|
ip = ipaddress.ip_address(self.server_address).packed
|
2022-05-18 15:53:04 +03:00
|
|
|
return _AsyncString(CURRENT_VERSION + StringSession.encode(struct.pack(
|
2019-02-10 13:10:41 +03:00
|
|
|
_STRUCT_PREFORMAT.format(len(ip)),
|
2019-05-06 09:55:24 +03:00
|
|
|
self.dc_id,
|
2018-08-05 17:24:34 +03:00
|
|
|
ip,
|
2019-05-06 09:55:24 +03:00
|
|
|
self.port,
|
|
|
|
self.auth_key.key
|
2022-05-18 15:53:04 +03:00
|
|
|
)))
|