From fbded4e23bafef1b42b95381e7e6abd640844840 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 22 Aug 2020 11:26:11 +0200 Subject: [PATCH] Fix time going out of sync when starting new sessions --- pyrogram/session/internals/msg_factory.py | 5 ++--- pyrogram/session/internals/msg_id.py | 15 +++++++++++++-- pyrogram/session/session.py | 7 +++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pyrogram/session/internals/msg_factory.py b/pyrogram/session/internals/msg_factory.py index bf44b7d3..98c88b3f 100644 --- a/pyrogram/session/internals/msg_factory.py +++ b/pyrogram/session/internals/msg_factory.py @@ -26,14 +26,13 @@ not_content_related = (Ping, HttpWait, MsgsAck, MsgContainer) class MsgFactory: - def __init__(self, server_time: float = 0): + def __init__(self): self.seq_no = SeqNo() - self.server_time = server_time def __call__(self, body: TLObject) -> Message: return Message( body, - MsgId(self.server_time), + MsgId(), self.seq_no(not isinstance(body, not_content_related)), len(body) ) diff --git a/pyrogram/session/internals/msg_id.py b/pyrogram/session/internals/msg_id.py index 58474222..72b8e81f 100644 --- a/pyrogram/session/internals/msg_id.py +++ b/pyrogram/session/internals/msg_id.py @@ -16,18 +16,29 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging +from datetime import datetime from time import monotonic +log = logging.getLogger(__name__) + class MsgId: reference_clock = monotonic() last_time = 0 msg_id_offset = 0 + server_time = 0 - def __new__(cls, server_time: float = 0) -> int: - now = monotonic() - cls.reference_clock + server_time + def __new__(cls) -> int: + now = monotonic() - cls.reference_clock + cls.server_time cls.msg_id_offset = cls.msg_id_offset + 4 if now == cls.last_time else 0 msg_id = int(now * 2 ** 32) + cls.msg_id_offset cls.last_time = now return msg_id + + @classmethod + def set_server_time(cls, server_time: int): + if not cls.server_time: + cls.server_time = server_time + log.info(f"Time synced: {datetime.utcfromtimestamp(server_time)} UTC") diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 6c385199..b25b2166 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -23,6 +23,7 @@ from concurrent.futures.thread import ThreadPoolExecutor from datetime import datetime, timedelta from hashlib import sha1 from io import BytesIO +import os import pyrogram from pyrogram import __copyright__, __license__, __version__ @@ -96,7 +97,7 @@ class Session: self.auth_key_id = sha1(auth_key).digest()[-8:] - self.session_id = Long(MsgId(time.time())) + self.session_id = os.urandom(8) self.msg_factory = MsgFactory() self.current_salt = None @@ -247,9 +248,7 @@ class Session: for msg in messages: if msg.seq_no == 0: - server_time = msg.msg_id / (2 ** 32) - self.msg_factory.server_time = server_time - log.info(f"Time synced: {datetime.utcfromtimestamp(server_time)} UTC") + MsgId.set_server_time(msg.msg_id / (2 ** 32)) if msg.seq_no % 2 != 0: if msg.msg_id in self.pending_acks: