On 401 response from Signal server, reconnect websocket

This commit is contained in:
Scott Nonnenberg 2021-05-17 11:29:37 -07:00
parent 4ce4569afb
commit 38f9aef2af
2 changed files with 54 additions and 9 deletions

View file

@ -21,6 +21,7 @@ import { removeStorageKeyJobQueue } from './jobs/removeStorageKeyJobQueue';
import { ourProfileKeyService } from './services/ourProfileKey'; import { ourProfileKeyService } from './services/ourProfileKey';
import { shouldRespondWithProfileKey } from './util/shouldRespondWithProfileKey'; import { shouldRespondWithProfileKey } from './util/shouldRespondWithProfileKey';
import { setToExpire } from './services/MessageUpdater'; import { setToExpire } from './services/MessageUpdater';
import { LatestQueue } from './util/LatestQueue';
const MAX_ATTACHMENT_DOWNLOAD_AGE = 3600 * 72 * 1000; 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() { function runStorageService() {
window.Signal.Services.enableStorageService(); window.Signal.Services.enableStorageService();
window.textsecure.messaging.sendRequestKeySyncMessage(); window.textsecure.messaging.sendRequestKeySyncMessage();
@ -1758,16 +1782,16 @@ export async function startApp(): Promise<void> {
); );
} }
function disconnect() { async function disconnect() {
window.log.info('disconnect'); window.log.info('disconnect');
// Clear timer, since we're only called when the timer is expired // Clear timer, since we're only called when the timer is expired
disconnectTimer = null; disconnectTimer = null;
if (messageReceiver) {
messageReceiver.close();
}
window.Signal.AttachmentDownloads.stop(); window.Signal.AttachmentDownloads.stop();
if (messageReceiver) {
await messageReceiver.close();
}
} }
let connectCount = 0; let connectCount = 0;

View file

@ -330,6 +330,7 @@ type PromiseAjaxOptionsType = {
| 'jsonwithdetails' | 'jsonwithdetails'
| 'arraybuffer' | 'arraybuffer'
| 'arraybufferwithdetails'; | 'arraybufferwithdetails';
serverUrl?: string;
stack?: string; stack?: string;
timeout?: number; timeout?: number;
type: HTTPCodeType; type: HTTPCodeType;
@ -354,6 +355,11 @@ function isSuccess(status: number): boolean {
return status >= 0 && status < 400; return status >= 0 && status < 400;
} }
function getHostname(url: string): string {
const urlObject = new URL(url);
return urlObject.hostname;
}
async function _promiseAjax( async function _promiseAjax(
providedUrl: string | null, providedUrl: string | null,
options: PromiseAjaxOptionsType options: PromiseAjaxOptionsType
@ -438,12 +444,26 @@ async function _promiseAjax(
fetch(url, fetchOptions) fetch(url, fetchOptions)
.then(async response => { .then(async response => {
// Build expired! if (options.serverUrl) {
if (response.status === 499) { if (
window.log.error('Error: build expired'); 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()); await window.storage.put('remoteBuildExpiration', Date.now());
window.reduxActions.expiration.hydrateExpirationStatus(true); 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; let resultPromise;
if (DEBUG && !isSuccess(response.status)) { if (DEBUG && !isSuccess(response.status)) {
@ -1071,6 +1091,7 @@ export function initialize({
type: param.httpType, type: param.httpType,
user: param.username || username, user: param.username || username,
redactUrl: param.redactUrl, redactUrl: param.redactUrl,
serverUrl: url,
validateResponse: param.validateResponse, validateResponse: param.validateResponse,
version, version,
unauthenticated: param.unauthenticated, unauthenticated: param.unauthenticated,