diff --git a/telethon/network/mtprotostate.py b/telethon/network/mtprotostate.py index 53b66306..1444ac5b 100644 --- a/telethon/network/mtprotostate.py +++ b/telethon/network/mtprotostate.py @@ -152,7 +152,7 @@ class MTProtoState: """ Inverse of `encrypt_message_data` for incoming server messages. """ - now = time.time() + self.time_offset # get the time as early as possible, even if other checks make it go unused + now = time.time() # get the time as early as possible, even if other checks make it go unused if len(body) < 8: raise InvalidBufferError(body) @@ -204,8 +204,14 @@ class MTProtoState: # # This means we skip the time check for certain types of messages. if obj.CONSTRUCTOR_ID not in (BadServerSalt.CONSTRUCTOR_ID, BadMsgNotification.CONSTRUCTOR_ID): + if not self._highest_remote_id and not self.time_offset: + # If the first message we receive is a bad notification, take this opportunity + # to adjust the time offset. Assume it will remain stable afterwards. Updating + # the offset unconditionally would make the next checks pointless. + self.update_time_offset(remote_msg_id) + remote_msg_time = remote_msg_id >> 32 - time_delta = now - remote_msg_time + time_delta = (now + self.time_offset) - remote_msg_time if time_delta > MSG_TOO_OLD_DELTA: self._log.warning('Server sent a very old message with ID %d, ignoring (see FAQ for details)', remote_msg_id)