feat: stealth-proxy 添加人类行为模拟,帮助通过 Vercel bot 检测

模拟鼠标移动轨迹、滚动、点击等行为,降低被风控拦截概率。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
BaskDuan
2026-04-02 23:57:55 +08:00
parent 7e0a01f14f
commit 941f8e234a
2 changed files with 59 additions and 4 deletions

View File

@@ -65,12 +65,16 @@ async function initBrowser() {
timeout: 60000,
});
// 模拟人类行为,帮助通过 bot 检测
await simulateHumanBehavior(challengePage);
// 等待 cookiechallenge JS 会异步设置 _vcrcs
const ok = await waitForCookie();
if (!ok) {
// 重试一次:刷新页面再等
// 重试:刷新页面 + 再次模拟行为
console.log('[Stealth] First attempt failed, retrying challenge...');
await challengePage.reload({ waitUntil: 'domcontentloaded', timeout: 60000 });
await simulateHumanBehavior(challengePage);
const retryOk = await waitForCookie();
if (!retryOk) {
throw new Error('Failed to obtain _vcrcs cookie after retry');
@@ -149,6 +153,38 @@ async function waitForCookie(maxWait) {
return false;
}
// 模拟人类行为:鼠标移动、点击、滚动,帮助通过 Vercel bot 检测
async function simulateHumanBehavior(page) {
try {
// 随机延迟
await new Promise(r => setTimeout(r, 500 + Math.random() * 1000));
// 模拟鼠标移动轨迹(多个随机点)
const points = Array.from({ length: 5 }, () => ({
x: 100 + Math.random() * 600,
y: 100 + Math.random() * 400,
}));
for (const p of points) {
await page.mouse.move(p.x, p.y, { steps: 5 + Math.floor(Math.random() * 10) });
await new Promise(r => setTimeout(r, 100 + Math.random() * 300));
}
// 模拟滚动
await page.mouse.wheel(0, 100 + Math.random() * 200);
await new Promise(r => setTimeout(r, 300 + Math.random() * 500));
await page.mouse.wheel(0, -(50 + Math.random() * 100));
// 模拟点击页面空白区域
await page.mouse.click(300 + Math.random() * 400, 300 + Math.random() * 200);
await new Promise(r => setTimeout(r, 200 + Math.random() * 500));
console.log('[Stealth] Human behavior simulation done');
} catch (e) {
// 模拟行为失败不影响主流程
console.log('[Stealth] Human simulation skipped:', e.message);
}
}
async function refreshChallenge() {
console.log('[Stealth] Refreshing challenge...');
try {

View File

@@ -4,6 +4,22 @@
*/
const CHALLENGE_URL = process.env.CHALLENGE_URL || 'https://cursor.com/cn/docs';
async function simulateHuman(page) {
await new Promise(r => setTimeout(r, 500 + Math.random() * 1000));
const points = Array.from({ length: 5 }, () => ({
x: 100 + Math.random() * 600,
y: 100 + Math.random() * 400,
}));
for (const p of points) {
await page.mouse.move(p.x, p.y, { steps: 5 + Math.floor(Math.random() * 10) });
await new Promise(r => setTimeout(r, 100 + Math.random() * 300));
}
await page.mouse.wheel(0, 100 + Math.random() * 200);
await new Promise(r => setTimeout(r, 300 + Math.random() * 500));
await page.mouse.click(300 + Math.random() * 400, 300 + Math.random() * 200);
console.log(`[Test] Human behavior simulation done`);
}
(async () => {
const { chromium } = require('playwright-extra');
const stealth = require('puppeteer-extra-plugin-stealth');
@@ -27,18 +43,21 @@ const CHALLENGE_URL = process.env.CHALLENGE_URL || 'https://cursor.com/cn/docs';
try {
await page.goto(CHALLENGE_URL, { waitUntil: 'domcontentloaded', timeout: 120000 });
console.log(`[Test] [${elapsed()}] Page loaded, waiting for _vcrcs cookie...`);
console.log(`[Test] [${elapsed()}] Page loaded, simulating human behavior...`);
} catch (e) {
console.error(`[Test] [${elapsed()}] Page load failed: ${e.message}`);
await browser.close();
process.exit(1);
}
await simulateHuman(page);
console.log(`[Test] [${elapsed()}] Waiting for _vcrcs cookie...`);
for (let i = 0; i < 60; i++) {
const cookies = await ctx.cookies();
const vcrcs = cookies.find(c => c.name === '_vcrcs');
if (vcrcs) {
console.log(`[Test] [${elapsed()}] Got _vcrcs cookie!`);
console.log(`[Test] [${elapsed()}] Got _vcrcs cookie!`);
console.log(`[Test] Value: ${vcrcs.value.substring(0, 50)}...`);
await browser.close();
process.exit(0);
@@ -46,7 +65,7 @@ const CHALLENGE_URL = process.env.CHALLENGE_URL || 'https://cursor.com/cn/docs';
await new Promise(r => setTimeout(r, 2000));
}
console.error(`[Test] [${elapsed()}] Failed to get _vcrcs cookie after 120s`);
console.error(`[Test] [${elapsed()}] Failed to get _vcrcs cookie after 120s`);
await browser.close();
process.exit(1);
})();