mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 15:36:16 -04:00
Defer rate limiter creation to a hook call
This makes it possible to change the rate limiter settings via `/admin/settings` or by modifying the appropriate settings object and reinvoking the hook.
This commit is contained in:
parent
d7ed71eba0
commit
00d45e3229
3 changed files with 18 additions and 13 deletions
|
@ -84,7 +84,8 @@
|
||||||
"name": "socketio",
|
"name": "socketio",
|
||||||
"hooks": {
|
"hooks": {
|
||||||
"expressCloseServer": "ep_etherpad-lite/node/hooks/express/socketio",
|
"expressCloseServer": "ep_etherpad-lite/node/hooks/express/socketio",
|
||||||
"expressCreateServer": "ep_etherpad-lite/node/hooks/express/socketio"
|
"expressCreateServer": "ep_etherpad-lite/node/hooks/express/socketio",
|
||||||
|
"socketio": "ep_etherpad-lite/node/handler/PadMessageHandler"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -40,10 +40,14 @@ const nodeify = require('nodeify');
|
||||||
const {RateLimiterMemory} = require('rate-limiter-flexible');
|
const {RateLimiterMemory} = require('rate-limiter-flexible');
|
||||||
const webaccess = require('../hooks/express/webaccess');
|
const webaccess = require('../hooks/express/webaccess');
|
||||||
|
|
||||||
const rateLimiter = new RateLimiterMemory({
|
let rateLimiter;
|
||||||
points: settings.commitRateLimiting.points,
|
|
||||||
duration: settings.commitRateLimiting.duration,
|
exports.socketio = () => {
|
||||||
});
|
// The rate limiter is created in this hook so that restarting the server resets the limiter. The
|
||||||
|
// settings.commitRateLimiting object is passed directly to the rate limiter so that the limits
|
||||||
|
// can be dynamically changed during runtime by modifying its properties.
|
||||||
|
rateLimiter = new RateLimiterMemory(settings.commitRateLimiting);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A associative array that saves information about a session
|
* A associative array that saves information about a session
|
||||||
|
|
|
@ -10,15 +10,15 @@ const rateLimit = require('express-rate-limit');
|
||||||
const securityManager = require('../../db/SecurityManager');
|
const securityManager = require('../../db/SecurityManager');
|
||||||
const webaccess = require('./webaccess');
|
const webaccess = require('./webaccess');
|
||||||
|
|
||||||
settings.importExportRateLimiting.onLimitReached = (req, res, options) => {
|
|
||||||
// when the rate limiter triggers, write a warning in the logs
|
|
||||||
console.warn('Import/Export rate limiter triggered on ' +
|
|
||||||
`"${req.originalUrl}" for IP address ${req.ip}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
const limiter = rateLimit(settings.importExportRateLimiting);
|
|
||||||
|
|
||||||
exports.expressCreateServer = (hookName, args, cb) => {
|
exports.expressCreateServer = (hookName, args, cb) => {
|
||||||
|
settings.importExportRateLimiting.onLimitReached = (req, res, options) => {
|
||||||
|
// when the rate limiter triggers, write a warning in the logs
|
||||||
|
console.warn('Import/Export rate limiter triggered on ' +
|
||||||
|
`"${req.originalUrl}" for IP address ${req.ip}`);
|
||||||
|
};
|
||||||
|
// The rate limiter is created in this hook so that restarting the server resets the limiter.
|
||||||
|
const limiter = rateLimit(settings.importExportRateLimiting);
|
||||||
|
|
||||||
// handle export requests
|
// handle export requests
|
||||||
args.app.use('/p/:pad/:rev?/export/:type', limiter);
|
args.app.use('/p/:pad/:rev?/export/:type', limiter);
|
||||||
args.app.get('/p/:pad/:rev?/export/:type', (req, res, next) => {
|
args.app.get('/p/:pad/:rev?/export/:type', (req, res, next) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue