Fix saving of StringSession

This commit is contained in:
Lonami Exo 2019-05-06 08:55:24 +02:00
parent 7d0efcf50f
commit 744f5f50fe
3 changed files with 18 additions and 9 deletions

View File

@ -20,6 +20,14 @@ class Session(ABC):
"""
raise NotImplementedError
@property
@abstractmethod
def dc_id(self):
"""
Returns the currently-used data center ID.
"""
raise NotImplementedError
@property
@abstractmethod
def server_address(self):

View File

@ -2,6 +2,7 @@ import base64
import ipaddress
import struct
from .abstract import Session
from .memory import MemorySession
from ..crypto import AuthKey
@ -34,7 +35,7 @@ class StringSession(MemorySession):
string = string[1:]
ip_len = 4 if len(string) == 352 else 16
self._dc_id, ip, self._port, key = struct.unpack(
_STRUCT_PREFORMAT.format(ip_len), self.decode(string))
_STRUCT_PREFORMAT.format(ip_len), StringSession.decode(string))
self._server_address = ipaddress.ip_address(ip).compressed
if any(key):
@ -48,15 +49,15 @@ class StringSession(MemorySession):
def decode(x: str) -> bytes:
return base64.urlsafe_b64decode(x)
def save(self):
if not self._auth_key:
def save(self: Session):
if not self.auth_key:
return ''
ip = ipaddress.ip_address(self._server_address).packed
return CURRENT_VERSION + self.encode(struct.pack(
ip = ipaddress.ip_address(self.server_address).packed
return CURRENT_VERSION + StringSession.encode(struct.pack(
_STRUCT_PREFORMAT.format(len(ip)),
self._dc_id,
self.dc_id,
ip,
self._port,
self._auth_key.key
self.port,
self.auth_key.key
))

View File

@ -1,3 +1,3 @@
# Versions should comply with PEP440.
# This line is parsed in setup.py:
__version__ = '1.7.6'
__version__ = '1.7.7'