Spaces:
Sleeping
Sleeping
Upload server.js
Browse files
server.js
CHANGED
|
@@ -373,7 +373,7 @@ const REQ_HEADERS_SKIP = new Set([
|
|
| 373 |
])
|
| 374 |
const RES_HEADERS_PASS = [
|
| 375 |
'content-type', 'content-length', 'content-range', 'accept-ranges',
|
| 376 |
-
'cache-control', 'expires', 'last-modified', 'etag'
|
| 377 |
]
|
| 378 |
|
| 379 |
app.all('/r', async (req, res) => {
|
|
@@ -444,7 +444,18 @@ app.all('/r', async (req, res) => {
|
|
| 444 |
res.setHeader(h, outBuf.length);
|
| 445 |
continue;
|
| 446 |
}
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
if (v) res.setHeader(h, v);
|
| 449 |
}
|
| 450 |
res.setHeader('access-control-allow-origin', '*');
|
|
@@ -460,7 +471,17 @@ app.all('/r', async (req, res) => {
|
|
| 460 |
continue; // fetch decompresses gzip/br, so original content-length is invalid for text/json
|
| 461 |
}
|
| 462 |
}
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
if (v) res.setHeader(h, v)
|
| 465 |
}
|
| 466 |
res.setHeader('access-control-allow-origin', '*')
|
|
@@ -495,25 +516,26 @@ server.on('upgrade', (req, clientSocket, head) => {
|
|
| 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 |
-
|
|
|
|
| 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);
|
| 519 |
|
|
|
|
| 373 |
])
|
| 374 |
const RES_HEADERS_PASS = [
|
| 375 |
'content-type', 'content-length', 'content-range', 'accept-ranges',
|
| 376 |
+
'cache-control', 'expires', 'last-modified', 'etag', 'set-cookie'
|
| 377 |
]
|
| 378 |
|
| 379 |
app.all('/r', async (req, res) => {
|
|
|
|
| 444 |
res.setHeader(h, outBuf.length);
|
| 445 |
continue;
|
| 446 |
}
|
| 447 |
+
|
| 448 |
+
let v;
|
| 449 |
+
if (h === 'set-cookie') {
|
| 450 |
+
const cookies = upstream.headers.getSetCookie ? upstream.headers.getSetCookie() : [];
|
| 451 |
+
if (cookies.length) {
|
| 452 |
+
// Strip domain from cookies so they apply to our proxy's domain
|
| 453 |
+
v = cookies.map(c => c.replace(/domain=[^;]+;?\s*/gi, ''));
|
| 454 |
+
}
|
| 455 |
+
} else {
|
| 456 |
+
v = upstream.headers.get(h);
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
if (v) res.setHeader(h, v);
|
| 460 |
}
|
| 461 |
res.setHeader('access-control-allow-origin', '*');
|
|
|
|
| 471 |
continue; // fetch decompresses gzip/br, so original content-length is invalid for text/json
|
| 472 |
}
|
| 473 |
}
|
| 474 |
+
|
| 475 |
+
let v;
|
| 476 |
+
if (h === 'set-cookie') {
|
| 477 |
+
const cookies = upstream.headers.getSetCookie ? upstream.headers.getSetCookie() : [];
|
| 478 |
+
if (cookies.length) {
|
| 479 |
+
v = cookies.map(c => c.replace(/domain=[^;]+;?\s*/gi, ''));
|
| 480 |
+
}
|
| 481 |
+
} else {
|
| 482 |
+
v = upstream.headers.get(h)
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
if (v) res.setHeader(h, v)
|
| 486 |
}
|
| 487 |
res.setHeader('access-control-allow-origin', '*')
|
|
|
|
| 516 |
}
|
| 517 |
|
| 518 |
const targetUrl = new URL(targetUrlStr);
|
| 519 |
+
|
| 520 |
+
// Filter headers carefully
|
| 521 |
+
const proxyHeaders = {};
|
| 522 |
+
for (const h in req.headers) {
|
| 523 |
+
if (['host', 'x-forwarded-for', 'x-forwarded-proto', 'x-forwarded-host', 'cf-connecting-ip', 'cf-ray', 'cf-visitor'].includes(h.toLowerCase())) continue;
|
| 524 |
+
proxyHeaders[h] = req.headers[h];
|
| 525 |
+
}
|
| 526 |
+
|
| 527 |
+
proxyHeaders['host'] = targetUrl.hostname;
|
| 528 |
+
proxyHeaders['origin'] = targetUrl.protocol.replace('ws', 'http') + '//' + targetUrl.hostname;
|
| 529 |
+
proxyHeaders['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';
|
| 530 |
+
|
| 531 |
const options = {
|
| 532 |
port: targetUrl.port || (targetUrl.protocol === 'wss:' ? 443 : 80),
|
| 533 |
host: targetUrl.hostname,
|
| 534 |
+
servername: targetUrl.hostname, // CRITICAL FOR SNI (Cloudflare drops without this)
|
| 535 |
+
headers: proxyHeaders,
|
| 536 |
path: targetUrl.pathname + targetUrl.search
|
| 537 |
};
|
| 538 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
const proto = targetUrl.protocol === 'wss:' ? require('https') : require('http');
|
| 540 |
const proxyReq = proto.request(options);
|
| 541 |
|