Reaperxxxx commited on
Commit
8f40ab9
·
verified ·
1 Parent(s): 8e07372

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +376 -33
index.js CHANGED
@@ -3,27 +3,176 @@ const axios = require('axios');
3
  const tough = require('tough-cookie');
4
  const { HttpsCookieAgent } = require('http-cookie-agent/http');
5
  const cheerio = require('cheerio');
6
- const { File } = require('undici');
7
- global.File = File;
8
 
9
  const app = express();
10
  const PORT = process.env.PORT || 3000;
11
 
12
  app.use(express.json());
13
 
14
- // Method: Using axios with enhanced headers and cookie handling
15
- async function extractWithAxios(url) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  try {
 
17
  const cookieJar = new tough.CookieJar();
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  const client = axios.create({
20
- httpsAgent: new HttpsCookieAgent({
21
- cookies: { jar: cookieJar },
22
- rejectUnauthorized: false
23
- }),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  headers: {
25
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
26
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
27
  'Accept-Language': 'en-US,en;q=0.9',
28
  'Accept-Encoding': 'gzip, deflate, br',
29
  'DNT': '1',
@@ -33,36 +182,199 @@ async function extractWithAxios(url) {
33
  'Sec-Fetch-Mode': 'navigate',
34
  'Sec-Fetch-Site': 'none',
35
  'Sec-Fetch-User': '?1',
36
- 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
37
  'sec-ch-ua-mobile': '?0',
38
  'sec-ch-ua-platform': '"Windows"',
39
- 'Cache-Control': 'max-age=0',
40
- 'Referer': 'https://kwik.cx/'
41
  },
42
- maxRedirects: 10,
43
- validateStatus: () => true,
44
- timeout: 30000
45
  });
46
 
47
- const response = await client.get(url);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  if (response.status === 403) {
50
  throw new Error('403 Forbidden - Access Denied');
51
  }
52
 
53
- const html = response.data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  const $ = cheerio.load(html);
55
 
56
- // Check for iframe
57
  const iframe = $('iframe.embed-responsive-item');
58
  const iframeExists = iframe.length > 0;
59
  const iframeSrc = iframe.attr('src') || null;
60
 
61
- // Check for download section
62
  const downloadSection = $('.download-form, .message.video-details, .download');
63
  const downloadSectionExists = downloadSection.length > 0;
64
 
65
- // Extract file details
66
  const fileName = $('strong').first().text().trim() || null;
67
  const fileSize = $('abbr[title*="Bytes"]').text().trim() || null;
68
  const resolution = $('.column:contains("Resolution")').text().replace('Resolution:', '').trim() || null;
@@ -78,20 +390,20 @@ async function extractWithAxios(url) {
78
  fileSize: fileSize,
79
  resolution: resolution
80
  },
81
- method: 'axios'
82
  };
83
 
84
  } catch (error) {
85
- console.error('axios error:', error.message);
86
  return {
87
  success: false,
88
  error: error.message,
89
- method: 'axios'
90
  };
91
  }
92
  }
93
 
