From dacc8f169bb98e879168446ca116d6221cf3ed86 Mon Sep 17 00:00:00 2001 From: Rico Chen Date: Wed, 18 Nov 2015 17:18:21 -0500 Subject: [PATCH 1/2] added redis authentication option, added whitelist ip option --- lib/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index ef08165..585fc36 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,7 +13,8 @@ internals.defaults = { limit: -1, duration: 1 }, - redis: {} + redis: {}, + whitelist: [] }; var MILLISECONDS = 1000; @@ -23,6 +24,9 @@ exports.name = 'hapi-ratelimit'; exports.register = function(plugin, options, next) { var settings = Hoek.applyToDefaults(internals.defaults, options); var redisClient = redis.createClient(options.redis.port, options.redis.host, options.redis.options); + if (options.redis.password) { + redisClient.auth(options.redis.password); + } plugin.ext('onPreAuth', function(request, reply) { var route = request.route; @@ -30,8 +34,12 @@ exports.register = function(plugin, options, next) { if (!routeLimit && settings.global.limit > 0) { routeLimit = settings.global; } + var ip = request.info.remoteAddress; + if( settings.whitelist.indexOf(ip)>-1 ) { + return reply.continue(); + } if (routeLimit) { - var ipts = settings.namespace + ':' + request.info.remoteAddress + ':' + route.path; + var ipts = settings.namespace + ':' + ip + ':' + route.path; var routeLimiter = new Limiter({ id: ipts, db: redisClient, From 1809bf4b257912e8207987cb0822b44721a6a75c Mon Sep 17 00:00:00 2001 From: Rico Chen Date: Mon, 30 Nov 2015 20:46:40 -0500 Subject: [PATCH 2/2] added request.method to key --- lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 585fc36..34c6f3e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,12 +34,13 @@ exports.register = function(plugin, options, next) { if (!routeLimit && settings.global.limit > 0) { routeLimit = settings.global; } + console.log(routeLimit); var ip = request.info.remoteAddress; if( settings.whitelist.indexOf(ip)>-1 ) { return reply.continue(); } if (routeLimit) { - var ipts = settings.namespace + ':' + ip + ':' + route.path; + var ipts = settings.namespace + ':' + ip + ':' + request.method + ':' + route.path; var routeLimiter = new Limiter({ id: ipts, db: redisClient,