diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs index 7a888cf..9b15298 100644 --- a/apps/web/next.config.mjs +++ b/apps/web/next.config.mjs @@ -56,6 +56,9 @@ const nextConfig = { }, ]; }, + logging: { + incomingRequests: false, + }, }; export default withSentryConfig(nextConfig, { diff --git a/apps/web/src/app/(ui)/(protected)/api/mediamtx/publish/route.ts b/apps/web/src/app/(ui)/(protected)/api/mediamtx/publish/route.ts index 3db4814..a073e4d 100644 --- a/apps/web/src/app/(ui)/(protected)/api/mediamtx/publish/route.ts +++ b/apps/web/src/app/(ui)/(protected)/api/mediamtx/publish/route.ts @@ -1,4 +1,4 @@ -import { prisma } from '@hctv/db'; +import { prisma, getRedisConnection } from '@hctv/db'; import { NextRequest } from 'next/server'; import { z } from 'zod'; @@ -8,29 +8,35 @@ export async function POST(request: NextRequest) { const parsed = schema.safeParse(body); if (!parsed.success) { - console.log('Parsing error:', parsed.error); - return new Response('Invalid request', { status: 400 }); + return new Response('invalid request', { status: 400 }); } - console.log('Parsed data:', parsed.data); const { action, protocol, path, password } = parsed.data; + if (action === 'publish' && protocol === 'srt') { + const redis = getRedisConnection(); + const channelKey = await redis.get(`streamKey:${path}`) - if (action === 'publish' && protocol !== 'srt') { - const key = await prisma.streamKey.findFirst({ - where: { - key: password, - channel: { - name: path, - } - }, - include: { - channel: true, - }, - }); + if (channelKey) { + if (channelKey !== password) { + return new Response('invalid stream key', { status: 403 }); + } + return new Response('youre in yay', { status: 200 }); + } else { + const key = await prisma.streamKey.findFirst({ + where: { + key: password, + channel: { + name: path, + } + }, + include: { + channel: true, + }, + }); - if (!key) { - return new Response('Invalid stream key', { status: 403 }); + if (!key) { + return new Response('invalid stream key', { status: 403 }); + } } - console.log('Stream key valid for channel:', key.channel.name); } return new Response('Request processed', { status: 200 }); diff --git a/apps/web/src/app/(ui)/(protected)/api/rtmp/streamKey/route.ts b/apps/web/src/app/(ui)/(protected)/api/rtmp/streamKey/route.ts index 6536d2b..73f0dbd 100644 --- a/apps/web/src/app/(ui)/(protected)/api/rtmp/streamKey/route.ts +++ b/apps/web/src/app/(ui)/(protected)/api/rtmp/streamKey/route.ts @@ -1,5 +1,5 @@ import { validateRequest } from '@/lib/auth/validate'; -import { prisma } from '@hctv/db'; +import { prisma, getRedisConnection } from '@hctv/db'; import { NextRequest } from "next/server"; export async function POST(request: NextRequest) { @@ -47,6 +47,9 @@ export async function POST(request: NextRequest) { } }) + const redis = getRedisConnection(); + await redis.set(`streamKey:${channel}`, dbUpdate.key); + return new Response(JSON.stringify({ key: dbUpdate.key }), { status: 200, headers: { diff --git a/apps/web/src/instrumentation.ts b/apps/web/src/instrumentation.ts index 9ee3a20..1b57c7a 100644 --- a/apps/web/src/instrumentation.ts +++ b/apps/web/src/instrumentation.ts @@ -6,6 +6,7 @@ export async function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { await (await import('@/lib/instrumentation/streamInfo')).default(); await (await import('@/lib/instrumentation/writeSessions')).default(); + await (await import('@/lib/instrumentation/syncStreamKeys')).default(); } if (process.env.NEXT_RUNTIME === 'nodejs') { diff --git a/apps/web/src/lib/instrumentation/syncStreamKeys.ts b/apps/web/src/lib/instrumentation/syncStreamKeys.ts new file mode 100644 index 0000000..5bc8d68 --- /dev/null +++ b/apps/web/src/lib/instrumentation/syncStreamKeys.ts @@ -0,0 +1,31 @@ +import { prisma, getRedisConnection } from '@hctv/db'; + +export default async function syncStreamKeys() { + console.log('Syncing stream keys to Redis...'); + try { + const keys = await prisma.streamKey.findMany({ + include: { + channel: true, + }, + }); + + if (keys.length === 0) { + console.log('No stream keys found to sync.'); + return; + } + + const redis = getRedisConnection(); + const pipeline = redis.pipeline(); + + for (const key of keys) { + if (key.channel) { + pipeline.set(`streamKey:${key.channel.name}`, key.key); + } + } + + await pipeline.exec(); + console.log(`Synced ${keys.length} stream keys to Redis`); + } catch (error) { + console.error('Failed to sync stream keys to Redis:', error); + } +} diff --git a/dev/docker-compose.yml b/dev/docker-compose.yml index a908bb9..2693651 100644 --- a/dev/docker-compose.yml +++ b/dev/docker-compose.yml @@ -15,41 +15,6 @@ services: - ./redis:/data ports: - 6379:6379 - nginx-rtmp: - # ports: - # - 1935:1935 - # - 8888:8888 - network_mode: host - environment: - UID: 1000 - GID: 1000 - API_AUTH: skibiditoilet - volumes: - - ./nginx.conf:/etc/nginx/templates/nginx.conf.template - - ./html:/var/www/html - - /dev/shm/hls:/dev/shm/hls - image: srizan10/flv-module - entrypoint: - - /bin/sh - - -c - - | - # Process the template file - mkdir -p /usr/local/nginx/conf - envsubst '$${API_AUTH}' < /etc/nginx/templates/nginx.conf.template > /usr/local/nginx/conf/nginx.conf - - echo "Setting UID to $${UID} and GID to $${GID}" - usermod -u $${UID} nginx || echo "failed to change uid" - groupmod -g $${GID} nginx || echo "failed to change gid" - - mkdir -p /usr/local/nginx/proxy_temp /usr/local/nginx/client_body_temp - chown -R nginx:nginx /usr/local/nginx - mkdir -p /var/www/html - chown -R nginx:nginx /var/www/html - - echo "testing nginx config..." - /usr/local/nginx/sbin/nginx -t - - /usr/local/nginx/sbin/nginx -g 'daemon off;' mediamtx: image: bluenviron/mediamtx:latest ports: