vikarshana commited on
Commit
5710d2f
·
verified ·
1 Parent(s): 64fa433

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +17 -145
server.js CHANGED
@@ -257,158 +257,30 @@ app.get('/api/bypass', async (req, res) => {
257
  return res.status(500).json({ error: 'Internal error' });
258
  }
259
  });
260
- async function capturetokenandcookies(url, options = {}) {
261
- const {
262
- chromePath = '/opt/google/chrome/chrome',
263
- headless = 'new',
264
- buttonSelector = '#gdriveButton',
265
- waitForButtonMs = 60000,
266
- waitForPostMs = 15000
267
- } = options;
268
-
269
- let browser;
270
- try {
271
- browser = await puppeteer.launch({
272
- executablePath: chromePath,
273
- headless,
274
- args: [
275
- '--no-sandbox',
276
- '--disable-setuid-sandbox',
277
- '--disable-gpu',
278
- '--no-zygote',
279
- ],
280
- });
281
-
282
- const page = await browser.newPage();
283
- await page.setViewport({ width: 1280, height: 900 });
284
- await page.setUserAgent(
285
- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
286
- );
287
-
288
- // Optional: lighten load by blocking images/fonts/stylesheets if you don't need them
289
- await page.setRequestInterception(true);
290
- page.on('request', req => {
291
- const rt = req.resourceType();
292
- if (['image', 'stylesheet', 'font', 'media'].includes(rt)) req.abort();
293
- else req.continue();
294
- });
295
-
296
- // Collect any POST requests observed (helpful if multiple are fired)
297
- const captured = [];
298
- page.on('request', req => {
299
- if (req.method() === 'POST') {
300
- captured.push({
301
- url: req.url(),
302
- headers: req.headers(),
303
- body: req.postData(),
304
- timestamp: Date.now(),
305
- });
306
- }
307
- });
308
-
309
- // Also capture responses for those requests
310
- const responses = [];
311
- page.on('response', async res => {
312
- try {
313
- const req = res.request();
314
- if (req.method() === 'POST') {
315
- let text = null;
316
- try { text = await res.text(); } catch {}
317
- responses.push({
318
- url: res.url(),
319
- status: res.status(),
320
- bodyText: text,
321
- timestamp: Date.now(),
322
- });
323
- }
324
- } catch (e) { /* ignore */ }
325
- });
326
-
327
- // Navigate
328
- await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 60000 });
329
-
330
- // Wait for dynamic injection: wait for DOM ready and the button
331
- await page.waitForFunction(() => document.readyState === 'complete', { timeout: 30000 });
332
-
333
- console.log(`⏳ Waiting up to ${waitForButtonMs}ms for selector ${buttonSelector} to appear...`);
334
- await page.waitForSelector(buttonSelector, { visible: true, timeout: waitForButtonMs });
335
- console.log('✅ Button found. Preparing to capture the POST request after click.');
336
-
337
- // Prepare a Promise that resolves after the first POST request observed post-click
338
- const firstPostAfterClick = new Promise((resolve, reject) => {
339
- const timeout = setTimeout(() => reject(new Error('Timed out waiting for POST after click')), waitForPostMs);
340
- const checkInterval = setInterval(() => {
341
- // If a POST was added after the click (captured array non-empty) resolve
342
- if (captured.length > 0) {
343
- clearTimeout(timeout);
344
- clearInterval(checkInterval);
345
- resolve(captured.slice()); // return a shallow copy
346
- }
347
- }, 150);
348
- });
349
-
350
- // Click the button (this should trigger the same client-side fetch the site uses)
351
- await page.click(buttonSelector);
352
-
353
- // Wait for the POST(s) to appear (or timeout)
354
- let posts;
355
- try {
356
- posts = await firstPostAfterClick;
357
- } catch (err) {
358
- // If no POST was captured, still gather what we have and continue
359
- console.warn('⚠️ No POST captured after click (or timed out). Captured so far:', captured.length);
360
- posts = captured.slice();
361
- }
362
-
363
- // Get cookies for the current page (useful for authenticated flows)
364
- const pageCookies = await page.cookies();
365
-
366
- // Optionally, parse first post body as JSON if possible
367
- const parsedPosts = posts.map(p => {
368
- let parsedBody = null;
369
- try {
370
- parsedBody = JSON.parse(p.body);
371
- } catch (e) {
372
- // attempt URLSearchParams parse
373
- try {
374
- const params = new URLSearchParams(p.body || '');
375
- if ([...params].length) {
376
- parsedBody = {};
377
- for (const [k, v] of params.entries()) {
378
- parsedBody[k] = v;
379
- }
380
- }
381
- } catch {}
382
- }
383
- return { ...p, parsedBody };
384
- });
385
-
386
- const respSnapshot = responses.slice();
387
-
388
- await page.close();
389
-
390
- return {
391
- success: true,
392
- posts: parsedPosts,
393
- responses: respSnapshot,
394
- cookies: pageCookies,
395
- };
396
-
397
- } catch (err) {
398
- console.error('Error in capturePayloadAndCookies:', err);
399
- page.close()
400
- return { success: false, error: err.message || String(err) };
401
- }
402
- }
403
 
404
  app.get('/api/cinex', async (req, res) => {
405
  const { url } = req.query;
406
  if (!url) {
407
  return res.status(400).json({ error: 'URL is required' });
408
  }
 
 
 
 
 
 
 
 
409
 
410
- const result = await capturetokenandcookies("https://vikarshana-puppeteer.hf.space/api/bypass?url=" + url);
411
- res.json(result);
 
 
 
 
 
 
 
412
  });
413
 
414
 
 
257
  return res.status(500).json({ error: 'Internal error' });
258
  }
259
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
 
261
  app.get('/api/cinex', async (req, res) => {
262
  const { url } = req.query;
263
  if (!url) {
264
  return res.status(400).json({ error: 'URL is required' });
265
  }
266
+ const result = await captureGDrivePayloadAndCookie(url, {
267
+ chromePath: '/opt/google/chrome/chrome',
268
+ headless: 'new',
269
+ buttonSelector: '#gdriveButton',
270
+ fallbackSelector: '.google-download',
271
+ waitForButtonMs: 60000,
272
+ waitForPostMs: 20000,
273
+ });
274
 
275
+ if (!result.success) {
276
+ return res.status(500).json({ error: result.error || 'failed' });
277
+ }
278
+
279
+ return res.json({
280
+ cookie: result.cookieHeader,
281
+ payload: result.payload,
282
+ requestUrl: result.requestUrl,
283
+ });
284
  });
285
 
286