94
- // Main route
95
  app.get('/extract', async (req, res) => {
96
  const { url } = req.query;
97
 
@@ -111,13 +423,38 @@ app.get('/extract', async (req, res) => {
111
 
112
  console.log(`[REQUEST] Extracting data from: ${url}`);
113
 
114
- const result = await extractWithAxios(url);
 
 
 
 
 
 
115
 
116
- if (result.success) {
117
- console.log(`[SUCCESS] Iframe exists: ${result.iframeExists}`);
118
- console.log(`[SUCCESS] Download section exists: ${result.downloadSectionExists}`);
119
- } else {
120
- console.log(`[FAILED] Error: ${result.error}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  }
122
 
123
  return res.json(result);
@@ -132,12 +469,18 @@ app.get('/health', (req, res) => {
132
  app.get('/', (req, res) => {
133
  res.json({
134
  service: 'Kwik Extractor API',
135
- version: '1.0.0',
136
  endpoints: {
137
  extract: '/extract?url=<kwik_url>',
138
  health: '/health'
139
  },
140
- example: '/extract?url=https://kwik.cx/f/0SoyAdDuRvy1'
 
 
 
 
 
 
141
  });
142
  });
143
 
 
3
  const tough = require('tough-cookie');
4
  const { HttpsCookieAgent } = require('http-cookie-agent/http');
5
  const cheerio = require('cheerio');
6
+ const https = require('https');
7
+ const { randomBytes } = require('crypto');
8
 
9
  const app = express();
10
  const PORT = process.env.PORT || 3000;
11
 
12
  app.use(express.json());
13
 
14
+ // Generate realistic browser fingerprint
15
+ function generateFingerprint() {
16
+ const browsers = [
17
+ {
18
+ name: 'Chrome',
19
+ version: '120.0.0.0',
20
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
21
+ secChUa: '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"'
22
+ },
23
+ {
24
+ name: 'Chrome',
25
+ version: '119.0.0.0',
26
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
27
+ secChUa: '"Not_A Brand";v="8", "Chromium";v="119", "Google Chrome";v="119"'
28
+ },
29
+ {
30
+ name: 'Chrome',
31
+ version: '121.0.0.0',
32
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
33
+ secChUa: '"Not_A Brand";v="8", "Chromium";v="121", "Google Chrome";v="121"'
34
+ },
35
+ {
36
+ name: 'Firefox',
37
+ version: '122.0',
38
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0',
39
+ secChUa: null
40
+ },
41
+ {
42
+ name: 'Edge',
43
+ version: '120.0.0.0',
44
+ userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0',
45
+ secChUa: '"Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"'
46
+ }
47
+ ];
48
+
49
+ return browsers[Math.floor(Math.random() * browsers.length)];
50
+ }
51
+
52
+ // Random delay helper
53
+ function randomDelay(min = 500, max = 2000) {
54
+ return new Promise(resolve => setTimeout(resolve, Math.floor(Math.random() * (max - min)) + min));
55
+ }
56
+
57
+ // Method 1: Advanced Axios with full header spoofing
58
+ async function extractWithAdvancedAxios(url) {
59
  try {
60
+ const fingerprint = generateFingerprint();
61
  const cookieJar = new tough.CookieJar();
62
 
63
+ // Create custom HTTPS agent with TLS fingerprinting
64
+ const httpsAgent = new https.Agent({
65
+ rejectUnauthorized: false,
66
+ minVersion: 'TLSv1.2',
67
+ maxVersion: 'TLSv1.3',
68
+ ciphers: [
69
+ 'TLS_AES_128_GCM_SHA256',
70
+ 'TLS_AES_256_GCM_SHA384',
71
+ 'TLS_CHACHA20_POLY1305_SHA256',
72
+ 'ECDHE-ECDSA-AES128-GCM-SHA256',
73
+ 'ECDHE-RSA-AES128-GCM-SHA256',
74
+ 'ECDHE-ECDSA-AES256-GCM-SHA384',
75
+ 'ECDHE-RSA-AES256-GCM-SHA384'
76
+ ].join(':'),
77
+ honorCipherOrder: true,
78
+ secureOptions: require('constants').SSL_OP_NO_SSLv2 | require('constants').SSL_OP_NO_SSLv3
79
+ });
80
+
81
+ const cookieAgent = new HttpsCookieAgent({
82
+ cookies: { jar: cookieJar },
83
+ ...httpsAgent.options
84
+ });
85
+
86
+ const headers = {
87
+ 'User-Agent': fingerprint.userAgent,
88
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
89
+ 'Accept-Language': 'en-US,en;q=0.9',
90
+ 'Accept-Encoding': 'gzip, deflate, br',
91
+ 'DNT': '1',
92
+ 'Connection': 'keep-alive',
93
+ 'Upgrade-Insecure-Requests': '1',
94
+ 'Sec-Fetch-Dest': 'document',
95
+ 'Sec-Fetch-Mode': 'navigate',
96
+ 'Sec-Fetch-Site': 'none',
97
+ 'Sec-Fetch-User': '?1',
98
+ 'sec-ch-ua-mobile': '?0',
99
+ 'sec-ch-ua-platform': '"Windows"',
100
+ 'Cache-Control': 'max-age=0',
101
+ 'Pragma': 'no-cache'
102
+ };
103
+
104
+ if (fingerprint.secChUa) {
105
+ headers['sec-ch-ua'] = fingerprint.secChUa;
106
+ }
107
+
108
  const client = axios.create({
109
+ httpsAgent: cookieAgent,
110
+ headers: headers,
111
+ maxRedirects: 10,
112
+ validateStatus: () => true,
113
+ timeout: 30000,
114
+ decompress: true
115
+ });
116
+
117
+ // First request to get cookies
118
+ await randomDelay(300, 800);
119
+ const response = await client.get(url);
120
+
121
+ if (response.status === 403) {
122
+ throw new Error('403 Forbidden - Access Denied');
123
+ }
124
+
125
+ const html = response.data;
126
+ const $ = cheerio.load(html);
127
+
128
+ const iframe = $('iframe.embed-responsive-item');
129
+ const iframeExists = iframe.length > 0;
130
+ const iframeSrc = iframe.attr('src') || null;
131
+
132
+ const downloadSection = $('.download-form, .message.video-details, .download');
133
+ const downloadSectionExists = downloadSection.length > 0;
134
+
135
+ const fileName = $('strong').first().text().trim() || null;
136
+ const fileSize = $('abbr[title*="Bytes"]').text().trim() || null;
137
+ const resolution = $('.column:contains("Resolution")').text().replace('Resolution:', '').trim() || null;
138
+
139
+ return {
140
+ success: true,
141
+ url: url,
142
+ iframeExists: iframeExists,
143
+ iframeSrc: iframeSrc,
144
+ downloadSectionExists: downloadSectionExists,
145
+ fileDetails: {
146
+ fileName: fileName,
147
+ fileSize: fileSize,
148
+ resolution: resolution
149
+ },
150
+ method: 'advanced-axios'
151
+ };
152
+
153
+ } catch (error) {
154
+ console.error('advanced-axios error:', error.message);
155
+ return {
156
+ success: false,
157
+ error: error.message,
158
+ method: 'advanced-axios'
159
+ };
160
+ }
161
+ }
162
+
163
+ // Method 2: Using undici with HTTP/2
164
+ async function extractWithUndici(url) {
165
+ try {
166
+ const { request } = require('undici');
167
+ const fingerprint = generateFingerprint();
168
+
169
+ await randomDelay(300, 800);
170
+
171
+ const { statusCode, headers, body } = await request(url, {
172
+ method: 'GET',
173
  headers: {
174
+ 'User-Agent': fingerprint.userAgent,
175
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
176
  'Accept-Language': 'en-US,en;q=0.9',
177
  'Accept-Encoding': 'gzip, deflate, br',
178
  'DNT': '1',
 
182
  'Sec-Fetch-Mode': 'navigate',
183
  'Sec-Fetch-Site': 'none',
184
  'Sec-Fetch-User': '?1',
185
+ 'sec-ch-ua': fingerprint.secChUa || '',
186
  'sec-ch-ua-mobile': '?0',
187
  'sec-ch-ua-platform': '"Windows"',
188
+ 'Cache-Control': 'max-age=0'
 
189
  },
190
+ maxRedirections: 10
 
 
191
  });
192
 
193
+ if (statusCode === 403) {
194
+ throw new Error('403 Forbidden - Access Denied');
195
+ }
196
+
197
+ let html = '';
198
+ for await (const chunk of body) {
199
+ html += chunk.toString();
200
+ }
201
+
202
+ const $ = cheerio.load(html);
203
+
204
+ const iframe = $('iframe.embed-responsive-item');
205
+ const iframeExists = iframe.length > 0;
206
+ const iframeSrc = iframe.attr('src') || null;
207
+
208
+ const downloadSection = $('.download-form, .message.video-details, .download');
209
+ const downloadSectionExists = downloadSection.length > 0;
210
+
211
+ const fileName = $('strong').first().text().trim() || null;
212
+ const fileSize = $('abbr[title*="Bytes"]').text().trim() || null;
213
+ const resolution = $('.column:contains("Resolution")').text().replace('Resolution:', '').trim() || null;
214
+
215
+ return {
216
+ success: true,
217
+ url: url,
218
+ iframeExists: iframeExists,
219
+ iframeSrc: iframeSrc,
220
+ downloadSectionExists: downloadSectionExists,
221
+ fileDetails: {
222
+ fileName: fileName,
223
+ fileSize: fileSize,
224
+ resolution: resolution
225
+ },
226
+ method: 'undici'
227
+ };
228
+
229
+ } catch (error) {
230
+ console.error('undici error:', error.message);
231
+ return {
232
+ success: false,
233
+ error: error.message,
234
+ method: 'undici'
235
+ };
236
+ }
237
+ }
238
+
239
+ // Method 3: Using node-fetch with enhanced headers
240
+ async function extractWithNodeFetch(url) {
241
+ try {
242
+ const fetch = require('node-fetch');
243
+ const fingerprint = generateFingerprint();
244
+
245
+ // Create HTTPS agent
246
+ const httpsAgent = new https.Agent({
247
+ rejectUnauthorized: false,
248
+ minVersion: 'TLSv1.2',
249
+ maxVersion: 'TLSv1.3'
250
+ });
251
+
252
+ await randomDelay(300, 800);
253
+
254
+ const response = await fetch(url, {
255
+ method: 'GET',
256
+ headers: {
257
+ 'User-Agent': fingerprint.userAgent,
258
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
259
+ 'Accept-Language': 'en-US,en;q=0.9',
260
+ 'Accept-Encoding': 'gzip, deflate, br',
261
+ 'DNT': '1',
262
+ 'Connection': 'keep-alive',
263
+ 'Upgrade-Insecure-Requests': '1',
264
+ 'Sec-Fetch-Dest': 'document',
265
+ 'Sec-Fetch-Mode': 'navigate',
266
+ 'Sec-Fetch-Site': 'none',
267
+ 'Sec-Fetch-User': '?1',
268
+ 'sec-ch-ua': fingerprint.secChUa || '',
269
+ 'sec-ch-ua-mobile': '?0',
270
+ 'sec-ch-ua-platform': '"Windows"',
271
+ 'Cache-Control': 'max-age=0',
272
+ 'Pragma': 'no-cache'
273
+ },
274
+ agent: httpsAgent,
275
+ redirect: 'follow',
276
+ follow: 10,
277
+ compress: true
278
+ });
279
 
280
  if (response.status === 403) {
281
  throw new Error('403 Forbidden - Access Denied');
282
  }
283
 
284
+ const html = await response.text();
285
+ const $ = cheerio.load(html);
286
+
287
+ const iframe = $('iframe.embed-responsive-item');
288
+ const iframeExists = iframe.length > 0;
289
+ const iframeSrc = iframe.attr('src') || null;
290
+
291
+ const downloadSection = $('.download-form, .message.video-details, .download');
292
+ const downloadSectionExists = downloadSection.length > 0;
293
+
294
+ const fileName = $('strong').first().text().trim() || null;
295
+ const fileSize = $('abbr[title*="Bytes"]').text().trim() || null;
296
+ const resolution = $('.column:contains("Resolution")').text().replace('Resolution:', '').trim() || null;
297
+
298
+ return {
299
+ success: true,
300
+ url: url,
301
+ iframeExists: iframeExists,
302
+ iframeSrc: iframeSrc,
303
+ downloadSectionExists: downloadSectionExists,
304
+ fileDetails: {
305
+ fileName: fileName,
306
+ fileSize: fileSize,
307
+ resolution: resolution
308
+ },
309
+ method: 'node-fetch'
310
+ };
311
+
312
+ } catch (error) {
313
+ console.error('node-fetch error:', error.message);
314
+ return {
315
+ success: false,
316
+ error: error.message,
317
+ method: 'node-fetch'
318
+ };
319
+ }
320
+ }
321
+
322
+ // Method 4: Using request-promise with full spoofing
323
+ async function extractWithRequestPromise(url) {
324
+ try {
325
+ const request = require('request-promise-native');
326
+ const fingerprint = generateFingerprint();
327
+
328
+ await randomDelay(300, 800);
329
+
330
+ const options = {
331
+ uri: url,
332
+ method: 'GET',
333
+ headers: {
334
+ 'User-Agent': fingerprint.userAgent,
335
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
336
+ 'Accept-Language': 'en-US,en;q=0.9',
337
+ 'Accept-Encoding': 'gzip, deflate, br',
338
+ 'DNT': '1',
339
+ 'Connection': 'keep-alive',
340
+ 'Upgrade-Insecure-Requests': '1',
341
+ 'Sec-Fetch-Dest': 'document',
342
+ 'Sec-Fetch-Mode': 'navigate',
343
+ 'Sec-Fetch-Site': 'none',
344
+ 'Sec-Fetch-User': '?1',
345
+ 'sec-ch-ua': fingerprint.secChUa || '',
346
+ 'sec-ch-ua-mobile': '?0',
347
+ 'sec-ch-ua-platform': '"Windows"',
348
+ 'Cache-Control': 'max-age=0'
349
+ },
350
+ followRedirect: true,
351
+ maxRedirects: 10,
352
+ gzip: true,
353
+ jar: true,
354
+ simple: false,
355
+ resolveWithFullResponse: true,
356
+ agentOptions: {
357
+ rejectUnauthorized: false,
358
+ secureProtocol: 'TLSv1_2_method'
359
+ }
360
+ };
361
+
362
+ const response = await request(options);
363
+
364
+ if (response.statusCode === 403) {
365
+ throw new Error('403 Forbidden - Access Denied');
366
+ }
367
+
368
+ const html = response.body;
369
  const $ = cheerio.load(html);
370
 
 
371
  const iframe = $('iframe.embed-responsive-item');
372
  const iframeExists = iframe.length > 0;
373
  const iframeSrc = iframe.attr('src') || null;
374
 
 
375
  const downloadSection = $('.download-form, .message.video-details, .download');
376
  const downloadSectionExists = downloadSection.length > 0;
377
 
 
378
  const fileName = $('strong').first().text().trim() || null;
379
  const fileSize = $('abbr[title*="Bytes"]').text().trim() || null;
380
  const resolution = $('.column:contains("Resolution")').text().replace('Resolution:', '').trim() || null;
 
390
  fileSize: fileSize,
391
  resolution: resolution
392
  },
393
+ method: 'request-promise'
394
  };
395
 
396
  } catch (error) {
397
+ console.error('request-promise error:', error.message);
398
  return {
399
  success: false,
400
  error: error.message,
401
+ method: 'request-promise'
402
  };
403
  }
404
  }
405
 
406
+ // Main route with multi-method fallback
407
  app.get('/extract', async (req, res) => {
408
  const { url } = req.query;
409
 
 
423
 
424
  console.log(`[REQUEST] Extracting data from: ${url}`);
425
 
426
+ // Try multiple methods in sequence
427
+ const methods = [
428
+ extractWithAdvancedAxios,
429
+ extractWithUndici,
430
+ extractWithNodeFetch,
431
+ extractWithRequestPromise
432
+ ];
433
 
434
+ let result = null;
435
+
436
+ for (const method of methods) {
437
+ try {
438
+ result = await method(url);
439
+ if (result.success) {
440
+ console.log(`[SUCCESS] Method: ${result.method}`);
441
+ console.log(`[SUCCESS] Iframe exists: ${result.iframeExists}`);
442
+ console.log(`[SUCCESS] Download section exists: ${result.downloadSectionExists}`);
443
+ break;
444
+ } else {
445
+ console.log(`[FAILED] Method: ${result.method} - ${result.error}`);
446
+ }
447
+ } catch (error) {
448
+ console.log(`[ERROR] Method failed: ${error.message}`);
449
+ }
450
+ }
451
+
452
+ if (!result || !result.success) {
453
+ return res.status(500).json({
454
+ success: false,
455
+ error: 'All extraction methods failed',
456
+ lastError: result ? result.error : 'Unknown error'
457
+ });
458
  }
459
 
460
  return res.json(result);
 
469
  app.get('/', (req, res) => {
470
  res.json({
471
  service: 'Kwik Extractor API',
472
+ version: '2.0.0',
473
  endpoints: {
474
  extract: '/extract?url=<kwik_url>',
475
  health: '/health'
476
  },
477
+ example: '/extract?url=https://kwik.cx/f/0SoyAdDuRvy1',
478
+ methods: [
479
+ 'advanced-axios',
480
+ 'undici',
481
+ 'node-fetch',
482
+ 'request-promise'
483
+ ]
484
  });
485
  });
486