update
Browse files- endpoints/cloudflare-cookies.js +103 -73
- index.js +16 -16
- package.json +2 -2
endpoints/cloudflare-cookies.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
-
async function cloudflareCookies({ domain, cookies = [], proxy
|
| 2 |
if (!domain) throw new Error("Missing domain parameter");
|
| 3 |
|
| 4 |
const timeout = global.timeOut || 60000;
|
| 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,
|
|
@@ -19,100 +19,130 @@ async function cloudflareCookies({ domain, cookies = [], proxy = null, mobile =
|
|
| 19 |
});
|
| 20 |
}
|
| 21 |
|
| 22 |
-
// Set
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
if (cookies && cookies.length > 0) {
|
| 24 |
-
|
| 25 |
|
| 26 |
-
const
|
| 27 |
if (typeof cookie === 'string') {
|
| 28 |
-
// Parse string
|
| 29 |
-
const
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
} else {
|
| 37 |
-
//
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
path: cookie.path || '/'
|
| 43 |
-
};
|
| 44 |
}
|
| 45 |
-
}
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
-
// Navigate to the
|
| 52 |
-
console.log('Navigating to:', domain);
|
| 53 |
await page.goto(domain, {
|
| 54 |
-
waitUntil:
|
| 55 |
timeout: timeout
|
| 56 |
});
|
| 57 |
|
| 58 |
-
//
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
-
// Get
|
|
|
|
| 83 |
const finalUrl = page.url();
|
| 84 |
-
const
|
| 85 |
-
|
| 86 |
-
// Filter important Cloudflare cookies
|
| 87 |
-
const cfCookies = allCookies.filter(cookie =>
|
| 88 |
-
cookie.name.includes('cf_') ||
|
| 89 |
-
cookie.name.includes('__cf') ||
|
| 90 |
-
cookie.name.toLowerCase().includes('cloudflare')
|
| 91 |
-
);
|
| 92 |
-
|
| 93 |
-
// Get page content info
|
| 94 |
-
const pageInfo = await page.evaluate(() => {
|
| 95 |
-
return {
|
| 96 |
-
title: document.title,
|
| 97 |
-
url: window.location.href,
|
| 98 |
-
hasChallenge: document.title.includes('Just a moment') ||
|
| 99 |
-
document.body.innerHTML.includes('challenge'),
|
| 100 |
-
hasFaucet: document.body.innerHTML.includes('faucet') ||
|
| 101 |
-
document.body.innerHTML.includes('claim'),
|
| 102 |
-
hasForm: document.querySelector('form') !== null,
|
| 103 |
-
contentLength: document.body.innerHTML.length
|
| 104 |
-
};
|
| 105 |
-
});
|
| 106 |
|
| 107 |
isResolved = true;
|
| 108 |
clearTimeout(cl);
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
return {
|
| 111 |
-
success: !pageInfo.hasChallenge,
|
| 112 |
url: finalUrl,
|
| 113 |
-
cookies:
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
| 116 |
};
|
| 117 |
|
| 118 |
} catch (e) {
|
|
|
|
| 1 |
+
async function cloudflareCookies({ domain, cookies = [], proxy, mobile = false }, page) {
|
| 2 |
if (!domain) throw new Error("Missing domain parameter");
|
| 3 |
|
| 4 |
const timeout = global.timeOut || 60000;
|
| 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 |
+
// Authenticate proxy if provided
|
| 15 |
if (proxy?.username && proxy?.password) {
|
| 16 |
await page.authenticate({
|
| 17 |
username: proxy.username,
|
|
|
|
| 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 |
+
// Handle cookies - bisa string panjang seperti contoh Anda
|
| 29 |
if (cookies && cookies.length > 0) {
|
| 30 |
+
const cookieObjects = [];
|
| 31 |
|
| 32 |
+
for (const cookie of cookies) {
|
| 33 |
if (typeof cookie === 'string') {
|
| 34 |
+
// Parse string cookies seperti: "name=value; name2=value2"
|
| 35 |
+
const cookiePairs = cookie.split(';').map(pair => pair.trim());
|
| 36 |
+
|
| 37 |
+
for (const pair of cookiePairs) {
|
| 38 |
+
if (pair.includes('=')) {
|
| 39 |
+
const [name, ...valueParts] = pair.split('=');
|
| 40 |
+
const value = valueParts.join('=');
|
| 41 |
+
|
| 42 |
+
if (name && value) {
|
| 43 |
+
cookieObjects.push({
|
| 44 |
+
name: name.trim(),
|
| 45 |
+
value: value.trim(),
|
| 46 |
+
domain: new URL(domain).hostname,
|
| 47 |
+
path: '/',
|
| 48 |
+
secure: true,
|
| 49 |
+
httpOnly: name.startsWith('_') || name.includes('token'),
|
| 50 |
+
sameSite: 'Lax'
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
} else {
|
| 56 |
+
// Jika sudah object, langsung push
|
| 57 |
+
cookieObjects.push({
|
| 58 |
+
...cookie,
|
| 59 |
+
domain: cookie.domain || new URL(domain).hostname
|
| 60 |
+
});
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
+
}
|
| 63 |
|
| 64 |
+
// Set cookies ke browser
|
| 65 |
+
if (cookieObjects.length > 0) {
|
| 66 |
+
for (const cookie of cookieObjects) {
|
| 67 |
+
try {
|
| 68 |
+
await page.setCookie(cookie);
|
| 69 |
+
} catch (e) {
|
| 70 |
+
console.log('Failed to set cookie:', cookie.name);
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
}
|
| 75 |
|
| 76 |
+
// Navigate to the domain
|
|
|
|
| 77 |
await page.goto(domain, {
|
| 78 |
+
waitUntil: "networkidle2",
|
| 79 |
timeout: timeout
|
| 80 |
});
|
| 81 |
|
| 82 |
+
// Cloudflare challenge detection and solving logic
|
| 83 |
+
let challengeDetected = false;
|
| 84 |
+
let clearanceCookie = null;
|
| 85 |
+
const startTime = Date.now();
|
| 86 |
|
| 87 |
+
while (Date.now() - startTime < timeout && !clearanceCookie) {
|
| 88 |
+
// Check for Cloudflare challenge
|
| 89 |
+
const hasChallenge = await page.evaluate(() => {
|
| 90 |
+
return document.querySelector('#challenge-form') !== null ||
|
| 91 |
+
document.querySelector('.cf-browser-verification') !== null ||
|
| 92 |
+
document.body.innerHTML.includes('Checking if the site connection is secure') ||
|
| 93 |
+
document.body.innerHTML.includes('cf-browser-verification');
|
| 94 |
+
});
|
| 95 |
|
| 96 |
+
if (hasChallenge && !challengeDetected) {
|
| 97 |
+
challengeDetected = true;
|
| 98 |
+
console.log('Cloudflare challenge detected, solving...');
|
| 99 |
+
|
| 100 |
+
// Try to click challenge button if exists
|
| 101 |
+
try {
|
| 102 |
+
const submitButton = await page.$('input[type="submit"]');
|
| 103 |
+
if (submitButton) {
|
| 104 |
+
await submitButton.click();
|
| 105 |
+
await page.waitForTimeout(5000);
|
| 106 |
+
}
|
| 107 |
+
} catch (e) {
|
| 108 |
+
// Continue if button click fails
|
| 109 |
+
}
|
| 110 |
}
|
| 111 |
+
|
| 112 |
+
// Check for clearance cookie
|
| 113 |
+
const currentCookies = await page.cookies();
|
| 114 |
+
clearanceCookie = currentCookies.find(cookie => cookie.name === 'cf_clearance');
|
| 115 |
+
|
| 116 |
+
if (clearanceCookie) break;
|
| 117 |
+
|
| 118 |
+
await page.waitForTimeout(1000);
|
| 119 |
}
|
| 120 |
|
| 121 |
+
// Get final results
|
| 122 |
+
const finalCookies = await page.cookies();
|
| 123 |
const finalUrl = page.url();
|
| 124 |
+
const userAgent = await page.evaluate(() => navigator.userAgent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
isResolved = true;
|
| 127 |
clearTimeout(cl);
|
| 128 |
|
| 129 |
+
if (!clearanceCookie) {
|
| 130 |
+
throw new Error("Failed to obtain Cloudflare clearance cookie");
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// Format cookies to string like your example
|
| 134 |
+
const cookieString = finalCookies.map(cookie =>
|
| 135 |
+
`${cookie.name}=${cookie.value}`
|
| 136 |
+
).join('; ');
|
| 137 |
+
|
| 138 |
return {
|
|
|
|
| 139 |
url: finalUrl,
|
| 140 |
+
cookies: finalCookies,
|
| 141 |
+
cookie_string: cookieString, // Format seperti contoh panjang Anda
|
| 142 |
+
clearance: clearanceCookie,
|
| 143 |
+
user_agent: userAgent,
|
| 144 |
+
cf_clearance: clearanceCookie.value,
|
| 145 |
+
success: true
|
| 146 |
};
|
| 147 |
|
| 148 |
} catch (e) {
|
index.js
CHANGED
|
@@ -13,7 +13,7 @@ global.timeOut = Number(process.env.timeOut) || 60000
|
|
| 13 |
|
| 14 |
const CACHE_DIR = path.join(__dirname, "cache")
|
| 15 |
const CACHE_FILE = path.join(CACHE_DIR, "cache.json")
|
| 16 |
-
const CACHE_TTL = 5 * 60 * 1000
|
| 17 |
|
| 18 |
function loadCache() {
|
| 19 |
if (!fs.existsSync(CACHE_FILE)) return {}
|
|
@@ -104,12 +104,10 @@ async function createBrowser(proxyServer = null) {
|
|
| 104 |
return { browser, page }
|
| 105 |
}
|
| 106 |
|
| 107 |
-
// Import endpoints
|
| 108 |
const turnstile = require('./endpoints/turnstile')
|
| 109 |
const cloudflare = require('./endpoints/cloudflare')
|
| 110 |
const cloudflareCookies = require('./endpoints/cloudflare-cookies')
|
| 111 |
|
| 112 |
-
// Cloudflare Challenge dengan Cookies
|
| 113 |
app.post('/cloudflare-cookies', async (req, res) => {
|
| 114 |
const data = req.body;
|
| 115 |
|
|
@@ -154,22 +152,26 @@ app.post('/cloudflare-cookies', async (req, res) => {
|
|
| 154 |
browser = ctx.browser;
|
| 155 |
page = ctx.page;
|
| 156 |
|
| 157 |
-
// Set user agent mobile jika diperlukan
|
| 158 |
if (data.mobile) {
|
| 159 |
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');
|
| 160 |
await page.setViewport({ width: 980, height: 1743 });
|
| 161 |
}
|
| 162 |
|
| 163 |
-
result = await cloudflareCookies(
|
| 164 |
-
.
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
if (result.status === "success") {
|
| 175 |
writeCache(cacheKey, result);
|
|
@@ -191,7 +193,6 @@ app.post('/cloudflare-cookies', async (req, res) => {
|
|
| 191 |
res.status(result.code ?? 200).json(result);
|
| 192 |
});
|
| 193 |
|
| 194 |
-
// Endpoint Cloudflare yang sudah ada
|
| 195 |
app.post('/cloudflare', async (req, res) => {
|
| 196 |
const data = req.body
|
| 197 |
|
|
@@ -302,7 +303,6 @@ app.post('/cloudflare', async (req, res) => {
|
|
| 302 |
res.status(result.code ?? 200).json(result);
|
| 303 |
});
|
| 304 |
|
| 305 |
-
// Endpoint khusus Turnstile
|
| 306 |
app.post('/turnstile', async (req, res) => {
|
| 307 |
const data = req.body;
|
| 308 |
|
|
|
|
| 13 |
|
| 14 |
const CACHE_DIR = path.join(__dirname, "cache")
|
| 15 |
const CACHE_FILE = path.join(CACHE_DIR, "cache.json")
|
| 16 |
+
const CACHE_TTL = 5 * 60 * 1000
|
| 17 |
|
| 18 |
function loadCache() {
|
| 19 |
if (!fs.existsSync(CACHE_FILE)) return {}
|
|
|
|
| 104 |
return { browser, page }
|
| 105 |
}
|
| 106 |
|
|
|
|
| 107 |
const turnstile = require('./endpoints/turnstile')
|
| 108 |
const cloudflare = require('./endpoints/cloudflare')
|
| 109 |
const cloudflareCookies = require('./endpoints/cloudflare-cookies')
|
| 110 |
|
|
|
|
| 111 |
app.post('/cloudflare-cookies', async (req, res) => {
|
| 112 |
const data = req.body;
|
| 113 |
|
|
|
|
| 152 |
browser = ctx.browser;
|
| 153 |
page = ctx.page;
|
| 154 |
|
|
|
|
| 155 |
if (data.mobile) {
|
| 156 |
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');
|
| 157 |
await page.setViewport({ width: 980, height: 1743 });
|
| 158 |
}
|
| 159 |
|
| 160 |
+
result = await cloudflareCookies({
|
| 161 |
+
domain: data.domain,
|
| 162 |
+
cookies: data.cookies || [],
|
| 163 |
+
proxy: data.proxy,
|
| 164 |
+
mobile: data.mobile || false
|
| 165 |
+
}, page)
|
| 166 |
+
.then(r => ({
|
| 167 |
+
...r,
|
| 168 |
+
status: "success"
|
| 169 |
+
}))
|
| 170 |
+
.catch(err => ({
|
| 171 |
+
code: 500,
|
| 172 |
+
message: err.message,
|
| 173 |
+
status: "failed"
|
| 174 |
+
}));
|
| 175 |
|
| 176 |
if (result.status === "success") {
|
| 177 |
writeCache(cacheKey, result);
|
|
|
|
| 193 |
res.status(result.code ?? 200).json(result);
|
| 194 |
});
|
| 195 |
|
|
|
|
| 196 |
app.post('/cloudflare', async (req, res) => {
|
| 197 |
const data = req.body
|
| 198 |
|
|
|
|
| 303 |
res.status(result.code ?? 200).json(result);
|
| 304 |
});
|
| 305 |
|
|
|
|
| 306 |
app.post('/turnstile', async (req, res) => {
|
| 307 |
const data = req.body;
|
| 308 |
|
package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
| 9 |
},
|
| 10 |
"dependencies": {
|
| 11 |
"express": "^4.18.2",
|
| 12 |
-
"puppeteer": "^
|
| 13 |
"puppeteer-extra": "^3.3.6",
|
| 14 |
"puppeteer-extra-plugin-stealth": "^2.11.2",
|
| 15 |
"puppeteer-real-browser": "^1.4.0"
|
|
@@ -17,4 +17,4 @@
|
|
| 17 |
"engines": {
|
| 18 |
"node": ">=18.0.0"
|
| 19 |
}
|
| 20 |
-
}
|
|
|
|
| 9 |
},
|
| 10 |
"dependencies": {
|
| 11 |
"express": "^4.18.2",
|
| 12 |
+
"puppeteer": "^24.15.0",
|
| 13 |
"puppeteer-extra": "^3.3.6",
|
| 14 |
"puppeteer-extra-plugin-stealth": "^2.11.2",
|
| 15 |
"puppeteer-real-browser": "^1.4.0"
|
|
|
|
| 17 |
"engines": {
|
| 18 |
"node": ">=18.0.0"
|
| 19 |
}
|
| 20 |
+
}
|