fix(core): infinite bad_msg_notification 17 when system time is wrong
This commit is contained in:
parent
371aea910b
commit
be899afca2
3 changed files with 12 additions and 4 deletions
|
@ -333,6 +333,7 @@ export async function doAuthorization(
|
||||||
|
|
||||||
const dhPrime = bufferToBigInt(serverDhInner.dhPrime)
|
const dhPrime = bufferToBigInt(serverDhInner.dhPrime)
|
||||||
const timeOffset = Math.floor(Date.now() / 1000) - serverDhInner.serverTime
|
const timeOffset = Math.floor(Date.now() / 1000) - serverDhInner.serverTime
|
||||||
|
session.updateTimeOffset(timeOffset)
|
||||||
|
|
||||||
const g = BigInt(serverDhInner.g)
|
const g = BigInt(serverDhInner.g)
|
||||||
const gA = bufferToBigInt(serverDhInner.gA)
|
const gA = bufferToBigInt(serverDhInner.gA)
|
||||||
|
|
|
@ -169,6 +169,13 @@ export class MtprotoSession {
|
||||||
this._authKeyTempSecondary.reset()
|
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.
|
* Reset session state and generate a new session ID.
|
||||||
*
|
*
|
||||||
|
@ -230,7 +237,7 @@ export class MtprotoSession {
|
||||||
|
|
||||||
getMessageId(): Long {
|
getMessageId(): Long {
|
||||||
const timeTicks = Date.now()
|
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 timeMSec = timeTicks % 1000
|
||||||
const random = getRandomInt(0xffff)
|
const random = getRandomInt(0xffff)
|
||||||
|
|
||||||
|
|
|
@ -281,7 +281,7 @@ export class SessionConnection extends PersistentConnection {
|
||||||
.then(([authKey, serverSalt, timeOffset]) => {
|
.then(([authKey, serverSalt, timeOffset]) => {
|
||||||
this._session._authKey.setup(authKey)
|
this._session._authKey.setup(authKey)
|
||||||
this._salts.currentSalt = serverSalt
|
this._salts.currentSalt = serverSalt
|
||||||
this._session._timeOffset = timeOffset
|
this._session.updateTimeOffset(timeOffset)
|
||||||
|
|
||||||
this._session.authorizationPending = false
|
this._session.authorizationPending = false
|
||||||
|
|
||||||
|
@ -1092,10 +1092,10 @@ export class SessionConnection extends PersistentConnection {
|
||||||
// msg_id is either too high or too low
|
// msg_id is either too high or too low
|
||||||
// code 20 means msg_id is too old,
|
// code 20 means msg_id is too old,
|
||||||
// we just need to resend the message
|
// 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
|
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)
|
this.log.debug('server time: %d, corrected offset to %d', serverTime, timeOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue