Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ internals.defaults = {
limit: -1,
duration: 1
},
redis: {}
redis: {},
whitelist: []
};

var MILLISECONDS = 1000;
Expand All @@ -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,
Expand Down