add ratelimit on saving videos

This commit is contained in:
localhost 2025-11-16 10:57:18 +01:00
parent ddc1bf72d6
commit 0340500cdd
1 changed files with 14 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { Elysia, t } from 'elysia'; import { Elysia, t } from 'elysia';
import { RedisRateLimiter } from 'rolling-rate-limiter'
import * as fs from 'node:fs' import * as fs from 'node:fs'
import { db } from '@/utils/database' import { db } from '@/utils/database'
@ -13,6 +14,13 @@ import redis from '@/utils/redis';
const app = new Elysia() const app = new Elysia()
const videoIds: Record<string, string> = {} const videoIds: Record<string, string> = {}
const limiter = new RedisRateLimiter({
client: redis,
namespace: 'save:',
interval: 24 * 60 * 60000, // 24h
maxInInterval: 100
})
const sendError = (ws: any, message: string) => { const sendError = (ws: any, message: string) => {
ws.send(`ERROR - ${message}`); ws.send(`ERROR - ${message}`);
ws.close(); ws.close();
@ -79,6 +87,12 @@ app.ws('/save', {
ws.send(`DONE - ${process.env.FRONTEND}/watch?v=${videoId}`) ws.send(`DONE - ${process.env.FRONTEND}/watch?v=${videoId}`)
ws.close() ws.close()
} else { } else {
const hash = Bun.hash(ws.data.headers['cf-connecting-ip'] || '0.0.0.0')
const isLimited = await limiter.limit(hash.toString())
if (isLimited) {
return sendError(ws, 'You have been ratelimited. </br>Is this an urgent archive? Please email me: admin@preservetube.com');
}
ws.send('DATA - This process is automatic. Your video will start archiving shortly.') ws.send('DATA - This process is automatic. Your video will start archiving shortly.')
ws.send('CAPTCHA - Solving a cryptographic challenge before downloading.') ws.send('CAPTCHA - Solving a cryptographic challenge before downloading.')
videoIds[ws.id] = videoId videoIds[ws.id] = videoId