From 52671cf093589ab8c1cc4bfb0b7b3570fd442c85 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 20 Aug 2014 15:37:25 -0400 Subject: [PATCH] add simple rate limiter --- insight.js | 10 ++++++---- plugins/ratelimiter.js | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/insight.js b/insight.js index acb3547f..8fa23981 100755 --- a/insight.js +++ b/insight.js @@ -111,9 +111,6 @@ if (!config.disableHistoricSync) { if (peerSync) peerSync.allowReorgs = true; -// express settings -require('./config/express')(expressApp, historicSync, peerSync); - // routes require('./config/routes')(expressApp); @@ -131,9 +128,14 @@ if (config.enableRatelimiter) { require('./plugins/ratelimiter').init(expressApp, config.ratelimiter); } + +// express settings +require('./config/express')(expressApp, historicSync, peerSync); + + //Start the app by listening on server.listen(config.port, function() { - console.log('insight server listening on port %d in %s mode', server.address().port, process.env.NODE_ENV); + logger.info('insight server listening on port %d in %s mode', server.address().port, process.env.NODE_ENV); }); //expose app diff --git a/plugins/ratelimiter.js b/plugins/ratelimiter.js index 5628b8de..73748345 100644 --- a/plugins/ratelimiter.js +++ b/plugins/ratelimiter.js @@ -7,4 +7,28 @@ module.exports.init = function(app, config) { preconditions.checkArgument(app); logger.info('Using ratelimiter plugin'); + config = config || {}; + config.whitelistRPH = config.whitelistRPH || 5000; + config.normalRPH = config.normalRPH || 1; + + console.log('asdasdasd'); + app.use(limiter({ + whitelist: [], + blacklist: ['localhost'], // 'example.com' + categories: { + whitelist: { + totalRequests: config.whitelistRPH, + every: 60 * 60 * 1000 + }, + blacklist: { + totalRequests: 0, + every: 0 + }, + normal: { + totalRequests: config.normalRPH, + every: 60 * 60 * 1000 + } + } + })); + };