CiEL7 commited on
Commit
35188ee
·
verified ·
1 Parent(s): 7de9db0

Create solve.js

Browse files
Files changed (1) hide show
  1. solve.js +249 -0
solve.js ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // @ts-nocheck
2
+ import { createRequire } from 'module';
3
+ const require = createRequire(import.meta.url);
4
+ 'use strict';
5
+ const { connect } = require('puppeteer-real-browser');
6
+ const fs = require('fs');
7
+ const WIDTH = 1280;
8
+ const HEIGHT = 800;
9
+ const ChallengePlatform = {
10
+ JAVASCRIPT: 'non-interactive',
11
+ MANAGED: 'managed',
12
+ INTERACTIVE: 'interactive',
13
+ };
14
+ const FALLBACK_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
15
+ '(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36';
16
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
17
+ function loadLines(fp) {
18
+ try {
19
+ return fs.readFileSync(fp, 'utf8').split('\n').map(l => l.trim()).filter(Boolean);
20
+ }
21
+ catch {
22
+ return [];
23
+ }
24
+ }
25
+ function randomChoice(arr) {
26
+ return arr[Math.floor(Math.random() * arr.length)];
27
+ }
28
+ function getChromeUA() {
29
+ const agents = loadLines('data/useragents.txt');
30
+ return agents.length ? randomChoice(agents) : FALLBACK_UA;
31
+ }
32
+ function parseProxy(url) {
33
+ const u = new URL(url);
34
+ return {
35
+ serverUrl: `${u.protocol}//${u.hostname}${u.port ? ':' + u.port : ''}`,
36
+ username: u.username || null,
37
+ password: u.password || null,
38
+ };
39
+ }
40
+ function extractClearance(cookies) {
41
+ return cookies.find(c => c.name === 'cf_clearance') || null;
42
+ }
43
+ function buildCookieString(cookies) {
44
+ return cookies.map(c => `${c.name}=${c.value}`).join('; ');
45
+ }
46
+ async function detectChallenge(page) {
47
+ try {
48
+ const html = await page.content();
49
+ for (const val of Object.values(ChallengePlatform)) {
50
+ if (html.includes(`cType: '${val}'`))
51
+ return val;
52
+ }
53
+ return null;
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ async function findCFFrame(page) {
60
+ try {
61
+ const frame = page.frames().find(f => {
62
+ try {
63
+ return f.url().includes('challenges.cloudflare.com');
64
+ }
65
+ catch {
66
+ return false;
67
+ }
68
+ });
69
+ if (frame)
70
+ return frame;
71
+ }
72
+ catch {
73
+ /* ignore */ }
74
+ try {
75
+ const el = await page.$('iframe[src*="challenges.cloudflare.com"]');
76
+ if (el)
77
+ return await el.contentFrame();
78
+ }
79
+ catch {
80
+ /* ignore */ }
81
+ return null;
82
+ }
83
+ async function clickCFFrame(page, frame) {
84
+ await sleep(1500);
85
+ try {
86
+ const cb = await frame.$('input[type="checkbox"]').catch(() => null);
87
+ if (cb) {
88
+ const box = await cb.boundingBox().catch(() => null);
89
+ if (box) {
90
+ await cb.click();
91
+ return 'checkbox';
92
+ }
93
+ }
94
+ const label = await frame.$('label').catch(() => null);
95
+ if (label) {
96
+ const box = await label.boundingBox().catch(() => null);
97
+ if (box) {
98
+ await label.click();
99
+ return 'label';
100
+ }
101
+ }
102
+ const widget = await frame.$('.cf-turnstile-content, #cf-stage, .ctp-checkbox-label').catch(() => null);
103
+ if (widget) {
104
+ const box = await widget.boundingBox().catch(() => null);
105
+ if (box) {
106
+ await widget.click();
107
+ return 'widget';
108
+ }
109
+ }
110
+ const body = await frame.$('body').catch(() => null);
111
+ if (body) {
112
+ const box = await body.boundingBox().catch(() => null);
113
+ if (box && box.width > 0) {
114
+ await body.click();
115
+ return 'body';
116
+ }
117
+ }
118
+ }
119
+ catch (e) { }
120
+ return null;
121
+ }
122
+ async function solveChallenge(page, timeoutSecs = 30) {
123
+ const deadline = Date.now() + timeoutSecs * 1000;
124
+ let attempt = 0;
125
+ let lastClicked = 0;
126
+ while (Date.now() < deadline) {
127
+ attempt++;
128
+ const cookies = await page.cookies().catch(() => []);
129
+ if (extractClearance(cookies)) {
130
+ return;
131
+ }
132
+ const platform = await detectChallenge(page);
133
+ if (!platform) {
134
+ return;
135
+ }
136
+ const frame = await findCFFrame(page);
137
+ if (!frame) {
138
+ await sleep(500);
139
+ continue;
140
+ }
141
+ if (Date.now() - lastClicked < 5000) {
142
+ await sleep(500);
143
+ continue;
144
+ }
145
+ const result = await clickCFFrame(page, frame);
146
+ if (result) {
147
+ lastClicked = Date.now();
148
+ await sleep(2500);
149
+ }
150
+ else {
151
+ await sleep(500);
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();
159
+ const parsed = chosenProxy ? parseProxy(chosenProxy) : null;
160
+ const args = [
161
+ '--no-sandbox',
162
+ '--disable-setuid-sandbox',
163
+ '--disable-quic',
164
+ `--window-size=${WIDTH},${HEIGHT}`,
165
+ ];
166
+ if (parsed)
167
+ args.push(`--proxy-server=${parsed.serverUrl}`);
168
+ const { browser, page } = await connect({
169
+ headless,
170
+ args,
171
+ turnstile: false,
172
+ connectOption: {},
173
+ disableXvfb: false,
174
+ ignoreAllFlags: false,
175
+ });
176
+ try {
177
+ await page.setViewport({
178
+ width: WIDTH,
179
+ height: HEIGHT
180
+ });
181
+ await page.setUserAgent(userAgent);
182
+ if (parsed?.username) {
183
+ await page.authenticate({
184
+ username: parsed.username,
185
+ password: parsed.password
186
+ });
187
+ }
188
+ try {
189
+ await page.goto(url, {
190
+ waitUntil: 'domcontentloaded',
191
+ timeout: timeout * 1000
192
+ });
193
+ }
194
+ catch (e) {
195
+ await browser.close();
196
+ return {
197
+ url,
198
+ success: false,
199
+ error: `Navigation error: ${e.message}`
200
+ };
201
+ }
202
+ await sleep(2000);
203
+ let cookies = await page.cookies().catch(() => []);
204
+ let clearance = extractClearance(cookies);
205
+ if (!clearance) {
206
+ const platform = await detectChallenge(page);
207
+ if (!platform) {
208
+ await browser.close();
209
+ return {
210
+ url,
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
+ if (!clearance) {
222
+ return {
223
+ url,
224
+ success: false,
225
+ error: 'Failed to obtain cf_clearance cookie'
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) {
245
+ await browser.close().catch(() => { });
246
+ throw err;
247
+ }
248
+ }
249
+ export { solveCloudflare };