diff --git a/packages/core/src/network/authorization.ts b/packages/core/src/network/authorization.ts index a9d5d7fd..b8e83224 100644 --- a/packages/core/src/network/authorization.ts +++ b/packages/core/src/network/authorization.ts @@ -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) diff --git a/packages/core/src/network/mtproto-session.ts b/packages/core/src/network/mtproto-session.ts index 9d4342bf..9f9b0be8 100644 --- a/packages/core/src/network/mtproto-session.ts +++ b/packages/core/src/network/mtproto-session.ts @@ -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) diff --git a/packages/core/src/network/session-connection.ts b/packages/core/src/network/session-connection.ts index 753a68de..cda4381f 100644 --- a/packages/core/src/network/session-connection.ts +++ b/packages/core/src/network/session-connection.ts @@ -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) }