fix(core): infinite bad_msg_notification 17 when system time is wrong

This commit is contained in:
alina 🌸 2024-02-28 13:18:36 +03:00
parent 371aea910b
commit be899afca2
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 12 additions and 4 deletions

View file

@ -333,6 +333,7 @@ export async function doAuthorization(
const dhPrime = bufferToBigInt(serverDhInner.dhPrime)
const timeOffset = Math.floor(Date.now() / 1000) - serverDhInner.serverTime
session.updateTimeOffset(timeOffset)
const g = BigInt(serverDhInner.g)
const gA = bufferToBigInt(serverDhInner.gA)

View file

@ -169,6 +169,13 @@ export class MtprotoSession {
this._authKeyTempSecondary.reset()
}
updateTimeOffset(offset: number) {
this._timeOffset = offset
// lastMessageId was generated with (potentially) wrong time
// reset it to avoid bigger issues - at worst, we'll get bad_msg_notification
this._lastMessageId = Long.ZERO
}
/**
* Reset session state and generate a new session ID.
*
@ -230,7 +237,7 @@ export class MtprotoSession {
getMessageId(): Long {
const timeTicks = Date.now()
const timeSec = Math.floor(timeTicks / 1000) + this._timeOffset
const timeSec = Math.floor(timeTicks / 1000) - this._timeOffset
const timeMSec = timeTicks % 1000
const random = getRandomInt(0xffff)

View file

@ -281,7 +281,7 @@ export class SessionConnection extends PersistentConnection {
.then(([authKey, serverSalt, timeOffset]) => {
this._session._authKey.setup(authKey)
this._salts.currentSalt = serverSalt
this._session._timeOffset = timeOffset
this._session.updateTimeOffset(timeOffset)
this._session.authorizationPending = false
@ -1092,10 +1092,10 @@ export class SessionConnection extends PersistentConnection {
// msg_id is either too high or too low
// code 20 means msg_id is too old,
// we just need to resend the message
const serverTime = msgId.low >>> 0
const serverTime = msgId.high >>> 0
const timeOffset = Math.floor(Date.now() / 1000) - serverTime
this._session._timeOffset = timeOffset
this._session.updateTimeOffset(timeOffset)
this.log.debug('server time: %d, corrected offset to %d', serverTime, timeOffset)
}