Update time offset if first message is badMsgNotification

This should prevent the library from getting stuck during connect.
This commit is contained in:
Lonami Exo 2025-04-17 17:08:08 +02:00
parent 11658d3bbf
commit 9545011de6

View File

@ -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)