fix(core): networking fixes

This commit is contained in:
alina 🌸 2024-12-26 19:59:32 +03:00
parent 7eaacc5343
commit 3068c07347
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
3 changed files with 25 additions and 7 deletions

View file

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

View file

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

View file

@ -99,6 +99,8 @@ export class SessionConnection extends PersistentConnection {
readonly onUpdate: Emitter<tl.TypeUpdates> = new Emitter()
readonly onFutureSalts: Emitter<mtp.RawMt_future_salt[]> = 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)