GramJs: Properly handle AUTH_KEY_UNREGISTERED (again)
This commit is contained in:
parent
616f8c523e
commit
5cf5c140f8
@ -1,3 +1,5 @@
|
|||||||
|
const { RPCError } = require('../errors');
|
||||||
|
|
||||||
const MtProtoPlainSender = require('./MTProtoPlainSender');
|
const MtProtoPlainSender = require('./MTProtoPlainSender');
|
||||||
const MTProtoState = require('./MTProtoState');
|
const MTProtoState = require('./MTProtoState');
|
||||||
const Helpers = require('../Helpers');
|
const Helpers = require('../Helpers');
|
||||||
@ -16,7 +18,10 @@ const {
|
|||||||
} = require('../tl').constructors;
|
} = require('../tl').constructors;
|
||||||
const MessagePacker = require('../extensions/MessagePacker');
|
const MessagePacker = require('../extensions/MessagePacker');
|
||||||
const BinaryReader = require('../extensions/BinaryReader');
|
const BinaryReader = require('../extensions/BinaryReader');
|
||||||
const { UpdateConnectionState, UpdateServerTimeOffset } = require('./index');
|
const {
|
||||||
|
UpdateConnectionState,
|
||||||
|
UpdateServerTimeOffset,
|
||||||
|
} = require('./index');
|
||||||
const { BadMessageError } = require('../errors/Common');
|
const { BadMessageError } = require('../errors/Common');
|
||||||
const {
|
const {
|
||||||
BadServerSalt,
|
BadServerSalt,
|
||||||
@ -420,13 +425,7 @@ class MTProtoSender {
|
|||||||
} else if (e instanceof InvalidBufferError) {
|
} else if (e instanceof InvalidBufferError) {
|
||||||
// 404 means that the server has "forgotten" our auth key and we need to create a new one.
|
// 404 means that the server has "forgotten" our auth key and we need to create a new one.
|
||||||
if (e.code === 404) {
|
if (e.code === 404) {
|
||||||
this._log.warn(`Broken authorization key for dc ${this._dcId}; resetting`);
|
this._handleBadAuthKey();
|
||||||
if (this._updateCallback && this._isMainSender) {
|
|
||||||
this._updateCallback(new UpdateConnectionState(UpdateConnectionState.broken));
|
|
||||||
} else if (this._onConnectionBreak && !this._isMainSender) {
|
|
||||||
// Deletes the current sender from the object
|
|
||||||
this._onConnectionBreak(this._dcId);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// this happens sometimes when telegram is having some internal issues.
|
// this happens sometimes when telegram is having some internal issues.
|
||||||
// reconnecting should be enough usually
|
// reconnecting should be enough usually
|
||||||
@ -445,12 +444,34 @@ class MTProtoSender {
|
|||||||
try {
|
try {
|
||||||
await this._processMessage(message);
|
await this._processMessage(message);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this._log.error('Unhandled error while receiving data');
|
// `RPCError` errors except for 'AUTH_KEY_UNREGISTERED' should be handled by the client
|
||||||
this._log.error(e);
|
if (e instanceof RPCError) {
|
||||||
|
if (e.message === 'AUTH_KEY_UNREGISTERED') {
|
||||||
|
// 'AUTH_KEY_UNREGISTERED' for the main sender is thrown when unauthorized and should be ignored
|
||||||
|
this._handleBadAuthKey(true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this._log.error('Unhandled error while receiving data');
|
||||||
|
this._log.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_handleBadAuthKey(shouldSkipForMain) {
|
||||||
|
if (shouldSkipForMain && this._isMainSender) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._log.warn(`Broken authorization key for dc ${this._dcId}, resetting...`);
|
||||||
|
|
||||||
|
if (this._isMainSender && this._updateCallback) {
|
||||||
|
this._updateCallback(new UpdateConnectionState(UpdateConnectionState.broken));
|
||||||
|
} else if (!this._isMainSender && this._onConnectionBreak) {
|
||||||
|
this._onConnectionBreak(this._dcId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Response Handlers
|
// Response Handlers
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -557,6 +578,7 @@ class MTProtoSender {
|
|||||||
const error = RPCMessageToError(result.error, state.request);
|
const error = RPCMessageToError(result.error, state.request);
|
||||||
this._send_queue.append(new RequestState(new MsgsAck({ msgIds: [state.msgId] })));
|
this._send_queue.append(new RequestState(new MsgsAck({ msgIds: [state.msgId] })));
|
||||||
state.reject(error);
|
state.reject(error);
|
||||||
|
throw error;
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
const reader = new BinaryReader(result.body);
|
const reader = new BinaryReader(result.body);
|
||||||
@ -826,10 +848,11 @@ class MTProtoSender {
|
|||||||
// this._user_connected = false
|
// this._user_connected = false
|
||||||
// we want to wait a second between each reconnect try to not flood the server with reconnects
|
// we want to wait a second between each reconnect try to not flood the server with reconnects
|
||||||
// in case of internal server issues.
|
// in case of internal server issues.
|
||||||
Helpers.sleep(1000).then(() => {
|
Helpers.sleep(1000)
|
||||||
this._log.info('Started reconnecting');
|
.then(() => {
|
||||||
this._reconnect();
|
this._log.info('Started reconnecting');
|
||||||
});
|
this._reconnect();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user