Files

165 lines
5.1 KiB
JavaScript
Raw Permalink Normal View History

2026-06-20 04:47:34 +08:00
/**
* Cloudflare / Turnstile 挑战页检测
2026-07-01 01:26:27 +08:00
*
* antiBot.cfClearanceRequired === falseA2C 业务域):仅真实 CF 中间页算 blocked
* 不因页面引用 turnstile.js 或缺少 cf_clearance cookie 误判。
2026-06-20 04:47:34 +08:00
*/
2026-06-29 04:54:41 +08:00
/**
* URL 是否处于 CF Managed Challenge 中间态(Whatshub 短链常见 __cf_chl_rt_tk
* @param {string} url
*/
function urlIndicatesCfChallenge(url) {
if (!url || typeof url !== 'string') {
return false;
}
return url.includes('__cf_chl_rt_tk')
|| url.includes('__cf_chl_tk')
|| url.includes('__cf_chl_f_tk')
|| url.includes('/cdn-cgi/challenge-platform')
|| url.includes('/cdn-cgi/challenge');
}
2026-07-03 20:12:20 +08:00
/** CF 挑战态查询参数(CapSolver websiteURL 需剔除) */
const CF_CHALLENGE_QUERY_KEYS = [
'__cf_chl_rt_tk',
'__cf_chl_tk',
'__cf_chl_f_tk',
'__cf_chl_captcha_tk__',
];
/**
* 去掉 URL 中 CF 挑战临时参数,供 CapSolver / reload 使用
* @param {string} url
* @returns {string}
*/
function sanitizePageUrlForCfSolver(url) {
if (!url || typeof url !== 'string') {
return url || '';
}
try {
const parsed = new URL(url);
for (const key of CF_CHALLENGE_QUERY_KEYS) {
parsed.searchParams.delete(key);
}
return parsed.toString();
} catch (_) {
return url.split('?')[0] || url;
}
}
2026-07-01 01:26:27 +08:00
/**
* 是否要求 cf_clearance 才视为过盾完成(A2C user.a2c.chat 为 false
* @param {object|null|undefined} antiBot
*/
function isCfClearanceRequired(antiBot) {
return antiBot?.cfClearanceRequired !== false;
}
2026-06-20 04:47:34 +08:00
/**
* @typedef {Object} CloudflareState
* @property {boolean} blocked 是否处于 CF 挑战中
* @property {string|null} challengeType turnstile|js_challenge|unknown|null
* @property {boolean} hasCfClearance 是否已有 cf_clearance cookie
* @property {string} url 当前页面 URL
* @property {string} title 页面 title
*/
/**
* 检测页面是否被 Cloudflare 拦截
* @param {import('puppeteer').Page} page
2026-07-01 01:26:27 +08:00
* @param {object|null} [antiBot]
2026-06-20 04:47:34 +08:00
* @returns {Promise<CloudflareState>}
*/
2026-07-01 01:26:27 +08:00
async function detectCloudflare(page, antiBot = null) {
2026-06-20 04:47:34 +08:00
const url = page.url();
const title = await page.title().catch(() => '');
const cookies = await page.cookies().catch(() => []);
const hasCfClearance = cookies.some((c) => c.name === 'cf_clearance');
const domSignals = await page.evaluate(() => {
const hasTurnstileInput = !!document.querySelector('[name="cf-turnstile-response"]');
const hasTurnstileWidget = !!document.querySelector('.cf-turnstile, [data-sitekey]');
const hasChallengeRunning = !!document.querySelector('#challenge-running, #cf-challenge-running');
const titleText = document.title || '';
const bodyText = (document.body && document.body.innerText) ? document.body.innerText.slice(0, 500) : '';
return {
hasTurnstileInput,
hasTurnstileWidget,
hasChallengeRunning,
titleHasJustAMoment: titleText.includes('Just a moment'),
2026-07-03 20:12:20 +08:00
bodyHasJustAMoment: bodyText.includes('Just a moment')
|| bodyText.includes('Checking your browser')
|| bodyText.includes('Performing security verification'),
2026-06-20 04:47:34 +08:00
};
}).catch(() => ({
hasTurnstileInput: false,
hasTurnstileWidget: false,
hasChallengeRunning: false,
titleHasJustAMoment: false,
bodyHasJustAMoment: false,
}));
2026-06-29 04:54:41 +08:00
const urlChallenge = urlIndicatesCfChallenge(url);
2026-06-20 04:47:34 +08:00
2026-07-01 01:26:27 +08:00
const realChallenge = urlChallenge
|| domSignals.titleHasJustAMoment
|| domSignals.bodyHasJustAMoment
|| domSignals.hasChallengeRunning
2026-07-03 20:12:20 +08:00
|| title.includes('Just a moment')
|| title.includes('Performing security verification');
2026-07-01 01:26:27 +08:00
const turnstileSignals = domSignals.hasTurnstileInput || domSignals.hasTurnstileWidget;
2026-06-20 04:47:34 +08:00
let challengeType = null;
2026-07-01 01:26:27 +08:00
if (turnstileSignals || url.includes('__cf_chl')) {
2026-06-20 04:47:34 +08:00
challengeType = 'turnstile';
2026-07-01 01:26:27 +08:00
} else if (realChallenge) {
2026-06-20 04:47:34 +08:00
challengeType = 'js_challenge';
}
2026-07-01 01:26:27 +08:00
const requireClearance = isCfClearanceRequired(antiBot);
let blocked;
if (requireClearance) {
blocked = !hasCfClearance && (realChallenge || turnstileSignals);
} else {
2026-07-03 20:12:20 +08:00
blocked = realChallenge || (turnstileSignals && urlChallenge);
2026-07-01 01:26:27 +08:00
}
2026-06-20 04:47:34 +08:00
return {
blocked,
challengeType,
hasCfClearance,
url,
title,
};
}
/**
* 检测 Turnstile 是否已通过
* @param {import('puppeteer').Page} page
* @returns {Promise<boolean>}
*/
async function isTurnstileSolved(page) {
const cookies = await page.cookies().catch(() => []);
if (cookies.some((c) => c.name === 'cf_clearance')) {
return true;
}
const tokenLen = await page.evaluate(() => {
const input = document.querySelector('[name="cf-turnstile-response"]');
return input && input.value ? input.value.length : 0;
}).catch(() => 0);
return tokenLen > 20;
}
module.exports = {
detectCloudflare,
isTurnstileSolved,
2026-06-29 04:54:41 +08:00
urlIndicatesCfChallenge,
2026-07-01 01:26:27 +08:00
isCfClearanceRequired,
2026-07-03 20:12:20 +08:00
sanitizePageUrlForCfSolver,
2026-06-20 04:47:34 +08:00
};