add rate limits
This commit is contained in:
parent
c9e9debd70
commit
2a873f38eb
|
@ -3,20 +3,18 @@ const router = express.Router();
|
||||||
const handler = require('express-async-handler');
|
const handler = require('express-async-handler');
|
||||||
const content = require('../helpers/content');
|
const content = require('../helpers/content');
|
||||||
|
|
||||||
const memcache = require('memcached');
|
|
||||||
const cache = new memcache(process.env.SESSION_MEMCACHE_HOST);
|
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
const { MAXFILES, APP_DEBUG, RATE_LIMIT } = process.env;
|
const { MAXFILES, APP_DEBUG, RATE_LIMIT } = process.env;
|
||||||
|
|
||||||
let ratelimits = {};
|
var ratelimits = {};
|
||||||
|
|
||||||
async function upload(req, res) {
|
async function upload(req, res) {
|
||||||
|
|
||||||
if (!ratelimits[req.ip])
|
let rate_lim = ratelimits[req.ip];
|
||||||
ratelimits[req.ip] = Date.now();
|
|
||||||
|
|
||||||
if (Date.now() - ratelimits[req.ip] < RATE_LIMIT) {
|
if (rate_lim)
|
||||||
|
if (Date.now() - rate_lim < RATE_LIMIT) {
|
||||||
res.status(503).send('You are being rate limited.');
|
res.status(503).send('You are being rate limited.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -50,6 +48,7 @@ async function upload(req, res) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ratelimits[req.ip] = Date.now();
|
||||||
const id = await content.create(data);
|
const id = await content.create(data);
|
||||||
res.redirect(
|
res.redirect(
|
||||||
'/view?id=' + encodeURIComponent(id)
|
'/view?id=' + encodeURIComponent(id)
|
||||||
|
|
Loading…
Reference in New Issue