Skip to content

Commit

Permalink
feat(lookup): handle Cloudflare DDoS protection (#1434)
Browse files Browse the repository at this point in the history
Resolves #1297
  • Loading branch information
neatchee committed Dec 17, 2020
1 parent 2b5588b commit f86a825
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ export const Print = {

return `✖ ${buildProductString(link, store)} :: CAPTCHA`;
},
cloudflare(link: Link, store: Store, color?: boolean): string {
if (color) {
return (
'✖ ' +
buildProductString(link, store, true) +
' :: ' +
chalk.yellow('CLOUDFLARE, WAITING')
);
}

return `✖ ${buildProductString(link, store)} :: CLOUDFLARE, WAITING`;
},
inStock(link: Link, store: Store, color?: boolean, sms?: boolean): string {
const productString = `${buildProductString(link, store)} :: IN STOCK`;

Expand Down
78 changes: 67 additions & 11 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,19 +275,16 @@ async function lookupCard(
waitUntil: givenWaitFor
});

if (!response) {
logger.debug(Print.noResponse(link, store, true));
}

const successStatusCodes = store.successStatusCodes ?? [[0, 399]];
const statusCode = response?.status() ?? 0;
if (!isStatusCodeInRange(statusCode, successStatusCodes)) {
if (statusCode === 429) {
logger.warn(Print.rateLimit(link, store, true));
} else {
logger.warn(Print.badStatusCode(link, store, statusCode, true));
}
const statusCode = await handleResponse(
browser,
store,
page,
link,
response
);

if (!isStatusCodeInRange(statusCode, successStatusCodes)) {
return statusCode;
}

Expand Down Expand Up @@ -325,6 +322,65 @@ async function lookupCard(
return statusCode;
}

async function handleResponse(
browser: Browser,
store: Store,
page: Page,
link: Link,
response?: Response | null
) {
if (!response) {
logger.debug(Print.noResponse(link, store, true));
}

const successStatusCodes = store.successStatusCodes ?? [[0, 399]];
let statusCode = response?.status() ?? 0;
if (!isStatusCodeInRange(statusCode, successStatusCodes)) {
if (statusCode === 429) {
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
);
} else {
logger.warn(Print.badStatusCode(link, store, statusCode, true));
}
} else {
logger.warn(Print.badStatusCode(link, store, statusCode, true));
}
}

return statusCode;
}

async function checkIsCloudflare(store: Store, page: Page, link: Link) {
const baseOptions: Selector = {
requireVisible: true,
selector: 'body',
type: 'textContent'
};

const cloudflareLabel = {
container: 'div[class="attribution"] a[rel="noopener noreferrer"]',
text: ['Cloudflare']
};

if (await pageIncludesLabels(page, cloudflareLabel, baseOptions)) {
logger.warn(Print.cloudflare(link, store, true));
return true;
}

return false;
}

async function lookupCardInStock(store: Store, page: Page, link: Link) {
const baseOptions: Selector = {
requireVisible: false,
Expand Down

0 comments on commit f86a825

Please sign in to comment.