Skip to content

best way to save what you love

merge: 10.6 updates

jjc4c47bdc2701
diff --cc api/src/core/itunnel.js
index fea4cb76,00000000..e16c0345
mode 100644,000000..100644
--- a/api/src/core/itunnel.js
+++ b/api/src/core/itunnel.js
@@@ -1,61 -1,0 +1,61 @@@
+import stream from "../stream/stream.js";
+import { getInternalTunnel } from "../stream/manage.js";
+import { setTunnelPort } from "../config.js";
+import { Green } from "../misc/console-text.js";
+import express from "express";
+
+const validateTunnel = (req, res) => {
+ if (!req.ip.endsWith('127.0.0.1')) {
+ res.sendStatus(403);
+ return;
+ }
+
+ if (String(req.query.id).length !== 21) {
+ res.sendStatus(400);
+ return;
+ }
+
+ const streamInfo = getInternalTunnel(req.query.id);
+ if (!streamInfo) {
+ res.sendStatus(404);
+ return;
+ }
+
+ return streamInfo;
+}
+
+const streamTunnel = (req, res) => {
+ const streamInfo = validateTunnel(req, res);
+ if (!streamInfo) {
+ return;
+ }
+
+ streamInfo.headers = new Map([
+ ...(streamInfo.headers || []),
+ ...Object.entries(req.headers)
+ ]);
+
- return stream(res, { type: 'internal', ...streamInfo });
++ return stream(res, { type: 'internal', data: streamInfo });
+}
+
+export const setupTunnelHandler = () => {
+ const tunnelHandler = express();
+
+ tunnelHandler.get('/itunnel', streamTunnel);
+
+ // fallback
+ tunnelHandler.use((_, res) => res.sendStatus(400));
+ // error handler
+ tunnelHandler.use((_, __, res, ____) => res.socket.end());
+
+
+ const server = tunnelHandler.listen({
+ port: 0,
+ host: '127.0.0.1',
+ exclusive: true
+ }, () => {
+ const { port } = server.address();
+ console.log(`${Green('[✓]')} internal tunnel handler running on 127.0.0.1:${port}`);
+ setTunnelPort(port);
+ });
+}
diff --cc api/src/stream/manage.js
index 10d25384,ebb5c6c7..0e6d496a
--- a/api/src/stream/manage.js
+++ b/api/src/stream/manage.js
@@@ -131,10 -124,14 +134,14 @@@ function getInternalTunnelId(url)
return;
}
- const id = url.searchParams.get('id');
+ return url.searchParams.get('id');
+ }
+
+ export function destroyInternalStream(url) {
+ const id = getInternalTunnelId(url);
if (internalStreamCache.has(id)) {
- closeRequest(getInternalStream(id)?.controller);
+ closeRequest(getInternalTunnel(id)?.controller);
internalStreamCache.delete(id);
}
}
diff --cc api/src/stream/stream.js
index 6de52793,c7cf7b56..e714f38e
--- a/api/src/stream/stream.js
+++ b/api/src/stream/stream.js
@@@ -10,10 -10,10 +10,10 @@@ export default async function(res, stre
return await stream.proxy(streamInfo, res);
case "internal":
- return await internalStream(streamInfo, res);
- return internalStream(streamInfo.data, res);
++ return await internalStream(streamInfo.data, res);
case "merge":
- return stream.merge(streamInfo, res);
+ return await stream.merge(streamInfo, res);
case "remux":
case "mute":
diff --cc pnpm-lock.yaml
index 9ce1c962,9e12913a..007ac4be
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@@ -4690,11 -4240,9 +4690,11 @@@ snapshots
optional: true
+
dependencies:
'@bufbuild/protobuf': 2.1.0
jintr: 3.2.0