diff --git a/packages/core/src/network/multi-session-connection.ts b/packages/core/src/network/multi-session-connection.ts index 07c1a73d..59f1ea96 100644 --- a/packages/core/src/network/multi-session-connection.ts +++ b/packages/core/src/network/multi-session-connection.ts @@ -143,6 +143,7 @@ export class MultiSessionConnection { }) this._connections.push(conn) + if (this._authKey !== null) conn._session._authKey.setup(this._authKey) // if enforcePfsChanged, we need to connect after setting the new auth key if (connect && !enforcePfsChanged) conn.connect() } @@ -212,10 +213,16 @@ export class MultiSessionConnection { this.connect() } - setAuthKey(authKey: Uint8Array | null, temp = false, idx = 0): void { - const session = this._connections[idx]._session - const key = temp ? session._authKeyTemp : session._authKey - key.setup(authKey) + private _authKey: Uint8Array | null = null + setAuthKey(authKey: Uint8Array | null): void { + this._authKey = authKey + for (const conn of this._connections) { + conn._session._authKey.setup(authKey) + } + } + + setTempAuthKey(authKey: Uint8Array | null, idx: number): void { + this._connections[idx]?._session._authKeyTemp.setup(authKey) } resetAuthKeys(): void { diff --git a/packages/core/src/network/network-manager.ts b/packages/core/src/network/network-manager.ts index 1ee74217..d3926aa2 100644 --- a/packages/core/src/network/network-manager.ts +++ b/packages/core/src/network/network-manager.ts @@ -314,7 +314,7 @@ export class DcConnectionManager { const connection = this[kind] connection.onKeyChange.add(([idx, key]) => { - if (kind !== 'main') { + if (kind !== 'main' && key !== null) { // main connection is responsible for authorization, // and keys are then sent to other connections this.manager._log.warn('got key-change from non-main connection, ignoring') @@ -322,7 +322,7 @@ export class DcConnectionManager { return } - this.manager._log.debug('key change for dc %d from connection %d', this.dcId, idx) + this.manager._log.debug('key change (empty=%b) for dc %d from connection %d', key === null, this.dcId, idx) // send key to other connections this.upload.setAuthKey(key) @@ -433,7 +433,7 @@ export class DcConnectionManager { await Promise.all( Array.from({ length: this.main.getCount() }, async (_, i) => { const temp = await this.manager._storage.provider.authKeys.getTemp(this.dcId, i, now) - this.main.setAuthKey(temp, true, i) + this.main.setTempAuthKey(temp, i) // NB: we do not set temp auth keys for media connections, // as they are ephemeral and dc-bound. doing this *will* lead to unwanted -404s diff --git a/packages/core/src/network/session-connection.ts b/packages/core/src/network/session-connection.ts index f9c73b65..28cdf7d0 100644 --- a/packages/core/src/network/session-connection.ts +++ b/packages/core/src/network/session-connection.ts @@ -99,6 +99,8 @@ export class SessionConnection extends PersistentConnection { readonly onUpdate: Emitter = new Emitter() readonly onFutureSalts: Emitter = new Emitter() + private _triedReconnectingOn404 = false + constructor(params: SessionConnectionParams, log: Logger) { super(params, log.create('conn')) this._session = new MtprotoSession( @@ -263,6 +265,14 @@ export class SessionConnection extends PersistentConnection { // otherwise, 404 must be referencing the perm_key } + if (!this._triedReconnectingOn404) { + // maybe this is some mtproto issue? let's try reconnecting and hope for the best + this.log.warn('transport error 404. trying to reconnect') + this._triedReconnectingOn404 = true + this._resetSession() + return + } + // there happened a little trolling this.log.warn('transport error 404, reauthorizing') this._session.resetAuthKey() @@ -1176,6 +1186,7 @@ export class SessionConnection extends PersistentConnection { } this._salts.currentSalt = serverSalt + this._triedReconnectingOn404 = false this.log.debug('received new_session_created, uid = %l, first msg_id = %l', uniqueId, firstMsgId)