Skip to content

Commit

Permalink
Add rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
arvida42 committed Mar 30, 2024
1 parent 83fc59a commit c0f9666
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jackettio",
"version": "1.1.17",
"version": "1.1.18",
"description": "Jackett and Debrid on Stremio",
"main": "src/index.js",
"type": "module",
Expand All @@ -18,6 +18,7 @@
"cache-manager-sqlite": "^0.2.0",
"compression": "^1.7.4",
"express": "^4.18.2",
"express-rate-limit": "^7.2.0",
"localtunnel": "^2.0.2",
"p-limit": "^5.0.0",
"parse-torrent": "^11.0.16",
Expand Down
27 changes: 24 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import showdown from 'showdown';
import compression from 'compression';
import express from 'express';
import localtunnel from 'localtunnel';
import { rateLimit } from 'express-rate-limit';
import {readFileSync} from "fs";
import config from './lib/config.js';
import cache, {vacuum as vacuumCache, clean as cleanCache} from './lib/cache.js';
Expand All @@ -22,7 +23,27 @@ const respond = (res, data) => {
res.setHeader('Access-Control-Allow-Headers', '*')
res.setHeader('Content-Type', 'application/json')
res.send(data)
}
};

const limiter = rateLimit({
windowMs: config.rateLimitWindow * 1000,
max: config.rateLimitRequest,
legacyHeaders: false,
standardHeaders: 'draft-7',
keyGenerator: (req) => req.clientIp || req.ip,
handler: (req, res, next, options) => {
if(req.route.path == '/:userConfig/stream/:type/:id.json'){
const resetInMs = new Date(req.rateLimit.resetTime) - new Date();
return res.json({streams: [{
name: `${config.addonName}`,
title: `🛑 Too many requests, please try in ${Math.ceil(resetInMs / 1000 / 60)} minute(s).`,
url: '#'
}]})
}else{
return res.status(options.statusCode).send(options.message);
}
}
});

app.set('trust proxy', config.trustProxy);

Expand Down Expand Up @@ -111,7 +132,7 @@ app.get("/:userConfig?/manifest.json", async(req, res) => {
respond(res, manifest);
});

app.get("/:userConfig/stream/:type/:id.json", async(req, res) => {
app.get("/:userConfig/stream/:type/:id.json", limiter, async(req, res) => {

try {

Expand Down Expand Up @@ -234,7 +255,7 @@ const server = app.listen(config.port, async () => {
setInterval(cleanTorrentFolder, 3600e3);

vacuumCache().catch(err => console.log(`Failed to vacuum cache: ${err}`));
setInterval(() => vacuumCache(), 86400e3*2);
setInterval(() => vacuumCache(), 86400e3*7);

cleanCache().catch(err => console.log(`Failed to clean cache: ${err}`));
setInterval(() => cleanCache(), 3600e3);
Expand Down
4 changes: 4 additions & 0 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default {
welcomeMessage: process.env.WELCOME_MESSAGE || '',
// Trust the cf-connecting-ip header
trustCfIpHeader: (process.env.TRUST_CF_IP_HEADER || 'false') === 'true',
// Rate limit interval in seconds to resolve stream
rateLimitWindow: parseInt(process.env.RATE_LIMIT_WINDOW || 60 * 60),
// Rate limit the number of requests to resolve stream
rateLimitRequest: parseInt(process.env.RATE_LIMIT_REQUEST || 150),

defaultUserConfig: {
qualities: commaListToArray(process.env.DEFAULT_QUALITIES || '0, 720, 1080').map(v => parseInt(v)),
Expand Down

0 comments on commit c0f9666

Please sign in to comment.