2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-05-06 00:09:29 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
// `ChallengeHandler` is responsible for:
|
|
|
|
// 1. tracking the messages that failed to send with 428 error and could be
|
|
|
|
// retried when user solves the challenge
|
|
|
|
// 2. presenting the challenge to user and sending the challenge response back
|
|
|
|
// to the server
|
|
|
|
//
|
|
|
|
// The tracked messages are persisted in the database, and are imported back
|
|
|
|
// to the `ChallengeHandler` on `.load()` call (from `ts/background.ts`). They
|
|
|
|
// are not immediately retried, however, until `.onOnline()` is called from
|
|
|
|
// when we are actually online.
|
|
|
|
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from './util/assert';
|
2021-05-06 00:09:29 +00:00
|
|
|
import { isOlderThan } from './util/timestamp';
|
2022-04-25 23:05:23 +00:00
|
|
|
import { parseRetryAfterWithDefault } from './util/parseRetryAfter';
|
2022-02-25 18:37:15 +00:00
|
|
|
import { clearTimeoutIfNecessary } from './util/clearTimeoutIfNecessary';
|
2023-04-06 21:49:24 +00:00
|
|
|
import { missingCaseError } from './util/missingCaseError';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StorageInterface } from './types/Storage.d';
|
2022-09-15 00:31:37 +00:00
|
|
|
import * as Errors from './types/errors';
|
2021-09-22 00:58:03 +00:00
|
|
|
import { HTTPError } from './textsecure/Errors';
|
2022-03-21 21:19:37 +00:00
|
|
|
import type { SendMessageChallengeData } from './textsecure/Errors';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from './logging/log';
|
2023-10-28 00:14:35 +00:00
|
|
|
import { drop } from './util/drop';
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
export type ChallengeResponse = Readonly<{
|
|
|
|
captcha: string;
|
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
export type IPCRequest = Readonly<{
|
|
|
|
seq: number;
|
|
|
|
reason: string;
|
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
export type IPCResponse = Readonly<{
|
|
|
|
seq: number;
|
|
|
|
data: ChallengeResponse;
|
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
type Handler = Readonly<{
|
|
|
|
token: string | undefined;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
resolve(response: ChallengeResponse): void;
|
|
|
|
reject(error: Error): void;
|
2022-09-15 00:31:37 +00:00
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
export type ChallengeData = Readonly<{
|
|
|
|
type: 'recaptcha';
|
|
|
|
token: string;
|
|
|
|
captcha: string;
|
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
export type Options = Readonly<{
|
|
|
|
storage: Pick<StorageInterface, 'get' | 'put'>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
requestChallenge(request: IPCRequest): void;
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
startQueue(conversationId: string): void;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
sendChallengeResponse(data: ChallengeData): Promise<void>;
|
|
|
|
|
|
|
|
setChallengeStatus(challengeStatus: 'idle' | 'required' | 'pending'): void;
|
|
|
|
|
|
|
|
onChallengeSolved(): void;
|
|
|
|
onChallengeFailed(retryAfter?: number): void;
|
|
|
|
|
|
|
|
expireAfter?: number;
|
2022-09-15 00:31:37 +00:00
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
export const STORAGE_KEY = 'challenge:conversations';
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
export type RegisteredChallengeType = Readonly<{
|
|
|
|
conversationId: string;
|
|
|
|
createdAt: number;
|
2022-09-15 00:31:37 +00:00
|
|
|
reason: string;
|
2022-04-25 23:05:23 +00:00
|
|
|
retryAt?: number;
|
2022-03-21 21:19:37 +00:00
|
|
|
token?: string;
|
2023-10-28 00:14:35 +00:00
|
|
|
silent: boolean;
|
2022-03-21 21:19:37 +00:00
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
type SolveOptionsType = Readonly<{
|
|
|
|
token: string;
|
|
|
|
reason: string;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type MaybeSolveOptionsType = Readonly<{
|
|
|
|
conversationId: string;
|
|
|
|
reason: string;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type RequestCaptchaOptionsType = Readonly<{
|
|
|
|
reason: string;
|
|
|
|
token?: string;
|
|
|
|
}>;
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
const DEFAULT_EXPIRE_AFTER = 24 * 3600 * 1000; // one day
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
function shouldStartQueue(registered: RegisteredChallengeType): boolean {
|
2022-04-25 23:05:23 +00:00
|
|
|
// No retryAt provided; waiting for user to complete captcha
|
|
|
|
if (!registered.retryAt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (registered.retryAt <= Date.now()) {
|
2021-05-06 00:09:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:49:24 +00:00
|
|
|
export function getChallengeURL(type: 'chat' | 'registration'): string {
|
|
|
|
if (type === 'chat') {
|
|
|
|
return window.SignalContext.config.challengeUrl;
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
2023-04-06 21:49:24 +00:00
|
|
|
if (type === 'registration') {
|
|
|
|
return window.SignalContext.config.registrationChallengeUrl;
|
|
|
|
}
|
|
|
|
throw missingCaseError(type);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that even though this is a class - only one instance of
|
|
|
|
// `ChallengeHandler` should be in memory at the same time because they could
|
|
|
|
// overwrite each others storage data.
|
|
|
|
export class ChallengeHandler {
|
2022-03-21 21:19:37 +00:00
|
|
|
private solving = 0;
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
private isLoaded = false;
|
|
|
|
|
|
|
|
private challengeToken: string | undefined;
|
|
|
|
|
|
|
|
private seq = 0;
|
|
|
|
|
|
|
|
private isOnline = false;
|
|
|
|
|
|
|
|
private readonly responseHandlers = new Map<number, Handler>();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
private readonly registeredConversations = new Map<
|
|
|
|
string,
|
|
|
|
RegisteredChallengeType
|
|
|
|
>();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
private readonly startTimers = new Map<string, NodeJS.Timeout>();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
private readonly pendingStarts = new Set<string>();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
constructor(private readonly options: Options) {}
|
|
|
|
|
|
|
|
public async load(): Promise<void> {
|
|
|
|
if (this.isLoaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isLoaded = true;
|
2022-03-21 21:19:37 +00:00
|
|
|
const challenges: ReadonlyArray<RegisteredChallengeType> =
|
|
|
|
this.options.storage.get(STORAGE_KEY) || [];
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
log.info(`challenge: loading ${challenges.length} challenges`);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
2022-03-21 21:19:37 +00:00
|
|
|
challenges.map(async challenge => {
|
2021-05-06 00:09:29 +00:00
|
|
|
const expireAfter = this.options.expireAfter || DEFAULT_EXPIRE_AFTER;
|
2022-03-21 21:19:37 +00:00
|
|
|
if (isOlderThan(challenge.createdAt, expireAfter)) {
|
|
|
|
log.info(
|
|
|
|
`challenge: expired challenge for conversation ${challenge.conversationId}`
|
|
|
|
);
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The initialization order is following:
|
|
|
|
//
|
|
|
|
// 1. `.load()` when the `window.storage` is ready
|
|
|
|
// 2. `.onOnline()` when we connected to the server
|
|
|
|
//
|
|
|
|
// Wait for `.onOnline()` to trigger the retries instead of triggering
|
|
|
|
// them here immediately (if the message is ready to be retried).
|
2022-03-21 21:19:37 +00:00
|
|
|
await this.register(challenge);
|
2021-05-06 00:09:29 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async onOffline(): Promise<void> {
|
|
|
|
this.isOnline = false;
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('challenge: offline');
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async onOnline(): Promise<void> {
|
|
|
|
this.isOnline = true;
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const pending = Array.from(this.pendingStarts.values());
|
|
|
|
this.pendingStarts.clear();
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
log.info(`challenge: online, starting ${pending.length} queues`);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
// Start queues for challenges that matured while we were offline
|
|
|
|
await Promise.all(
|
|
|
|
pending.map(conversationId => this.startQueue(conversationId))
|
|
|
|
);
|
|
|
|
|
|
|
|
await this.startAllQueues();
|
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
public maybeSolve({ conversationId, reason }: MaybeSolveOptionsType): void {
|
2022-03-21 21:19:37 +00:00
|
|
|
const challenge = this.registeredConversations.get(conversationId);
|
|
|
|
if (!challenge) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.solving > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (challenge.token) {
|
2023-10-28 00:14:35 +00:00
|
|
|
drop(this.solve({ reason, token: challenge.token }));
|
2022-03-21 21:19:37 +00:00
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async register(
|
2022-03-21 21:19:37 +00:00
|
|
|
challenge: RegisteredChallengeType,
|
|
|
|
data?: SendMessageChallengeData
|
2021-05-06 00:09:29 +00:00
|
|
|
): Promise<void> {
|
2022-09-15 00:31:37 +00:00
|
|
|
const { conversationId, reason } = challenge;
|
|
|
|
const logId = `challenge(${reason})`;
|
2022-03-21 21:19:37 +00:00
|
|
|
|
|
|
|
if (this.isRegistered(conversationId)) {
|
2022-09-15 00:31:37 +00:00
|
|
|
log.info(`${logId}: conversation ${conversationId} already registered`);
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
this.registeredConversations.set(conversationId, challenge);
|
2021-05-06 00:09:29 +00:00
|
|
|
await this.persist();
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
// Challenge is already retryable - start the queue
|
|
|
|
if (shouldStartQueue(challenge)) {
|
2022-09-15 00:31:37 +00:00
|
|
|
log.info(`${logId}: starting conversation ${conversationId} immediately`);
|
2022-03-21 21:19:37 +00:00
|
|
|
await this.startQueue(conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
if (challenge.retryAt) {
|
|
|
|
const waitTime = Math.max(0, challenge.retryAt - Date.now());
|
|
|
|
const oldTimer = this.startTimers.get(conversationId);
|
|
|
|
if (oldTimer) {
|
|
|
|
clearTimeoutIfNecessary(oldTimer);
|
|
|
|
}
|
|
|
|
this.startTimers.set(
|
|
|
|
conversationId,
|
|
|
|
setTimeout(() => {
|
|
|
|
this.startTimers.delete(conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2023-10-28 00:14:35 +00:00
|
|
|
drop(this.startQueue(conversationId));
|
2022-04-25 23:05:23 +00:00
|
|
|
}, waitTime)
|
|
|
|
);
|
|
|
|
log.info(
|
2022-09-15 00:31:37 +00:00
|
|
|
`${logId}: tracking ${conversationId} with waitTime=${waitTime}`
|
2022-04-25 23:05:23 +00:00
|
|
|
);
|
|
|
|
} else {
|
2022-09-15 00:31:37 +00:00
|
|
|
log.info(`${logId}: tracking ${conversationId} with no waitTime`);
|
2022-04-25 23:05:23 +00:00
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
if (data && !data.options?.includes('recaptcha')) {
|
2022-09-15 00:31:37 +00:00
|
|
|
log.error(`${logId}: unexpected options ${JSON.stringify(data.options)}`);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
if (!challenge.token) {
|
|
|
|
const dataString = JSON.stringify(data);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-09-15 00:31:37 +00:00
|
|
|
`${logId}: ${conversationId} is waiting; no token in data ${dataString}`
|
2021-05-06 00:09:29 +00:00
|
|
|
);
|
2022-03-21 21:19:37 +00:00
|
|
|
return;
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
2022-03-21 21:19:37 +00:00
|
|
|
|
2023-10-28 00:14:35 +00:00
|
|
|
if (!challenge.silent) {
|
|
|
|
drop(this.solve({ token: challenge.token, reason }));
|
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public onResponse(response: IPCResponse): void {
|
|
|
|
const handler = this.responseHandlers.get(response.seq);
|
|
|
|
if (!handler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.responseHandlers.delete(response.seq);
|
|
|
|
handler.resolve(response.data);
|
|
|
|
}
|
|
|
|
|
2023-10-28 00:14:35 +00:00
|
|
|
public async unregister(
|
|
|
|
conversationId: string,
|
|
|
|
source: string
|
|
|
|
): Promise<void> {
|
|
|
|
log.info(
|
|
|
|
`challenge: unregistered conversation ${conversationId} via ${source}`
|
|
|
|
);
|
2022-03-21 21:19:37 +00:00
|
|
|
this.registeredConversations.delete(conversationId);
|
|
|
|
this.pendingStarts.delete(conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
const timer = this.startTimers.get(conversationId);
|
|
|
|
this.startTimers.delete(conversationId);
|
2022-02-25 18:37:15 +00:00
|
|
|
clearTimeoutIfNecessary(timer);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
await this.persist();
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
public async requestCaptcha({
|
|
|
|
reason,
|
|
|
|
token = '',
|
|
|
|
}: RequestCaptchaOptionsType): Promise<string> {
|
|
|
|
const request: IPCRequest = { seq: this.seq, reason };
|
2021-11-30 17:51:53 +00:00
|
|
|
this.seq += 1;
|
|
|
|
|
|
|
|
this.options.requestChallenge(request);
|
|
|
|
|
|
|
|
const response = await new Promise<ChallengeResponse>((resolve, reject) => {
|
|
|
|
this.responseHandlers.set(request.seq, { token, resolve, reject });
|
|
|
|
});
|
|
|
|
|
|
|
|
return response.captcha;
|
|
|
|
}
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
private async persist(): Promise<void> {
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-05-06 00:09:29 +00:00
|
|
|
this.isLoaded,
|
|
|
|
'ChallengeHandler has to be loaded before persisting new data'
|
|
|
|
);
|
|
|
|
await this.options.storage.put(
|
2022-03-21 21:19:37 +00:00
|
|
|
STORAGE_KEY,
|
|
|
|
Array.from(this.registeredConversations.values())
|
2021-05-06 00:09:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
public isRegistered(conversationId: string): boolean {
|
|
|
|
return this.registeredConversations.has(conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
private startAllQueues({
|
|
|
|
force = false,
|
|
|
|
}: {
|
|
|
|
force?: boolean;
|
|
|
|
} = {}): void {
|
|
|
|
log.info(`challenge: startAllQueues force=${force}`);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
Array.from(this.registeredConversations.values())
|
|
|
|
.filter(challenge => force || shouldStartQueue(challenge))
|
|
|
|
.forEach(challenge => this.startQueue(challenge.conversationId));
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
private async startQueue(conversationId: string): Promise<void> {
|
2021-05-06 00:09:29 +00:00
|
|
|
if (!this.isOnline) {
|
2022-03-21 21:19:37 +00:00
|
|
|
this.pendingStarts.add(conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-28 00:14:35 +00:00
|
|
|
await this.unregister(conversationId, 'startQueue');
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
if (this.registeredConversations.size === 0) {
|
|
|
|
this.options.setChallengeStatus('idle');
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
log.info(`startQueue: starting queue ${conversationId}`);
|
|
|
|
this.options.startQueue(conversationId);
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
private async solve({ reason, token }: SolveOptionsType): Promise<void> {
|
2022-03-21 21:19:37 +00:00
|
|
|
this.solving += 1;
|
2021-05-06 00:09:29 +00:00
|
|
|
this.options.setChallengeStatus('required');
|
2021-11-30 17:51:53 +00:00
|
|
|
this.challengeToken = token;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
const captcha = await this.requestCaptcha({ reason, token });
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
// Another `.solve()` has completed earlier than us
|
|
|
|
if (this.challengeToken === undefined) {
|
2022-03-21 21:19:37 +00:00
|
|
|
this.solving -= 1;
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastToken = this.challengeToken;
|
|
|
|
this.challengeToken = undefined;
|
|
|
|
|
|
|
|
this.options.setChallengeStatus('pending');
|
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
log.info(`challenge(${reason}): sending challenge to server`);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await this.sendChallengeResponse({
|
|
|
|
type: 'recaptcha',
|
|
|
|
token: lastToken,
|
2021-11-30 17:51:53 +00:00
|
|
|
captcha,
|
2021-05-06 00:09:29 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2022-09-15 00:31:37 +00:00
|
|
|
log.error(
|
|
|
|
`challenge(${reason}): challenge failure, error:`,
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
2021-05-06 00:09:29 +00:00
|
|
|
this.options.setChallengeStatus('required');
|
2022-03-21 21:19:37 +00:00
|
|
|
this.solving -= 1;
|
2021-05-06 00:09:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
log.info(`challenge(${reason}): challenge success. force sending`);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
this.options.setChallengeStatus('idle');
|
|
|
|
|
2022-03-21 21:19:37 +00:00
|
|
|
this.startAllQueues({ force: true });
|
|
|
|
this.solving -= 1;
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async sendChallengeResponse(data: ChallengeData): Promise<void> {
|
|
|
|
try {
|
|
|
|
await this.options.sendChallengeResponse(data);
|
|
|
|
} catch (error) {
|
|
|
|
if (
|
2021-09-22 00:58:03 +00:00
|
|
|
!(error instanceof HTTPError) ||
|
2022-02-25 00:26:58 +00:00
|
|
|
!(error.code === 413 || error.code === 429) ||
|
2021-05-06 00:09:29 +00:00
|
|
|
!error.responseHeaders
|
|
|
|
) {
|
|
|
|
this.options.onChallengeFailed();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
const retryAfter = parseRetryAfterWithDefault(
|
|
|
|
error.responseHeaders['retry-after']
|
|
|
|
);
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`challenge: retry after ${retryAfter}ms`);
|
2021-05-06 00:09:29 +00:00
|
|
|
this.options.onChallengeFailed(retryAfter);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.options.onChallengeSolved();
|
|
|
|
}
|
|
|
|
}
|