Handle new Cloudflare challenge. Resolves #135 Resolves #134

This commit is contained in:
ngosang 2021-05-30 13:40:17 +02:00
parent 805a34c9d6
commit c6677f4d84
2 changed files with 19 additions and 5 deletions

View File

@ -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({

View File

@ -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> {
@ -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.')