On 401 response from Signal server, reconnect websocket
This commit is contained in:
parent
4ce4569afb
commit
38f9aef2af
2 changed files with 54 additions and 9 deletions
|
@ -21,6 +21,7 @@ import { removeStorageKeyJobQueue } from './jobs/removeStorageKeyJobQueue';
|
|||
import { ourProfileKeyService } from './services/ourProfileKey';
|
||||
import { shouldRespondWithProfileKey } from './util/shouldRespondWithProfileKey';
|
||||
import { setToExpire } from './services/MessageUpdater';
|
||||
import { LatestQueue } from './util/LatestQueue';
|
||||
|
||||
const MAX_ATTACHMENT_DOWNLOAD_AGE = 3600 * 72 * 1000;
|
||||
|
||||
|
@ -1442,6 +1443,29 @@ export async function startApp(): Promise<void> {
|
|||
}
|
||||
});
|
||||
|
||||
const reconnectToWebSocketQueue = new LatestQueue();
|
||||
|
||||
const enqueueReconnectToWebSocket = () => {
|
||||
reconnectToWebSocketQueue.add(async () => {
|
||||
if (!messageReceiver) {
|
||||
window.log.info(
|
||||
'reconnectToWebSocket: No messageReceiver. Early return.'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
window.log.info('reconnectToWebSocket starting...');
|
||||
await disconnect();
|
||||
connect();
|
||||
window.log.info('reconnectToWebSocket complete.');
|
||||
});
|
||||
};
|
||||
|
||||
window.Whisper.events.on(
|
||||
'mightBeUnlinked',
|
||||
window._.debounce(enqueueReconnectToWebSocket, 1000, { maxWait: 5000 })
|
||||
);
|
||||
|
||||
function runStorageService() {
|
||||
window.Signal.Services.enableStorageService();
|
||||
window.textsecure.messaging.sendRequestKeySyncMessage();
|
||||
|
@ -1758,16 +1782,16 @@ export async function startApp(): Promise<void> {
|
|||
);
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
async function disconnect() {
|
||||
window.log.info('disconnect');
|
||||
|
||||
// Clear timer, since we're only called when the timer is expired
|
||||
disconnectTimer = null;
|
||||
|
||||
if (messageReceiver) {
|
||||
messageReceiver.close();
|
||||
}
|
||||
window.Signal.AttachmentDownloads.stop();
|
||||
if (messageReceiver) {
|
||||
await messageReceiver.close();
|
||||
}
|
||||
}
|
||||
|
||||
let connectCount = 0;
|
||||
|
|
|
@ -330,6 +330,7 @@ type PromiseAjaxOptionsType = {
|
|||
| 'jsonwithdetails'
|
||||
| 'arraybuffer'
|
||||
| 'arraybufferwithdetails';
|
||||
serverUrl?: string;
|
||||
stack?: string;
|
||||
timeout?: number;
|
||||
type: HTTPCodeType;
|
||||
|
@ -354,6 +355,11 @@ function isSuccess(status: number): boolean {
|
|||
return status >= 0 && status < 400;
|
||||
}
|
||||
|
||||
function getHostname(url: string): string {
|
||||
const urlObject = new URL(url);
|
||||
return urlObject.hostname;
|
||||
}
|
||||
|
||||
async function _promiseAjax(
|
||||
providedUrl: string | null,
|
||||
options: PromiseAjaxOptionsType
|
||||
|
@ -438,11 +444,25 @@ async function _promiseAjax(
|
|||
|
||||
fetch(url, fetchOptions)
|
||||
.then(async response => {
|
||||
// Build expired!
|
||||
if (response.status === 499) {
|
||||
window.log.error('Error: build expired');
|
||||
await window.storage.put('remoteBuildExpiration', Date.now());
|
||||
window.reduxActions.expiration.hydrateExpirationStatus(true);
|
||||
if (options.serverUrl) {
|
||||
if (
|
||||
response.status === 499 &&
|
||||
getHostname(options.serverUrl) === getHostname(url)
|
||||
) {
|
||||
window.log.error('Got 499 from Signal Server. Build is expired.');
|
||||
await window.storage.put('remoteBuildExpiration', Date.now());
|
||||
window.reduxActions.expiration.hydrateExpirationStatus(true);
|
||||
}
|
||||
if (
|
||||
!unauthenticated &&
|
||||
response.status === 401 &&
|
||||
getHostname(options.serverUrl) === getHostname(url)
|
||||
) {
|
||||
window.log.error(
|
||||
'Got 401 from Signal Server. We might be unlinked.'
|
||||
);
|
||||
window.Whisper.events.trigger('mightBeUnlinked');
|
||||
}
|
||||
}
|
||||
|
||||
let resultPromise;
|
||||
|
@ -1071,6 +1091,7 @@ export function initialize({
|
|||
type: param.httpType,
|
||||
user: param.username || username,
|
||||
redactUrl: param.redactUrl,
|
||||
serverUrl: url,
|
||||
validateResponse: param.validateResponse,
|
||||
version,
|
||||
unauthenticated: param.unauthenticated,
|
||||
|
|
Loading…
Reference in a new issue