2021-05-06 00:09:29 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2021-08-26 18:18:00 +00:00
|
|
|
/* eslint-disable no-console */
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
import { ipcMain as ipc, IpcMainEvent } from 'electron';
|
|
|
|
|
|
|
|
import { IPCRequest, IPCResponse, ChallengeResponse } from '../challenge';
|
|
|
|
|
|
|
|
export class ChallengeMainHandler {
|
|
|
|
private handlers: Array<(response: ChallengeResponse) => void> = [];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
public handleCaptcha(captcha: string): void {
|
|
|
|
const response: ChallengeResponse = { captcha };
|
|
|
|
|
|
|
|
const { handlers } = this;
|
|
|
|
this.handlers = [];
|
|
|
|
for (const resolve of handlers) {
|
|
|
|
resolve(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async onRequest(
|
|
|
|
event: IpcMainEvent,
|
|
|
|
request: IPCRequest
|
|
|
|
): Promise<void> {
|
|
|
|
console.log('Received challenge request, waiting for response');
|
|
|
|
|
|
|
|
const data = await new Promise<ChallengeResponse>(resolve => {
|
|
|
|
this.handlers.push(resolve);
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('Sending challenge response', data);
|
|
|
|
|
|
|
|
const ipcResponse: IPCResponse = {
|
|
|
|
seq: request.seq,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
event.sender.send('challenge:response', ipcResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
private initialize(): void {
|
|
|
|
ipc.on('challenge:request', (event, request) => {
|
|
|
|
this.onRequest(event, request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|