CiEL7 commited on
Commit
bf6502f
·
verified ·
1 Parent(s): 0c6c8c9

Update index.js

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