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

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