|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | var redis = require('redis'); |
| 4 | +var Limiter = require('ratelimiter'); |
4 | 5 |
|
5 | 6 | var internals = {}; |
6 | 7 |
|
7 | 8 | internals.defaults = { |
8 | 9 | namespace: 'clhr', |
9 | 10 | global: { |
10 | 11 | limit: -1, |
11 | | - bucketLength: 1 |
| 12 | + duration: 1 |
12 | 13 | }, |
13 | 14 | redis: {} |
14 | 15 | }; |
15 | 16 |
|
| 17 | +var MILLISECONDS = 1000; |
| 18 | + |
16 | 19 | exports.register = function(plugin, options, next) { |
17 | 20 | var settings = plugin.hapi.utils.applyToDefaults(internals.defaults, options); |
18 | 21 | var redisClient = redis.createClient(options.redis.port, options.redis.host, options.redis.options); |
19 | 22 |
|
20 | 23 | plugin.ext('onPreAuth', function(request, callback) { |
21 | 24 | var route = request.route; |
22 | | - var limit = route.plugins && route.plugins['hapi-ratelimit']; |
23 | | - if (!limit && settings.global.limit > 0) { |
24 | | - limit = settings.global; |
| 25 | + var routeLimit = route.plugins && route.plugins['hapi-ratelimit']; |
| 26 | + if (!routeLimit && settings.global.limit > 0) { |
| 27 | + routeLimit = settings.global; |
25 | 28 | } |
26 | | - if (limit) { |
| 29 | + if (routeLimit) { |
27 | 30 | var ipts = settings.namespace + ':' + request.info.remoteAddress + ':' + route.path; |
| 31 | + var routeLimiter = new Limiter({ |
| 32 | + id: ipts, |
| 33 | + db: redisClient, |
| 34 | + max: routeLimit.limit, |
| 35 | + duration: routeLimit.duration * MILLISECONDS |
| 36 | + }); |
28 | 37 | var error = null; |
29 | | - redisClient.get(ipts, function(err, token) { |
| 38 | + routeLimiter.get(function(err, rateLimit) { |
30 | 39 | if (err) { |
31 | 40 | return callback(err); |
32 | 41 | } |
33 | 42 | request.plugins['hapi-ratelimit'] = {}; |
34 | | - request.plugins['hapi-ratelimit'].limit = limit.limit; |
35 | | - var left = limit.limit - token; |
36 | | - request.plugins['hapi-ratelimit'].remaining = left > 0 ? left - 1 : 0; //they've already reached it at this point |
37 | | - request.plugins['hapi-ratelimit'].reset = limit.bucketLength; |
| 43 | + request.plugins['hapi-ratelimit'].limit = rateLimit.total; |
| 44 | + request.plugins['hapi-ratelimit'].remaining = rateLimit.remaining; |
| 45 | + request.plugins['hapi-ratelimit'].reset = rateLimit.reset; |
38 | 46 |
|
39 | | - if (token) { |
40 | | - if (token >= limit.limit) { |
41 | | - error = plugin.hapi.error.badRequest('Rate limit exceeded'); |
42 | | - error.output.statusCode = 429; // Assign a Too Many Requests response |
43 | | - error.output.headers['X-Rate-Limit-Limit'] = request.plugins['hapi-ratelimit'].limit; |
44 | | - error.output.headers['X-Rate-Limit-Remaining'] = request.plugins['hapi-ratelimit'].remaining; |
45 | | - error.output.headers['X-Rate-Limit-Reset'] = request.plugins['hapi-ratelimit'].reset; |
46 | | - error.reformat(); |
47 | | - } |
48 | | - redisClient.incr(ipts, function(err) { |
49 | | - if (err) { |
50 | | - return callback(err); |
51 | | - } |
52 | | - return callback(error); |
53 | | - }); |
54 | | - } else { |
55 | | - redisClient.multi([ |
56 | | - ['INCR', ipts], |
57 | | - ['EXPIRE', ipts, limit.bucketLength] |
58 | | - ]).exec(function(err) { |
59 | | - if (err) { |
60 | | - return callback(err); |
61 | | - } |
62 | | - return callback(); |
63 | | - }); |
| 47 | + if (rateLimit.remaining <= 0) { |
| 48 | + error = plugin.hapi.error.badRequest('Rate limit exceeded'); |
| 49 | + error.output.statusCode = 429; // Assign a Too Many Requests response |
| 50 | + error.output.headers['X-Rate-Limit-Limit'] = request.plugins['hapi-ratelimit'].limit; |
| 51 | + error.output.headers['X-Rate-Limit-Remaining'] = request.plugins['hapi-ratelimit'].remaining; |
| 52 | + error.output.headers['X-Rate-Limit-Reset'] = request.plugins['hapi-ratelimit'].reset; |
| 53 | + error.reformat(); |
64 | 54 | } |
| 55 | + return callback(error); |
65 | 56 | }); |
66 | 57 | } else { |
67 | 58 | return callback(); |
|
0 commit comments