recycleactor commited on
Commit
efd245a
·
verified ·
1 Parent(s): 6108235

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +90 -1
server.js CHANGED
@@ -422,6 +422,36 @@ app.all('/r', async (req, res) => {
422
  signal: controller.signal
423
  })
424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
  res.status(upstream.status)
426
  for (const h of RES_HEADERS_PASS) {
427
  if (h === 'content-length') {
@@ -450,6 +480,65 @@ app.all('/r', async (req, res) => {
450
  app.options('*', (req, res) => res.sendStatus(204))
451
 
452
  const port = Number(process.env.PORT || 7860)
453
- app.listen(port, () => {
454
  console.log('lampa-es5-proxy on :' + port)
455
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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();
429
+ let text = Buffer.from(buffer).toString('utf8');
430
+
431
+ // Replace wss:// urls to go through our proxy
432
+ const serverBase = getSelfBase(req);
433
+ const wsBase = serverBase.replace(/^http/, 'ws');
434
+
435
+ text = text.replace(/wss:\/\/[^"'\s]+/g, (match) => {
436
+ return wsBase + '/ws?url=' + encodeURIComponent(match);
437
+ });
438
+
439
+ const outBuf = Buffer.from(text, 'utf8');
440
+
441
+ res.status(upstream.status);
442
+ for (const h of RES_HEADERS_PASS) {
443
+ if (h === 'content-length') {
444
+ res.setHeader(h, outBuf.length);
445
+ continue;
446
+ }
447
+ const v = upstream.headers.get(h);
448
+ if (v) res.setHeader(h, v);
449
+ }
450
+ res.setHeader('access-control-allow-origin', '*');
451
+
452
+ return res.send(outBuf);
453
+ }
454
+
455
  res.status(upstream.status)
456
  for (const h of RES_HEADERS_PASS) {
457
  if (h === 'content-length') {
 
480
  app.options('*', (req, res) => res.sendStatus(204))
481
 
482
  const port = Number(process.env.PORT || 7860)
483
+ const server = app.listen(port, () => {
484
  console.log('lampa-es5-proxy on :' + port)
485
  })
486
+
487
+ server.on('upgrade', (req, clientSocket, head) => {
488
+ try {
489
+ const urlObj = new URL(req.url, 'http://localhost');
490
+ if (urlObj.pathname === '/ws') {
491
+ let targetUrlStr = urlObj.searchParams.get('url');
492
+ if (!targetUrlStr) {
493
+ clientSocket.destroy();
494
+ return;
495
+ }
496
+
497
+ const targetUrl = new URL(targetUrlStr);
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);
513
+
514
+ proxyReq.on('upgrade', (proxyRes, proxySocket, proxyHead) => {
515
+ let headers = 'HTTP/1.1 101 Switching Protocols\r\n';
516
+ for (let i = 0; i < proxyRes.rawHeaders.length; i += 2) {
517
+ headers += proxyRes.rawHeaders[i] + ': ' + proxyRes.rawHeaders[i+1] + '\r\n';
518
+ }
519
+ headers += '\r\n';
520
+ clientSocket.write(headers);
521
+
522
+ if (proxyHead && proxyHead.length) clientSocket.write(proxyHead);
523
+
524
+ proxySocket.pipe(clientSocket);
525
+ clientSocket.pipe(proxySocket);
526
+ });
527
+
528
+ proxyReq.on('error', (err) => {
529
+ clientSocket.destroy();
530
+ });
531
+
532
+ clientSocket.on('error', () => {
533
+ proxyReq.destroy();
534
+ });
535
+
536
+ // Send the initial upgrade request
537
+ proxyReq.end();
538
+ } else {
539
+ clientSocket.destroy();
540
+ }
541
+ } catch(e) {
542
+ clientSocket.destroy();
543
+ }
544
+ })