recycleactor commited on
Commit
acbcb24
·
verified ·
1 Parent(s): 77a6b7f

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +41 -0
server.js CHANGED
@@ -444,6 +444,7 @@ app.all('/r', async (req, res) => {
444
  })
445
 
446
  const isJson = (upstream.headers.get('content-type') || '').includes('application/json');
 
447
 
448
  if (isJson) {
449
  const buffer = await upstream.arrayBuffer();
@@ -481,6 +482,46 @@ app.all('/r', async (req, res) => {
481
  }
482
  res.setHeader('access-control-allow-origin', '*');
483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484
  return res.end(outBuf);
485
  }
486
 
 
444
  })
445
 
446
  const isJson = (upstream.headers.get('content-type') || '').includes('application/json');
447
+ const isM3u8 = (upstream.headers.get('content-type') || '').includes('mpegurl') || source.includes('.m3u8');
448
 
449
  if (isJson) {
450
  const buffer = await upstream.arrayBuffer();
 
482
  }
483
  res.setHeader('access-control-allow-origin', '*');
484
 
485
+ return res.end(outBuf);
486
+ } else if (isM3u8) {
487
+ const buffer = await upstream.arrayBuffer();
488
+ let text = Buffer.from(buffer).toString('utf8');
489
+
490
+ const serverBase = getSelfBase(req);
491
+ const baseUrl = new URL(source);
492
+
493
+ const lines = text.split('\n');
494
+ for (let i = 0; i < lines.length; i++) {
495
+ let line = lines[i].trim();
496
+ if (line && !line.startsWith('#')) {
497
+ try {
498
+ const absoluteUrl = new URL(line, baseUrl).toString();
499
+ lines[i] = serverBase + '/r?url=' + encodeURIComponent(absoluteUrl);
500
+ } catch(e) {}
501
+ } else if (line.startsWith('#EXT-X-STREAM-INF:') || line.startsWith('#EXT-X-MEDIA:') || line.startsWith('#EXT-X-I-FRAME-STREAM-INF:')) {
502
+ lines[i] = line.replace(/URI=["']([^"']+)["']/g, (match, uri) => {
503
+ try {
504
+ const absoluteUrl = new URL(uri, baseUrl).toString();
505
+ return `URI="${serverBase}/r?url=${encodeURIComponent(absoluteUrl)}"`;
506
+ } catch(e) {
507
+ return match;
508
+ }
509
+ });
510
+ }
511
+ }
512
+ text = lines.join('\n');
513
+ const outBuf = Buffer.from(text, 'utf8');
514
+
515
+ res.status(upstream.status);
516
+ for (const h of RES_HEADERS_PASS) {
517
+ if (h === 'content-length') {
518
+ res.setHeader(h, outBuf.length);
519
+ continue;
520
+ }
521
+ let v = upstream.headers.get(h);
522
+ if (v) res.setHeader(h, v);
523
+ }
524
+ res.setHeader('access-control-allow-origin', '*');
525
  return res.end(outBuf);
526
  }
527