Update index.js
Browse files
index.js
CHANGED
|
@@ -43,74 +43,83 @@ async function createBrowser(proxy = null) {
|
|
| 43 |
return { browser, page };
|
| 44 |
}
|
| 45 |
|
| 46 |
-
async function
|
| 47 |
return new Promise(async (resolve, reject) => {
|
| 48 |
-
const timeout = setTimeout(() => reject(new Error("Timeout
|
|
|
|
| 49 |
try {
|
| 50 |
-
|
| 51 |
-
await page.setRequestInterception(true);
|
| 52 |
-
page.once('request', request => request.respond({ status: 200, contentType: 'text/html', body: htmlContent }));
|
| 53 |
-
page.on('request', req => { if (req.isInterceptResolutionHandled()) return; req.continue(); });
|
| 54 |
-
|
| 55 |
-
console.log(`[Turnstile] Navigating to ${url}`);
|
| 56 |
-
await page.goto(url, { waitUntil: "domcontentloaded" });
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
try {
|
| 60 |
-
const
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
const timeout = setTimeout(() => reject(new Error("Timeout Cloudflare Bypass")), 60000);
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 60000 });
|
| 75 |
|
| 76 |
const loop = setInterval(async () => {
|
| 77 |
try {
|
| 78 |
const title = await page.title();
|
| 79 |
const content = await page.content();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
const isChallengePage = title.includes("Just a moment") ||
|
| 82 |
content.includes("challenge-platform") ||
|
| 83 |
title.includes("Cloudflare");
|
| 84 |
|
| 85 |
-
if (!isChallengePage) {
|
| 86 |
clearInterval(loop);
|
| 87 |
clearTimeout(timeout);
|
| 88 |
|
| 89 |
-
const cookies = await page.cookies();
|
| 90 |
const ua = await page.evaluate(() => navigator.userAgent);
|
| 91 |
-
const
|
| 92 |
|
| 93 |
-
console.log(`[Cloudflare] Success! (
|
| 94 |
|
| 95 |
resolve({
|
| 96 |
status: "success",
|
| 97 |
userAgent: ua,
|
| 98 |
cookies_full: cookies,
|
| 99 |
-
cookie:
|
| 100 |
title: title
|
| 101 |
});
|
| 102 |
return;
|
| 103 |
}
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
try {
|
| 106 |
-
await page.mouse.move(
|
| 107 |
-
|
| 108 |
-
Math.floor(Math.random() * 500)
|
| 109 |
-
);
|
| 110 |
} catch (err) {}
|
| 111 |
|
| 112 |
-
} catch (e) {
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
} catch (e) {
|
| 116 |
clearTimeout(timeout);
|
|
@@ -119,43 +128,47 @@ async function handleCloudflare(page, url) {
|
|
| 119 |
});
|
| 120 |
}
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
app.post('/bypass', async (req, res) => {
|
| 123 |
const { url, mode, siteKey, proxy } = req.body;
|
| 124 |
-
|
| 125 |
if (!url || !mode) return res.status(400).json({ status: "error", message: "Missing url or mode" });
|
| 126 |
-
if (
|
| 127 |
-
|
| 128 |
-
if (activeBrowsers >= MAX_CONCURRENT_BROWSERS) {
|
| 129 |
-
return res.status(429).json({ status: "busy", message: "Server busy, try again later" });
|
| 130 |
-
}
|
| 131 |
|
| 132 |
activeBrowsers++;
|
| 133 |
let browserInstance = null;
|
| 134 |
-
|
| 135 |
try {
|
| 136 |
console.log(`[REQ] Processing: ${url} | Mode: ${mode}`);
|
| 137 |
const { browser, page } = await createBrowser(proxy);
|
| 138 |
browserInstance = browser;
|
| 139 |
-
|
| 140 |
let result;
|
| 141 |
-
if (mode === 'turnstile')
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
result = await handleCloudflare(page, url);
|
| 145 |
-
} else {
|
| 146 |
-
throw new Error("Invalid mode. Use 'turnstile' or 'cloudflare'");
|
| 147 |
-
}
|
| 148 |
-
|
| 149 |
console.log(`[SUCCESS] Operation completed for ${mode}`);
|
| 150 |
res.json({ status: "success", ...result });
|
| 151 |
-
|
| 152 |
} catch (error) {
|
| 153 |
console.error(`[ERROR] ${error.message}`);
|
| 154 |
res.status(500).json({ status: "error", message: error.message });
|
| 155 |
} finally {
|
| 156 |
-
if (browserInstance) {
|
| 157 |
-
try { await browserInstance.close(); } catch {}
|
| 158 |
-
}
|
| 159 |
activeBrowsers--;
|
| 160 |
}
|
| 161 |
});
|
|
|
|
| 43 |
return { browser, page };
|
| 44 |
}
|
| 45 |
|
| 46 |
+
async function handleCloudflare(page, url) {
|
| 47 |
return new Promise(async (resolve, reject) => {
|
| 48 |
+
const timeout = setTimeout(() => reject(new Error("Timeout Cloudflare Bypass")), 120000);
|
| 49 |
+
|
| 50 |
try {
|
| 51 |
+
console.log(`[Cloudflare] Navigating to ${url}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
let gotCfFromListener = false;
|
| 54 |
+
page.on('response', async resp => {
|
| 55 |
try {
|
| 56 |
+
const headers = resp.headers();
|
| 57 |
+
const setCookie = headers['set-cookie'] || headers['Set-Cookie'] || headers['set-cookie'.toLowerCase()];
|
| 58 |
+
|
| 59 |
+
if (setCookie) {
|
| 60 |
+
const cookieStr = Array.isArray(setCookie) ? setCookie.join(' ') : setCookie;
|
| 61 |
+
if (cookieStr.includes('cf_clearance')) {
|
| 62 |
+
gotCfFromListener = true;
|
| 63 |
+
console.log(`[Cloudflare] 🔥 FAST DETECT: cf_clearance found in response headers from ${resp.url()}`);
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
} catch(e){}
|
| 67 |
+
});
|
| 68 |
|
| 69 |
+
await page.bringToFront();
|
| 70 |
+
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 90000 });
|
|
|
|
| 71 |
|
| 72 |
+
let reloadCount = 0;
|
| 73 |
+
let startTime = Date.now();
|
|
|
|
| 74 |
|
| 75 |
const loop = setInterval(async () => {
|
| 76 |
try {
|
| 77 |
const title = await page.title();
|
| 78 |
const content = await page.content();
|
| 79 |
+
const currentTime = Date.now();
|
| 80 |
+
const cookies = await page.cookies();
|
| 81 |
+
|
| 82 |
+
const cfCookie = cookies.find(c => c.name === 'cf_clearance');
|
| 83 |
|
| 84 |
const isChallengePage = title.includes("Just a moment") ||
|
| 85 |
content.includes("challenge-platform") ||
|
| 86 |
title.includes("Cloudflare");
|
| 87 |
|
| 88 |
+
if (!isChallengePage || cfCookie || gotCfFromListener) {
|
| 89 |
clearInterval(loop);
|
| 90 |
clearTimeout(timeout);
|
| 91 |
|
|
|
|
| 92 |
const ua = await page.evaluate(() => navigator.userAgent);
|
| 93 |
+
const finalCookie = cfCookie ? cfCookie.value : "";
|
| 94 |
|
| 95 |
+
console.log(`[Cloudflare] Success! (Source: ${cfCookie ? "Cookie Found" : (gotCfFromListener ? "Listener" : "Page Change")})`);
|
| 96 |
|
| 97 |
resolve({
|
| 98 |
status: "success",
|
| 99 |
userAgent: ua,
|
| 100 |
cookies_full: cookies,
|
| 101 |
+
cookie: finalCookie,
|
| 102 |
title: title
|
| 103 |
});
|
| 104 |
return;
|
| 105 |
}
|
| 106 |
|
| 107 |
+
if ((currentTime - startTime) > 30000 && reloadCount < 2) {
|
| 108 |
+
console.log("[Cloudflare] Stuck detected (>30s). Reloading page...");
|
| 109 |
+
reloadCount++;
|
| 110 |
+
startTime = Date.now();
|
| 111 |
+
await page.reload({ waitUntil: "domcontentloaded" });
|
| 112 |
+
return;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
try {
|
| 116 |
+
await page.mouse.move(Math.floor(Math.random() * 500), Math.floor(Math.random() * 500));
|
| 117 |
+
if (Math.random() > 0.9) await page.click('body').catch(()=>{});
|
|
|
|
|
|
|
| 118 |
} catch (err) {}
|
| 119 |
|
| 120 |
+
} catch (e) {
|
| 121 |
+
}
|
| 122 |
+
}, 1000);
|
| 123 |
|
| 124 |
} catch (e) {
|
| 125 |
clearTimeout(timeout);
|
|
|
|
| 128 |
});
|
| 129 |
}
|
| 130 |
|
| 131 |
+
async function handleTurnstile(page, url, siteKey) {
|
| 132 |
+
return new Promise(async (resolve, reject) => {
|
| 133 |
+
const timeout = setTimeout(() => reject(new Error("Timeout Turnstile")), 60000);
|
| 134 |
+
try {
|
| 135 |
+
const htmlContent = `<!DOCTYPE html><html lang="en"><body><div class="cf-turnstile" data-sitekey="${siteKey}" data-callback="turnstileCallback"></div><script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script><script>function turnstileCallback(token) { window.cf_token = token; }</script></body></html>`;
|
| 136 |
+
await page.setRequestInterception(true);
|
| 137 |
+
page.once('request', request => request.respond({ status: 200, contentType: 'text/html', body: htmlContent }));
|
| 138 |
+
page.on('request', req => { if (req.isInterceptResolutionHandled()) return; req.continue(); });
|
| 139 |
+
await page.goto(url, { waitUntil: "domcontentloaded" });
|
| 140 |
+
const checkToken = setInterval(async () => {
|
| 141 |
+
try {
|
| 142 |
+
const token = await page.evaluate(() => window.cf_token);
|
| 143 |
+
if (token) { clearInterval(checkToken); clearTimeout(timeout); resolve({ token: token }); }
|
| 144 |
+
} catch (e) {}
|
| 145 |
+
}, 1000);
|
| 146 |
+
} catch (e) { clearTimeout(timeout); reject(e); }
|
| 147 |
+
});
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
app.post('/bypass', async (req, res) => {
|
| 151 |
const { url, mode, siteKey, proxy } = req.body;
|
|
|
|
| 152 |
if (!url || !mode) return res.status(400).json({ status: "error", message: "Missing url or mode" });
|
| 153 |
+
if (activeBrowsers >= MAX_CONCURRENT_BROWSERS) return res.status(429).json({ status: "busy", message: "Server busy" });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
activeBrowsers++;
|
| 156 |
let browserInstance = null;
|
|
|
|
| 157 |
try {
|
| 158 |
console.log(`[REQ] Processing: ${url} | Mode: ${mode}`);
|
| 159 |
const { browser, page } = await createBrowser(proxy);
|
| 160 |
browserInstance = browser;
|
|
|
|
| 161 |
let result;
|
| 162 |
+
if (mode === 'turnstile') result = await handleTurnstile(page, url, siteKey);
|
| 163 |
+
else if (mode === 'cloudflare') result = await handleCloudflare(page, url);
|
| 164 |
+
else throw new Error("Invalid mode");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
console.log(`[SUCCESS] Operation completed for ${mode}`);
|
| 166 |
res.json({ status: "success", ...result });
|
|
|
|
| 167 |
} catch (error) {
|
| 168 |
console.error(`[ERROR] ${error.message}`);
|
| 169 |
res.status(500).json({ status: "error", message: error.message });
|
| 170 |
} finally {
|
| 171 |
+
if (browserInstance) try { await browserInstance.close(); } catch {}
|
|
|
|
|
|
|
| 172 |
activeBrowsers--;
|
| 173 |
}
|
| 174 |
});
|