recycleactor commited on
Commit
300b535
verified
1 Parent(s): e188a86

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +19 -4
server.js CHANGED
@@ -422,7 +422,7 @@ app.all('/r', async (req, res) => {
422
  signal: controller.signal
423
  })
424
 
425
- const isJson = false; // 袙袪袝袦袝袧袧袨 袨孝袣袥挟效袝袧袨: (upstream.headers.get('content-type') || '').includes('application/json');
426
 
427
  if (isJson) {
428
  const buffer = await upstream.arrayBuffer();
@@ -449,7 +449,7 @@ app.all('/r', async (req, res) => {
449
  }
450
  res.setHeader('access-control-allow-origin', '*');
451
 
452
- return res.send(outBuf);
453
  }
454
 
455
  res.status(upstream.status)
@@ -498,15 +498,21 @@ server.on('upgrade', (req, clientSocket, head) => {
498
  const options = {
499
  port: targetUrl.port || (targetUrl.protocol === 'wss:' ? 443 : 80),
500
  host: targetUrl.hostname,
501
- headers: req.headers,
502
  path: targetUrl.pathname + targetUrl.search
503
  };
504
 
505
- // Clean up headers
506
  options.headers.host = targetUrl.hostname;
 
 
 
507
  delete options.headers['x-forwarded-for'];
508
  delete options.headers['x-forwarded-proto'];
509
  delete options.headers['x-forwarded-host'];
 
 
 
510
 
511
  const proto = targetUrl.protocol === 'wss:' ? require('https') : require('http');
512
  const proxyReq = proto.request(options);
@@ -524,8 +530,16 @@ server.on('upgrade', (req, clientSocket, head) => {
524
  proxySocket.pipe(clientSocket);
525
  clientSocket.pipe(proxySocket);
526
  });
 
 
 
 
 
 
 
527
 
528
  proxyReq.on('error', (err) => {
 
529
  clientSocket.destroy();
530
  });
531
 
@@ -539,6 +553,7 @@ server.on('upgrade', (req, clientSocket, head) => {
539
  clientSocket.destroy();
540
  }
541
  } catch(e) {
 
542
  clientSocket.destroy();
543
  }
544
  })
 
422
  signal: controller.signal
423
  })
424
 
425
+ const isJson = (upstream.headers.get('content-type') || '').includes('application/json');
426
 
427
  if (isJson) {
428
  const buffer = await upstream.arrayBuffer();
 
449
  }
450
  res.setHeader('access-control-allow-origin', '*');
451
 
452
+ return res.end(outBuf);
453
  }
454
 
455
  res.status(upstream.status)
 
498
  const options = {
499
  port: targetUrl.port || (targetUrl.protocol === 'wss:' ? 443 : 80),
500
  host: targetUrl.hostname,
501
+ headers: Object.assign({}, req.headers), // clone headers to avoid modifying original
502
  path: targetUrl.pathname + targetUrl.search
503
  };
504
 
505
+ // Clean up headers and fake origin to avoid 403 Forbidden
506
  options.headers.host = targetUrl.hostname;
507
+ options.headers.origin = targetUrl.protocol.replace('ws', 'http') + '//' + targetUrl.hostname;
508
+ options.headers['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';
509
+
510
  delete options.headers['x-forwarded-for'];
511
  delete options.headers['x-forwarded-proto'];
512
  delete options.headers['x-forwarded-host'];
513
+ delete options.headers['cf-connecting-ip'];
514
+ delete options.headers['cf-ray'];
515
+ delete options.headers['cf-visitor'];
516
 
517
  const proto = targetUrl.protocol === 'wss:' ? require('https') : require('http');
518
  const proxyReq = proto.request(options);
 
530
  proxySocket.pipe(clientSocket);
531
  clientSocket.pipe(proxySocket);
532
  });
533
+
534
+ // Handle server rejecting the WS connection (e.g. 403 or 400)
535
+ proxyReq.on('response', (proxyRes) => {
536
+ console.error('WS Proxy rejected with status:', proxyRes.statusCode);
537
+ clientSocket.write(`HTTP/1.1 ${proxyRes.statusCode} ${proxyRes.statusMessage}\r\n\r\n`);
538
+ clientSocket.destroy();
539
+ });
540
 
541
  proxyReq.on('error', (err) => {
542
+ console.error('WS Proxy error:', err.message);
543
  clientSocket.destroy();
544
  });
545
 
 
553
  clientSocket.destroy();
554
  }
555
  } catch(e) {
556
+ console.error('WS Upgrade catch error:', e.message);
557
  clientSocket.destroy();
558
  }
559
  })