refactor: optimize authentication requests

This commit is contained in:
2025-12-15 21:47:00 +01:00
parent 8e8c58e195
commit f9d11476bf
6 changed files with 64 additions and 55 deletions

View File

@@ -56,6 +56,9 @@ const nextConfig = {
},
];
},
logging: {
incomingRequests: false,
},
};
export default withSentryConfig(nextConfig, {

View File

@@ -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 });

View File

@@ -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: {

View File

@@ -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') {

View File

@@ -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);
}
}

View File

@@ -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: