added public watch parties with cloudflared
This commit is contained in:
@@ -47,6 +47,76 @@ fastify.addHook("preHandler", async (request) => {
|
||||
}
|
||||
});
|
||||
|
||||
const roomService = require('./electron/api/rooms/rooms.service');
|
||||
|
||||
fastify.addHook('onRequest', async (req, reply) => {
|
||||
const isTunnel =
|
||||
!!req.headers['cf-connecting-ip'] ||
|
||||
!!req.headers['cf-ray'];
|
||||
|
||||
if (!isTunnel) return;
|
||||
|
||||
if (req.url.startsWith('/public/') ||
|
||||
req.url.startsWith('/views/') ||
|
||||
req.url.startsWith('/src/')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url.startsWith('/room')) {
|
||||
const urlParams = new URLSearchParams(req.url.split('?')[1]);
|
||||
const roomId = urlParams.get('id');
|
||||
|
||||
if (!roomId) {
|
||||
return reply.code(404).send({ error: 'Room ID required' });
|
||||
}
|
||||
|
||||
const room = roomService.getRoom(roomId);
|
||||
if (!room || room.exposed !== true) {
|
||||
return reply.code(404).send({ error: 'Room not found' });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const wsMatch = req.url.match(/^\/ws\/room\/([a-f0-9]+)/);
|
||||
if (wsMatch) {
|
||||
const roomId = wsMatch[1];
|
||||
const room = roomService.getRoom(roomId);
|
||||
|
||||
if (!room || room.exposed !== true) {
|
||||
return reply.code(404).send({ error: 'Room not found' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const apiMatch = req.url.match(/^\/api\/rooms\/([a-f0-9]+)/);
|
||||
if (apiMatch) {
|
||||
const roomId = apiMatch[1];
|
||||
const room = roomService.getRoom(roomId);
|
||||
|
||||
if (!room || room.exposed !== true) {
|
||||
return reply.code(404).send({ error: 'Room not found' });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const allowedEndpoints = [
|
||||
'/api/watch/stream',
|
||||
'/api/proxy',
|
||||
'/api/extensions',
|
||||
'/api/search'
|
||||
];
|
||||
|
||||
for (const endpoint of allowedEndpoints) {
|
||||
if (req.url.startsWith(endpoint)) {
|
||||
console.log('[Tunnel] ✓ Allowing utility endpoint:', endpoint);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
return reply.code(404).send({ error: 'Not found' });
|
||||
});
|
||||
|
||||
fastify.register(require("@fastify/static"), {
|
||||
root: path.join(__dirname, "public"),
|
||||
prefix: "/public/",
|
||||
|
||||
Reference in New Issue
Block a user