File size: 7,503 Bytes
3c0e9c6 7ce295b 3c0e9c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | 'use strict';
const {
connect
} = require('puppeteer-real-browser');
const fs = require('fs');
const WIDTH = 1280;
const HEIGHT = 800;
const ChallengePlatform = {
JAVASCRIPT: 'non-interactive',
MANAGED: 'managed',
INTERACTIVE: 'interactive',
};
const FALLBACK_UA =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
'(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
function loadLines(fp) {
try {
return fs.readFileSync(fp, 'utf8').split('\n').map(l => l.trim()).filter(Boolean);
} catch {
return [];
}
}
function randomChoice(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function getChromeUA() {
const agents = loadLines('data/useragents.txt');
return agents.length ? randomChoice(agents) : FALLBACK_UA;
}
function parseProxy(url) {
const u = new URL(url);
return {
serverUrl: `${u.protocol}//${u.hostname}${u.port ? ':' + u.port : ''}`,
username: u.username || null,
password: u.password || null,
};
}
function extractClearance(cookies) {
return cookies.find(c => c.name === 'cf_clearance') || null;
}
function buildCookieString(cookies) {
return cookies.map(c => `${c.name}=${c.value}`).join('; ');
}
async function detectChallenge(page) {
try {
const html = await page.content();
for (const val of Object.values(ChallengePlatform)) {
if (html.includes(`cType: '${val}'`)) return val;
}
return null;
} catch {
return null;
}
}
async function findCFFrame(page) {
try {
const frame = page.frames().find(f => {
try {
return f.url().includes('challenges.cloudflare.com');
} catch {
return false;
}
});
if (frame) return frame;
} catch {
/* ignore */ }
try {
const el = await page.$('iframe[src*="challenges.cloudflare.com"]');
if (el) return await el.contentFrame();
} catch {
/* ignore */ }
return null;
}
async function clickCFFrame(page, frame) {
await sleep(1500);
try {
const cb = await frame.$('input[type="checkbox"]').catch(() => null);
if (cb) {
const box = await cb.boundingBox().catch(() => null);
if (box) {
await cb.click();
return 'checkbox';
}
}
const label = await frame.$('label').catch(() => null);
if (label) {
const box = await label.boundingBox().catch(() => null);
if (box) {
await label.click();
return 'label';
}
}
const widget = await frame.$('.cf-turnstile-content, #cf-stage, .ctp-checkbox-label').catch(() => null);
if (widget) {
const box = await widget.boundingBox().catch(() => null);
if (box) {
await widget.click();
return 'widget';
}
}
const body = await frame.$('body').catch(() => null);
if (body) {
const box = await body.boundingBox().catch(() => null);
if (box && box.width > 0) {
await body.click();
return 'body';
}
}
} catch (e) {}
return null;
}
async function solveChallenge(page, timeoutSecs = 30) {
const deadline = Date.now() + timeoutSecs * 1000;
let attempt = 0;
let lastClicked = 0;
while (Date.now() < deadline) {
attempt++;
const cookies = await page.cookies().catch(() => []);
if (extractClearance(cookies)) {
return;
}
const platform = await detectChallenge(page);
if (!platform) {
return;
}
const frame = await findCFFrame(page);
if (!frame) {
await sleep(500);
continue;
}
if (Date.now() - lastClicked < 5000) {
await sleep(500);
continue;
}
const result = await clickCFFrame(page, frame);
if (result) {
lastClicked = Date.now();
await sleep(2500);
} else {
await sleep(500);
}
}
}
async function solveCloudflare({
url,
headless = true,
proxy = null,
proxyFile = null,
timeout = 30,
} = {}) {
const proxies = proxyFile ? loadLines(proxyFile) : [];
const chosenProxy = proxy || (proxies.length ? randomChoice(proxies) : null);
const userAgent = getChromeUA();
const parsed = chosenProxy ? parseProxy(chosenProxy) : null;
const args = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-quic',
`--window-size=${WIDTH},${HEIGHT}`,
];
if (parsed) args.push(`--proxy-server=${parsed.serverUrl}`);
const {
browser,
page
} = await connect({
headless,
args,
turnstile: false,
connectOption: {},
disableXvfb: false,
ignoreAllFlags: false,
});
try {
await page.setViewport({
width: WIDTH,
height: HEIGHT
});
await page.setUserAgent(userAgent);
if (parsed?.username) {
await page.authenticate({
username: parsed.username,
password: parsed.password
});
}
try {
await page.goto(url, {
waitUntil: 'domcontentloaded',
timeout: timeout * 1000
});
} catch (e) {
await browser.close();
return {
url,
success: false,
error: `Navigation error: ${e.message}`
};
}
await sleep(2000);
let cookies = await page.cookies().catch(() => []);
let clearance = extractClearance(cookies);
if (!clearance) {
const platform = await detectChallenge(page);
if (!platform) {
await browser.close();
return {
url,
success: false,
error: 'No Cloudflare challenge detected'
};
}
await solveChallenge(page, timeout);
cookies = await page.cookies().catch(() => []);
clearance = extractClearance(cookies);
}
const finalUA = await page.evaluate(() => navigator.userAgent).catch(() => userAgent);
await browser.close();
if (!clearance) {
return {
url,
success: false,
error: 'Failed to obtain cf_clearance cookie'
};
}
const cookieString = buildCookieString(cookies);
const unixTimestamp = Math.floor(clearance.expires - 365 * 24 * 3600);
const timestamp = new Date(unixTimestamp * 1000).toISOString();
return {
success: true,
url,
proxy: chosenProxy,
user_agent: finalUA,
cf_clearance: clearance.value,
all_cookies: cookies,
cookie_string: cookieString,
unix_timestamp: unixTimestamp,
timestamp,
domain: clearance.domain,
};
} catch (err) {
await browser.close().catch(() => {});
throw err;
}
}
module.exports = {
solveCloudflare
};
|