Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
index.js
CHANGED
|
@@ -11,7 +11,7 @@ app.get('/', (req, res) => {
|
|
| 11 |
});
|
| 12 |
|
| 13 |
app.post('/solve', async (req, res) => {
|
| 14 |
-
const { url } = req.body;
|
| 15 |
if (!url) {
|
| 16 |
return res.status(400).json({ error: 'URL parameter is required' });
|
| 17 |
}
|
|
@@ -20,8 +20,9 @@ app.post('/solve', async (req, res) => {
|
|
| 20 |
// Panggil fungsi solveCloudflare
|
| 21 |
const result = await solveCloudflare({
|
| 22 |
url: url,
|
| 23 |
-
headless: '
|
| 24 |
-
timeout: 60
|
|
|
|
| 25 |
});
|
| 26 |
res.json(result);
|
| 27 |
} catch (error) {
|
|
|
|
| 11 |
});
|
| 12 |
|
| 13 |
app.post('/solve', async (req, res) => {
|
| 14 |
+
const { url, waitForCookie } = req.body;
|
| 15 |
if (!url) {
|
| 16 |
return res.status(400).json({ error: 'URL parameter is required' });
|
| 17 |
}
|
|
|
|
| 20 |
// Panggil fungsi solveCloudflare
|
| 21 |
const result = await solveCloudflare({
|
| 22 |
url: url,
|
| 23 |
+
headless: 'auto',
|
| 24 |
+
timeout: 60,
|
| 25 |
+
waitForCookie: waitForCookie
|
| 26 |
});
|
| 27 |
res.json(result);
|
| 28 |
} catch (error) {
|
solve.js
CHANGED
|
@@ -152,7 +152,7 @@ async function solveChallenge(page, timeoutSecs = 30) {
|
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
-
async function solveCloudflare({ url, headless = false, proxy = null, proxyFile = null, timeout = 30, } = {}) {
|
| 156 |
const proxies = proxyFile ? loadLines(proxyFile) : [];
|
| 157 |
const chosenProxy = proxy || (proxies.length ? randomChoice(proxies) : null);
|
| 158 |
const userAgent = getChromeUA();
|
|
@@ -168,7 +168,7 @@ async function solveCloudflare({ url, headless = false, proxy = null, proxyFile
|
|
| 168 |
const { browser, page } = await connect({
|
| 169 |
headless,
|
| 170 |
args,
|
| 171 |
-
turnstile:
|
| 172 |
connectOption: {},
|
| 173 |
disableXvfb: false,
|
| 174 |
ignoreAllFlags: false,
|
|
@@ -200,45 +200,71 @@ async function solveCloudflare({ url, headless = false, proxy = null, proxyFile
|
|
| 200 |
};
|
| 201 |
}
|
| 202 |
await sleep(2000);
|
| 203 |
-
|
| 204 |
-
let
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
success: false,
|
| 212 |
-
error: 'No Cloudflare challenge detected'
|
| 213 |
-
};
|
| 214 |
-
}
|
| 215 |
-
await solveChallenge(page, timeout);
|
| 216 |
cookies = await page.cookies().catch(() => []);
|
| 217 |
clearance = extractClearance(cookies);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
}
|
|
|
|
| 219 |
const finalUA = await page.evaluate(() => navigator.userAgent).catch(() => userAgent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
await browser.close();
|
| 221 |
-
|
|
|
|
| 222 |
return {
|
| 223 |
url,
|
| 224 |
success: false,
|
| 225 |
-
error: 'Failed to obtain
|
| 226 |
};
|
| 227 |
}
|
|
|
|
| 228 |
const cookieString = buildCookieString(cookies);
|
| 229 |
-
const unixTimestamp = Math.floor(clearance.expires - 365 * 24 * 3600);
|
| 230 |
const timestamp = new Date(unixTimestamp * 1000).toISOString();
|
|
|
|
| 231 |
return {
|
| 232 |
success: true,
|
| 233 |
url,
|
| 234 |
proxy: chosenProxy,
|
| 235 |
user_agent: finalUA,
|
| 236 |
-
cf_clearance: clearance.value,
|
|
|
|
| 237 |
all_cookies: cookies,
|
| 238 |
cookie_string: cookieString,
|
| 239 |
unix_timestamp: unixTimestamp,
|
| 240 |
timestamp,
|
| 241 |
-
domain: clearance.domain,
|
| 242 |
};
|
| 243 |
}
|
| 244 |
catch (err) {
|
|
|
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
+
async function solveCloudflare({ url, headless = false, proxy = null, proxyFile = null, timeout = 30, waitForCookie = null } = {}) {
|
| 156 |
const proxies = proxyFile ? loadLines(proxyFile) : [];
|
| 157 |
const chosenProxy = proxy || (proxies.length ? randomChoice(proxies) : null);
|
| 158 |
const userAgent = getChromeUA();
|
|
|
|
| 168 |
const { browser, page } = await connect({
|
| 169 |
headless,
|
| 170 |
args,
|
| 171 |
+
turnstile: true, // Enable native Turnstile auto-solving
|
| 172 |
connectOption: {},
|
| 173 |
disableXvfb: false,
|
| 174 |
ignoreAllFlags: false,
|
|
|
|
| 200 |
};
|
| 201 |
}
|
| 202 |
await sleep(2000);
|
| 203 |
+
|
| 204 |
+
let cookies = [];
|
| 205 |
+
let clearance = null;
|
| 206 |
+
let targetCookieFound = false;
|
| 207 |
+
|
| 208 |
+
const deadline = Date.now() + timeout * 1000;
|
| 209 |
+
|
| 210 |
+
while (Date.now() < deadline) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
cookies = await page.cookies().catch(() => []);
|
| 212 |
clearance = extractClearance(cookies);
|
| 213 |
+
|
| 214 |
+
if (waitForCookie) {
|
| 215 |
+
targetCookieFound = cookies.some(c => c.name === waitForCookie);
|
| 216 |
+
} else {
|
| 217 |
+
targetCookieFound = true;
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
if (clearance && targetCookieFound) {
|
| 221 |
+
await sleep(2000); // Allow any final background requests to finish
|
| 222 |
+
cookies = await page.cookies().catch(() => []);
|
| 223 |
+
break;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
// Safety fallback: try manual click if Turnstile is stuck
|
| 227 |
+
const frame = await findCFFrame(page);
|
| 228 |
+
if (frame) {
|
| 229 |
+
await clickCFFrame(page, frame);
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
await sleep(1000);
|
| 233 |
}
|
| 234 |
+
|
| 235 |
const finalUA = await page.evaluate(() => navigator.userAgent).catch(() => userAgent);
|
| 236 |
+
|
| 237 |
+
let turnstileToken = await page.evaluate(() => {
|
| 238 |
+
const el = document.querySelector('input[name="cf-turnstile-response"]');
|
| 239 |
+
return el ? el.value : null;
|
| 240 |
+
}).catch(() => null);
|
| 241 |
+
|
| 242 |
await browser.close();
|
| 243 |
+
|
| 244 |
+
if (!clearance && !targetCookieFound) {
|
| 245 |
return {
|
| 246 |
url,
|
| 247 |
success: false,
|
| 248 |
+
error: 'Failed to obtain required cookies within timeout'
|
| 249 |
};
|
| 250 |
}
|
| 251 |
+
|
| 252 |
const cookieString = buildCookieString(cookies);
|
| 253 |
+
const unixTimestamp = clearance ? Math.floor(clearance.expires - 365 * 24 * 3600) : Math.floor(Date.now() / 1000);
|
| 254 |
const timestamp = new Date(unixTimestamp * 1000).toISOString();
|
| 255 |
+
|
| 256 |
return {
|
| 257 |
success: true,
|
| 258 |
url,
|
| 259 |
proxy: chosenProxy,
|
| 260 |
user_agent: finalUA,
|
| 261 |
+
cf_clearance: clearance ? clearance.value : null,
|
| 262 |
+
turnstile_token: turnstileToken,
|
| 263 |
all_cookies: cookies,
|
| 264 |
cookie_string: cookieString,
|
| 265 |
unix_timestamp: unixTimestamp,
|
| 266 |
timestamp,
|
| 267 |
+
domain: clearance ? clearance.domain : new URL(url).hostname,
|
| 268 |
};
|
| 269 |
}
|
| 270 |
catch (err) {
|