diff --git a/lib/index.js b/lib/index.js index ef08165..34c6f3e 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,15 +24,23 @@ 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; var routeLimit = route.settings.plugins && route.settings.plugins['hapi-ratelimit']; 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 + ':' + request.info.remoteAddress + ':' + route.path; + var ipts = settings.namespace + ':' + ip + ':' + request.method + ':' + route.path; var routeLimiter = new Limiter({ id: ipts, db: redisClient,