fix: remove stealth plugin to eliminate rimraf crash, use plain puppeteer
This commit is contained in:
1 parent
7137b046d7
commit
cef068ff3c
1 file changed
+23
-32
@@ -1,44 +1,37 @@
|
|||||||
|
import puppeteer from "puppeteer";
|
||||||
|
|
||||||
const UA =
|
const UA =
|
||||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
|
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
|
||||||
"AppleWebKit/537.36 (KHTML, like Gecko) " +
|
"AppleWebKit/537.36 (KHTML, like Gecko) " +
|
||||||
"Chrome/125.0.0.0 Safari/537.36";
|
"Chrome/125.0.0.0 Safari/537.36";
|
||||||
|
|
||||||
let browser: any = null;
|
let browser: Awaited<ReturnType<typeof puppeteer.launch>> | null = null;
|
||||||
|
|
||||||
async function getBrowser() {
|
async function getBrowser() {
|
||||||
if (browser) return browser;
|
if (browser) return browser;
|
||||||
try {
|
browser = await puppeteer.launch({
|
||||||
const puppeteer = await import("puppeteer-extra");
|
headless: true,
|
||||||
const StealthPlugin = await import("puppeteer-extra-plugin-stealth");
|
args: [
|
||||||
puppeteer.default.use(StealthPlugin.default());
|
"--no-sandbox",
|
||||||
browser = await puppeteer.default.launch({
|
"--disable-setuid-sandbox",
|
||||||
headless: true,
|
"--disable-web-security",
|
||||||
args: [
|
"--disable-blink-features=AutomationControlled",
|
||||||
"--no-sandbox",
|
"--user-agent=" + UA,
|
||||||
"--disable-setuid-sandbox",
|
],
|
||||||
"--disable-web-security",
|
});
|
||||||
"--disable-blink-features=AutomationControlled",
|
return browser;
|
||||||
"--user-agent=" + UA,
|
|
||||||
],
|
|
||||||
});
|
|
||||||
return browser;
|
|
||||||
} catch (e) {
|
|
||||||
throw new Error(`Cloudflare bypass unavailable: ${(e as Error).message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchWithCfBypass(
|
export async function fetchWithCfBypass(
|
||||||
url: string,
|
url: string,
|
||||||
timeout = 30000,
|
timeout = 30000,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
let b: any;
|
const b = await getBrowser();
|
||||||
let page: any;
|
const page = await b.newPage();
|
||||||
try {
|
await page.setUserAgent(UA);
|
||||||
b = await getBrowser();
|
await page.setDefaultTimeout(timeout);
|
||||||
page = await b.newPage();
|
|
||||||
await page.setUserAgent(UA);
|
|
||||||
await page.setDefaultTimeout(timeout);
|
|
||||||
|
|
||||||
|
try {
|
||||||
const response = await page.goto(url, {
|
const response = await page.goto(url, {
|
||||||
waitUntil: "networkidle2",
|
waitUntil: "networkidle2",
|
||||||
timeout,
|
timeout,
|
||||||
@@ -72,12 +65,10 @@ export async function fetchWithCfBypass(
|
|||||||
const finalBody = await page.content();
|
const finalBody = await page.content();
|
||||||
return finalBody;
|
return finalBody;
|
||||||
} finally {
|
} finally {
|
||||||
if (page) {
|
try {
|
||||||
try {
|
await page.close();
|
||||||
await page.close();
|
} catch {
|
||||||
} catch {
|
// ignore cleanup errors
|
||||||
// ignore cleanup errors
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user