Fix socket management for unlinkAndDisconnect
This commit is contained in:
parent
039bd072ed
commit
5780c3d4b8
5 changed files with 55 additions and 24 deletions
|
@ -212,7 +212,7 @@ window.encryptAndUpload = async (
|
||||||
const server = WebAPI.connect({
|
const server = WebAPI.connect({
|
||||||
username: username || oldUsername,
|
username: username || oldUsername,
|
||||||
password,
|
password,
|
||||||
disableWebSockets: true,
|
useWebSocket: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const uniqueStickers = uniqBy(
|
const uniqueStickers = uniqBy(
|
||||||
|
|
|
@ -636,7 +636,7 @@ export async function startApp(): Promise<void> {
|
||||||
'WebAPI should be initialized together with MessageReceiver'
|
'WebAPI should be initialized together with MessageReceiver'
|
||||||
);
|
);
|
||||||
server.unregisterRequestHandler(messageReceiver);
|
server.unregisterRequestHandler(messageReceiver);
|
||||||
await messageReceiver.stopProcessing();
|
messageReceiver.stopProcessing();
|
||||||
await window.waitForAllBatchers();
|
await window.waitForAllBatchers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3334,18 +3334,23 @@ export async function startApp(): Promise<void> {
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
window.Whisper.events.trigger('unauthorized');
|
window.Whisper.events.trigger('unauthorized');
|
||||||
|
|
||||||
|
window.log.warn(
|
||||||
|
'unlinkAndDisconnect: Client is no longer authorized; ' +
|
||||||
|
'deleting local configuration'
|
||||||
|
);
|
||||||
|
|
||||||
if (messageReceiver) {
|
if (messageReceiver) {
|
||||||
|
window.log.info('unlinkAndDisconnect: logging out');
|
||||||
strictAssert(server !== undefined, 'WebAPI not initialized');
|
strictAssert(server !== undefined, 'WebAPI not initialized');
|
||||||
server.unregisterRequestHandler(messageReceiver);
|
server.unregisterRequestHandler(messageReceiver);
|
||||||
await messageReceiver.stopProcessing();
|
messageReceiver.stopProcessing();
|
||||||
|
|
||||||
|
await server.logout();
|
||||||
await window.waitForAllBatchers();
|
await window.waitForAllBatchers();
|
||||||
}
|
}
|
||||||
|
|
||||||
onEmpty();
|
onEmpty();
|
||||||
|
|
||||||
window.log.warn(
|
|
||||||
'Client is no longer authorized; deleting local configuration'
|
|
||||||
);
|
|
||||||
window.Signal.Util.Registration.remove();
|
window.Signal.Util.Registration.remove();
|
||||||
|
|
||||||
const NUMBER_ID_KEY = 'number_id';
|
const NUMBER_ID_KEY = 'number_id';
|
||||||
|
@ -3364,6 +3369,7 @@ export async function startApp(): Promise<void> {
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
window.log.info('unlinkAndDisconnect: removing configuration');
|
||||||
await window.textsecure.storage.protocol.removeAllConfiguration(mode);
|
await window.textsecure.storage.protocol.removeAllConfiguration(mode);
|
||||||
|
|
||||||
// This was already done in the database with removeAllConfiguration; this does it
|
// This was already done in the database with removeAllConfiguration; this does it
|
||||||
|
@ -3398,10 +3404,13 @@ export async function startApp(): Promise<void> {
|
||||||
}
|
}
|
||||||
await window.textsecure.storage.put(VERSION_KEY, window.getVersion());
|
await window.textsecure.storage.put(VERSION_KEY, window.getVersion());
|
||||||
|
|
||||||
window.log.info('Successfully cleared local configuration');
|
window.log.info(
|
||||||
|
'unlinkAndDisconnect: Successfully cleared local configuration'
|
||||||
|
);
|
||||||
} catch (eraseError) {
|
} catch (eraseError) {
|
||||||
window.log.error(
|
window.log.error(
|
||||||
'Something went wrong clearing local configuration',
|
'unlinkAndDisconnect: Something went wrong clearing ' +
|
||||||
|
'local configuration',
|
||||||
eraseError && eraseError.stack ? eraseError.stack : eraseError
|
eraseError && eraseError.stack ? eraseError.stack : eraseError
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -187,14 +187,18 @@ export class SocketManager extends EventListener {
|
||||||
authenticated = await process.getResult();
|
authenticated = await process.getResult();
|
||||||
this.status = SocketStatus.OPEN;
|
this.status = SocketStatus.OPEN;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
strictAssert(this.authenticated === process, 'Someone stole our socket');
|
|
||||||
this.dropAuthenticated(process);
|
|
||||||
|
|
||||||
window.log.warn(
|
window.log.warn(
|
||||||
'SocketManager: authenticated socket connection failed with ' +
|
'SocketManager: authenticated socket connection failed with ' +
|
||||||
`error: ${Errors.toLogFormat(error)}`
|
`error: ${Errors.toLogFormat(error)}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// The socket was deliberately closed, don't follow up
|
||||||
|
if (this.authenticated !== process) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dropAuthenticated(process);
|
||||||
|
|
||||||
if (error instanceof HTTPError) {
|
if (error instanceof HTTPError) {
|
||||||
const { code } = error;
|
const { code } = error;
|
||||||
|
|
||||||
|
@ -389,6 +393,16 @@ export class SocketManager extends EventListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async logout(): Promise<void> {
|
||||||
|
const { authenticated } = this;
|
||||||
|
if (authenticated) {
|
||||||
|
authenticated.abort();
|
||||||
|
this.dropAuthenticated(authenticated);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.credentials = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Private
|
// Private
|
||||||
//
|
//
|
||||||
|
|
|
@ -755,7 +755,7 @@ type AjaxOptionsType = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WebAPIConnectOptionsType = WebAPICredentials & {
|
export type WebAPIConnectOptionsType = WebAPICredentials & {
|
||||||
disableWebSockets?: boolean;
|
useWebSocket?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type WebAPIConnectType = {
|
export type WebAPIConnectType = {
|
||||||
|
@ -964,6 +964,7 @@ export type WebAPIType = {
|
||||||
Array<{ name: string; enabled: boolean; value: string | null }>
|
Array<{ name: string; enabled: boolean; value: string | null }>
|
||||||
>;
|
>;
|
||||||
authenticate: (credentials: WebAPICredentials) => Promise<void>;
|
authenticate: (credentials: WebAPICredentials) => Promise<void>;
|
||||||
|
logout: () => Promise<void>;
|
||||||
getSocketStatus: () => SocketStatus;
|
getSocketStatus: () => SocketStatus;
|
||||||
registerRequestHandler: (handler: IRequestHandler) => void;
|
registerRequestHandler: (handler: IRequestHandler) => void;
|
||||||
unregisterRequestHandler: (handler: IRequestHandler) => void;
|
unregisterRequestHandler: (handler: IRequestHandler) => void;
|
||||||
|
@ -1078,7 +1079,7 @@ export function initialize({
|
||||||
function connect({
|
function connect({
|
||||||
username: initialUsername,
|
username: initialUsername,
|
||||||
password: initialPassword,
|
password: initialPassword,
|
||||||
disableWebSockets = false,
|
useWebSocket = true,
|
||||||
}: WebAPIConnectOptionsType) {
|
}: WebAPIConnectOptionsType) {
|
||||||
let username = initialUsername;
|
let username = initialUsername;
|
||||||
let password = initialPassword;
|
let password = initialPassword;
|
||||||
|
@ -1096,7 +1097,7 @@ export function initialize({
|
||||||
window.Whisper.events.trigger('unlinkAndDisconnect');
|
window.Whisper.events.trigger('unlinkAndDisconnect');
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!disableWebSockets) {
|
if (useWebSocket) {
|
||||||
socketManager.authenticate({ username, password });
|
socketManager.authenticate({ username, password });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1109,6 +1110,7 @@ export function initialize({
|
||||||
registerRequestHandler,
|
registerRequestHandler,
|
||||||
unregisterRequestHandler,
|
unregisterRequestHandler,
|
||||||
authenticate,
|
authenticate,
|
||||||
|
logout,
|
||||||
confirmCode,
|
confirmCode,
|
||||||
createGroup,
|
createGroup,
|
||||||
fetchLinkPreviewImage,
|
fetchLinkPreviewImage,
|
||||||
|
@ -1166,11 +1168,11 @@ export function initialize({
|
||||||
param.urlParameters = '';
|
param.urlParameters = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
const useWebSocket =
|
const useWebSocketForEndpoint =
|
||||||
!disableWebSockets && WEBSOCKET_CALLS.has(param.call);
|
useWebSocket && WEBSOCKET_CALLS.has(param.call);
|
||||||
|
|
||||||
return _outerAjax(null, {
|
return _outerAjax(null, {
|
||||||
socketManager: useWebSocket ? socketManager : undefined,
|
socketManager: useWebSocketForEndpoint ? socketManager : undefined,
|
||||||
basicAuth: param.basicAuth,
|
basicAuth: param.basicAuth,
|
||||||
certificateAuthority,
|
certificateAuthority,
|
||||||
contentType: param.contentType || 'application/json; charset=utf-8',
|
contentType: param.contentType || 'application/json; charset=utf-8',
|
||||||
|
@ -1221,11 +1223,20 @@ export function initialize({
|
||||||
username = newUsername;
|
username = newUsername;
|
||||||
password = newPassword;
|
password = newPassword;
|
||||||
|
|
||||||
if (!disableWebSockets) {
|
if (useWebSocket) {
|
||||||
await socketManager.authenticate({ username, password });
|
await socketManager.authenticate({ username, password });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function logout() {
|
||||||
|
username = '';
|
||||||
|
password = '';
|
||||||
|
|
||||||
|
if (useWebSocket) {
|
||||||
|
await socketManager.logout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getSocketStatus(): SocketStatus {
|
function getSocketStatus(): SocketStatus {
|
||||||
return socketManager.getStatus();
|
return socketManager.getStatus();
|
||||||
}
|
}
|
||||||
|
@ -1530,10 +1541,7 @@ export function initialize({
|
||||||
// Reset old websocket credentials and disconnect.
|
// Reset old websocket credentials and disconnect.
|
||||||
// AccountManager is our only caller and it will trigger
|
// AccountManager is our only caller and it will trigger
|
||||||
// `registration_done` which will update credentials.
|
// `registration_done` which will update credentials.
|
||||||
await socketManager.authenticate({
|
await logout();
|
||||||
username: '',
|
|
||||||
password: '',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update REST credentials, though. We need them for the call below
|
// Update REST credentials, though. We need them for the call below
|
||||||
username = number;
|
username = number;
|
||||||
|
|
|
@ -307,7 +307,7 @@ export default class WebSocketResource extends EventTarget {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (this.shuttingDown) {
|
if (this.shuttingDown) {
|
||||||
incomingRequest.respond(500, 'Shutting down');
|
incomingRequest.respond(-1, 'Shutting down');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,7 +343,7 @@ export default class WebSocketResource extends EventTarget {
|
||||||
|
|
||||||
for (const resolve of outgoing.values()) {
|
for (const resolve of outgoing.values()) {
|
||||||
resolve({
|
resolve({
|
||||||
status: 500,
|
status: -1,
|
||||||
message: 'Connection closed',
|
message: 'Connection closed',
|
||||||
response: undefined,
|
response: undefined,
|
||||||
headers: [],
|
headers: [],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue