feat(lookup): add protection against infinite recursion for Cloudflare (#1505)

Fixes #1459
Fixes #1490
This commit is contained in:
neatchee
2020-12-23 22:23:43 -08:00
committed by GitHub
parent 28d1cf90b8
commit 1cf618c1c1
2 changed files with 35 additions and 11 deletions
+15
View File
@@ -212,6 +212,21 @@ export const Print = {
} }
return `${buildProductString(link, store)} :: RATE LIMIT EXCEEDED`; 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`;
} }
}; };
+13 -4
View File
@@ -328,7 +328,8 @@ async function handleResponse(
store: Store, store: Store,
page: Page, page: Page,
link: Link, link: Link,
response?: Response | null response?: Response | null,
recursionDepth = 0
) { ) {
if (!response) { if (!response) {
logger.debug(Print.noResponse(link, store, true)); logger.debug(Print.noResponse(link, store, true));
@@ -341,16 +342,24 @@ async function handleResponse(
logger.warn(Print.rateLimit(link, store, true)); logger.warn(Print.rateLimit(link, store, true));
} else if (statusCode === 503) { } else if (statusCode === 503) {
if (await checkIsCloudflare(store, page, link)) { if (await checkIsCloudflare(store, page, link)) {
const response: Response | null = await page.waitForNavigation({ if (recursionDepth > 4) {
logger.warn(Print.recursionLimit(link, store, true));
} else {
const response: Response | null = await page.waitForNavigation(
{
waitUntil: 'networkidle0' waitUntil: 'networkidle0'
}); }
);
recursionDepth++;
statusCode = await handleResponse( statusCode = await handleResponse(
browser, browser,
store, store,
page, page,
link, link,
response response,
recursionDepth
); );
}
} else { } else {
logger.warn(Print.badStatusCode(link, store, statusCode, true)); logger.warn(Print.badStatusCode(link, store, statusCode, true));
} }