From 23d627301346a09aa3e27c0e3b42f8c4b5fc8c7c Mon Sep 17 00:00:00 2001 From: neatchee Date: Wed, 23 Dec 2020 15:57:58 -0800 Subject: [PATCH 1/2] feat(lookup): Add protection against infinite recursion if Cloudflare never moves past its DDoS protection page --- src/logger.ts | 12 ++++++++++++ src/store/lookup.ts | 29 ++++++++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index b04dffb6ab..c8566d3f16 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -107,6 +107,18 @@ export const Print = { return `✖ ${buildProductString(link, store)} :: CLOUDFLARE, WAITING`; }, + recursionLimit(link: Link, store: Store, color?: boolean): string { + if (color) { + return ( + '✖ ' + + buildProductString(link, store, true) + + ' :: ' + + chalk.yellow('CLOUDFLARE RETRY LIMIT REACHED, ABORT') + ); + } + + return `✖ ${buildProductString(link, store)} :: CLOUDFLARE RETRY LIMIT REACHED, ABORT`; + }, inStock(link: Link, store: Store, color?: boolean, sms?: boolean): string { const productString = `${buildProductString(link, store)} :: IN STOCK`; diff --git a/src/store/lookup.ts b/src/store/lookup.ts index 197986af8d..bbba65a13b 100644 --- a/src/store/lookup.ts +++ b/src/store/lookup.ts @@ -328,7 +328,8 @@ async function handleResponse( store: Store, page: Page, link: Link, - response?: Response | null + response?: Response | null, + recursionDepth: number = 0 ) { if (!response) { logger.debug(Print.noResponse(link, store, true)); @@ -341,16 +342,22 @@ async function handleResponse( logger.warn(Print.rateLimit(link, store, true)); } else if (statusCode === 503) { if (await checkIsCloudflare(store, page, link)) { - const response: Response | null = await page.waitForNavigation({ - waitUntil: 'networkidle0' - }); - statusCode = await handleResponse( - browser, - store, - page, - link, - response - ); + if (recursionDepth > 4) { + logger.warn(Print.recursionLimit(link, store, true)) + } else { + const response: Response | null = await page.waitForNavigation({ + waitUntil: 'networkidle0' + }); + recursionDepth++; + statusCode = await handleResponse( + browser, + store, + page, + link, + response, + recursionDepth + ); + } } else { logger.warn(Print.badStatusCode(link, store, statusCode, true)); } From 638573514a5a3c3d01636595f1c74ce8fafea013 Mon Sep 17 00:00:00 2001 From: neatchee Date: Wed, 23 Dec 2020 16:28:39 -0800 Subject: [PATCH 2/2] Make the Cloudflare recursion protection prettier --- src/logger.ts | 27 +++++++++++++++------------ src/store/lookup.ts | 12 +++++++----- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/logger.ts b/src/logger.ts index c8566d3f16..bb0eaded2e 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -107,18 +107,6 @@ export const Print = { return `✖ ${buildProductString(link, store)} :: CLOUDFLARE, WAITING`; }, - recursionLimit(link: Link, store: Store, color?: boolean): string { - if (color) { - return ( - '✖ ' + - buildProductString(link, store, true) + - ' :: ' + - chalk.yellow('CLOUDFLARE RETRY LIMIT REACHED, ABORT') - ); - } - - return `✖ ${buildProductString(link, store)} :: CLOUDFLARE RETRY LIMIT REACHED, ABORT`; - }, inStock(link: Link, store: Store, color?: boolean, sms?: boolean): string { const productString = `${buildProductString(link, store)} :: IN STOCK`; @@ -224,6 +212,21 @@ export const Print = { } return `✖ ${buildProductString(link, store)} :: RATE LIMIT EXCEEDED`; + }, + recursionLimit(link: Link, store: Store, color?: boolean): string { + if (color) { + return ( + '✖ ' + + buildProductString(link, store, true) + + ' :: ' + + chalk.yellow('CLOUDFLARE RETRY LIMIT REACHED, ABORT') + ); + } + + return `✖ ${buildProductString( + link, + store + )} :: CLOUDFLARE RETRY LIMIT REACHED, ABORT`; } }; diff --git a/src/store/lookup.ts b/src/store/lookup.ts index bbba65a13b..ab53f76400 100644 --- a/src/store/lookup.ts +++ b/src/store/lookup.ts @@ -329,7 +329,7 @@ async function handleResponse( page: Page, link: Link, response?: Response | null, - recursionDepth: number = 0 + recursionDepth = 0 ) { if (!response) { logger.debug(Print.noResponse(link, store, true)); @@ -343,11 +343,13 @@ async function handleResponse( } else if (statusCode === 503) { if (await checkIsCloudflare(store, page, link)) { if (recursionDepth > 4) { - logger.warn(Print.recursionLimit(link, store, true)) + logger.warn(Print.recursionLimit(link, store, true)); } else { - const response: Response | null = await page.waitForNavigation({ - waitUntil: 'networkidle0' - }); + const response: Response | null = await page.waitForNavigation( + { + waitUntil: 'networkidle0' + } + ); recursionDepth++; statusCode = await handleResponse( browser,