Skip to content

Commit

Permalink
Handle new Cloudflare challenge. Resolves #135 Resolves #134
Browse files Browse the repository at this point in the history
  • Loading branch information
ngosang committed May 30, 2021
1 parent 805a34c commit c6677f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -77,7 +77,7 @@ function errorResponse(errorMsg: string, res: ServerResponse, startTimestamp: nu

function successResponse(successMsg: string, extendedProperties: object, res: ServerResponse, startTimestamp: number) {
const endTimestamp = Date.now()
log.info(`Successful response in ${(endTimestamp - startTimestamp) / 1000} s`)
log.info(`Response in ${(endTimestamp - startTimestamp) / 1000} s`)
if (successMsg) { log.info(successMsg) }

const response = Object.assign({
Expand Down
22 changes: 18 additions & 4 deletions src/providers/cloudflare.ts
Expand Up @@ -8,7 +8,7 @@ import getCaptchaSolver, {CaptchaType} from "../captcha";
* This class contains the logic to solve protections provided by CloudFlare
**/

const CHALLENGE_SELECTORS = ['#trk_jschal_js', '.ray_id', '.attack-box'];
const CHALLENGE_SELECTORS = ['#trk_jschal_js', '.ray_id', '.attack-box', '#cf-please-wait'];
const TOKEN_INPUT_NAMES = ['g-recaptcha-response', 'h-captcha-response'];

export default async function resolveChallenge(url: string, page: Page, response: Response): Promise<Response> {
Expand Down Expand Up @@ -38,16 +38,30 @@ export default async function resolveChallenge(url: string, page: Page, response
await page.waitFor(1000)
try {
// catch exception timeout in waitForNavigation
response = await page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 5000 })
response = await page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 9000 })
} catch (error) { }

try {
// catch Execution context was destroyed
const cfChallengeElem = await page.$(selector)
if (!cfChallengeElem) { break }
if (!cfChallengeElem) {
// solved!
break
} else {
const displayStyle = await page.evaluate((selector) => {
return getComputedStyle(document.querySelector(selector)).getPropertyValue("display");
}, selector);
if (displayStyle == "none") {
// spinner is hidden, could be a captcha or not
await page.waitFor(1000)
break
}
}
log.debug('Found challenge element again...')
} catch (error)
{ }
{
log.debug("Unexpected error: " + error);
}

response = await page.reload({ waitUntil: 'domcontentloaded' })
log.debug('Page reloaded.')
Expand Down

0 comments on commit c6677f4

Please sign in to comment.