Update endpoints/cloudflare-cookies.js
Browse files- endpoints/cloudflare-cookies.js +28 -138
endpoints/cloudflare-cookies.js
CHANGED
|
@@ -1,164 +1,54 @@
|
|
| 1 |
-
async function cloudflareCookies(
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
let isResolved = false;
|
| 6 |
-
|
| 7 |
-
const cl = setTimeout(() => {
|
| 8 |
-
if (!isResolved) {
|
| 9 |
-
throw new Error("Timeout Error");
|
| 10 |
-
}
|
| 11 |
-
}, timeout);
|
| 12 |
|
| 13 |
try {
|
| 14 |
-
|
| 15 |
-
if (proxy?.username && proxy?.password) {
|
| 16 |
-
await page.authenticate({
|
| 17 |
-
username: proxy.username,
|
| 18 |
-
password: proxy.password,
|
| 19 |
-
});
|
| 20 |
-
}
|
| 21 |
-
|
| 22 |
-
// Set mobile user agent and viewport if requested
|
| 23 |
-
if (mobile) {
|
| 24 |
-
await page.setUserAgent('Mozilla/5.0 (Linux; Android 14; CPH2641 Build/UP1A.231005.007) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.7390.97 Mobile Safari/537.36');
|
| 25 |
-
await page.setViewport({ width: 980, height: 1743 });
|
| 26 |
-
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
// Parse cookies input untuk diset ke browser
|
| 32 |
-
let cookiesToSet = [];
|
| 33 |
-
let cookiesArray = [];
|
| 34 |
-
|
| 35 |
-
if (cookies) {
|
| 36 |
-
if (typeof cookies === 'string') {
|
| 37 |
-
cookiesArray = cookies.split(';').map(cookie => cookie.trim()).filter(cookie => cookie);
|
| 38 |
-
} else {
|
| 39 |
-
cookiesArray = cookies;
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
for (const cookie of cookiesArray) {
|
| 43 |
-
if (typeof cookie === 'string') {
|
| 44 |
-
const [name, ...valueParts] = cookie.split('=');
|
| 45 |
-
const value = valueParts.join('=');
|
| 46 |
-
|
| 47 |
-
if (name && value) {
|
| 48 |
-
cookiesToSet.push({
|
| 49 |
-
name: name.trim(),
|
| 50 |
-
value: value.trim(),
|
| 51 |
-
domain: new URL(domain).hostname
|
| 52 |
-
});
|
| 53 |
-
}
|
| 54 |
-
} else {
|
| 55 |
-
cookiesToSet.push({
|
| 56 |
-
...cookie,
|
| 57 |
-
domain: cookie.domain || new URL(domain).hostname
|
| 58 |
-
});
|
| 59 |
-
}
|
| 60 |
-
}
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
await page.setCookie(cookie);
|
| 67 |
-
} catch (e) {
|
| 68 |
-
// Ignore cookie errors
|
| 69 |
-
}
|
| 70 |
-
}
|
| 71 |
}
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
// Navigate to the domain
|
| 75 |
-
await page.goto(domain, {
|
| 76 |
-
waitUntil: "networkidle2",
|
| 77 |
-
timeout: timeout
|
| 78 |
-
});
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
const startTime = Date.now();
|
| 84 |
|
| 85 |
-
|
| 86 |
-
// Check for Cloudflare challenge
|
| 87 |
-
const challengeState = await page.evaluate(() => {
|
| 88 |
-
const hasChallenge = document.querySelector('#challenge-form') !== null ||
|
| 89 |
-
document.querySelector('.cf-browser-verification') !== null ||
|
| 90 |
-
document.body.innerHTML.includes('Checking if the site connection is secure') ||
|
| 91 |
-
document.body.innerHTML.includes('Just a moment...');
|
| 92 |
-
|
| 93 |
-
const challengeButton = document.querySelector('input[type="submit"]') ||
|
| 94 |
-
document.querySelector('button[type="submit"]');
|
| 95 |
-
|
| 96 |
-
return { hasChallenge, challengeButton: !!challengeButton };
|
| 97 |
-
});
|
| 98 |
-
|
| 99 |
-
if (challengeState.hasChallenge && !challengeSolved) {
|
| 100 |
-
console.log('Cloudflare challenge detected, solving...');
|
| 101 |
-
|
| 102 |
-
// Try to click challenge button if exists
|
| 103 |
-
if (challengeState.challengeButton) {
|
| 104 |
-
try {
|
| 105 |
-
await page.click('input[type="submit"], button[type="submit"]');
|
| 106 |
-
await page.waitForTimeout(5000);
|
| 107 |
-
} catch (e) {
|
| 108 |
-
// Continue if button click fails
|
| 109 |
-
}
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
await page.waitForTimeout(3000);
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
// Check for new clearance cookie
|
| 116 |
-
const currentCookies = await page.cookies();
|
| 117 |
-
newClearanceCookie = currentCookies.find(cookie => cookie.name === 'cf_clearance');
|
| 118 |
-
|
| 119 |
-
if (newClearanceCookie) {
|
| 120 |
-
challengeSolved = true;
|
| 121 |
-
break;
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
await page.waitForTimeout(1000);
|
| 125 |
}
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
const finalCookies = await page.cookies();
|
| 129 |
const finalUrl = page.url();
|
| 130 |
const userAgent = await page.evaluate(() => navigator.userAgent);
|
| 131 |
-
|
| 132 |
-
// Find the new clearance cookie
|
| 133 |
-
newClearanceCookie = finalCookies.find(cookie => cookie.name === 'cf_clearance');
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
}
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
// Replace cf_clearance lama dengan yang baru
|
| 146 |
-
if (originalCookiesString.includes('cf_clearance=')) {
|
| 147 |
-
const oldClearanceRegex = /cf_clearance=[^;]+/;
|
| 148 |
-
updatedCookieString = originalCookiesString.replace(oldClearanceRegex, `cf_clearance=${newClearanceCookie.value}`);
|
| 149 |
} else {
|
| 150 |
-
|
| 151 |
-
updatedCookieString = originalCookiesString + `; cf_clearance=${newClearanceCookie.value}`;
|
| 152 |
}
|
| 153 |
|
| 154 |
return {
|
|
|
|
| 155 |
url: finalUrl,
|
| 156 |
cookies: finalCookies,
|
| 157 |
-
cookie_string: updatedCookieString,
|
| 158 |
clearance: newClearanceCookie,
|
| 159 |
-
user_agent: userAgent,
|
| 160 |
cf_clearance: newClearanceCookie.value,
|
| 161 |
-
|
| 162 |
};
|
| 163 |
|
| 164 |
} catch (e) {
|
|
|
|
| 1 |
+
async function cloudflareCookies(page, originalCookiesString, cl, opts = {}) {
|
| 2 |
+
const timeout = typeof opts.timeout === 'number' ? opts.timeout : 60000;
|
| 3 |
+
const start = Date.now();
|
| 4 |
+
let newClearanceCookie = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
try {
|
| 7 |
+
const wait = ms => new Promise(res => setTimeout(res, ms));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
while (Date.now() - start < timeout) {
|
| 10 |
+
const content = await page.content();
|
| 11 |
+
const lower = content.toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
if (lower.includes('just a moment') || lower.includes('turnstile')) {
|
| 14 |
+
await page.waitForNavigation({ waitUntil: 'networkidle', timeout: 30000 }).catch(() => {});
|
| 15 |
+
await wait(1000);
|
| 16 |
+
continue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
const cookies = await page.cookies();
|
| 20 |
+
newClearanceCookie = cookies.find(c => c.name === 'cf_clearance');
|
| 21 |
+
if (newClearanceCookie) break;
|
|
|
|
| 22 |
|
| 23 |
+
await wait(1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
+
clearTimeout(cl);
|
| 27 |
+
|
| 28 |
const finalCookies = await page.cookies();
|
| 29 |
const finalUrl = page.url();
|
| 30 |
const userAgent = await page.evaluate(() => navigator.userAgent);
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
newClearanceCookie = newClearanceCookie || finalCookies.find(c => c.name === 'cf_clearance');
|
| 33 |
+
if (!newClearanceCookie) throw new Error('Gagal mendapatkan cf_clearance (Turnstile belum selesai)');
|
| 34 |
|
| 35 |
+
let updatedCookieString = (originalCookiesString || '').trim();
|
| 36 |
+
if (updatedCookieString && !updatedCookieString.endsWith(';')) updatedCookieString += ';';
|
|
|
|
| 37 |
|
| 38 |
+
if (/cf_clearance=[^;]+/.test(updatedCookieString)) {
|
| 39 |
+
updatedCookieString = updatedCookieString.replace(/cf_clearance=[^;]+/, `cf_clearance=${newClearanceCookie.value}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
} else {
|
| 41 |
+
updatedCookieString += ` cf_clearance=${newClearanceCookie.value};`;
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
return {
|
| 45 |
+
success: true,
|
| 46 |
url: finalUrl,
|
| 47 |
cookies: finalCookies,
|
| 48 |
+
cookie_string: updatedCookieString.trim(),
|
| 49 |
clearance: newClearanceCookie,
|
|
|
|
| 50 |
cf_clearance: newClearanceCookie.value,
|
| 51 |
+
user_agent: userAgent
|
| 52 |
};
|
| 53 |
|
| 54 |
} catch (e) {
|