...
|
...
|
@@ -20,7 +20,8 @@ module.exports = (limiter, policy) => { |
|
|
|
|
|
// 存储规则的cache keys
|
|
|
let ruleKeys = {};
|
|
|
let getOp = {}
|
|
|
let getOp = {};
|
|
|
|
|
|
_.forEach(MAX_TIMES, (val, key) => {
|
|
|
ruleKeys[key] = `${config.app}:limiter:${key}:max:${limiter.remoteIp}`;
|
|
|
getOp[key] = cache.getAsync(ruleKeys[key]);
|
...
|
...
|
@@ -34,12 +35,13 @@ module.exports = (limiter, policy) => { |
|
|
|
|
|
// 遍历限制规则,若满足返回相应处理策略, 否则页面访问次数加1
|
|
|
let operation = [];
|
|
|
|
|
|
_.forEach(MAX_TIMES, (val, key) => {
|
|
|
let cacheKey = ruleKeys[key];
|
|
|
|
|
|
if (!results[key]) {
|
|
|
operation.push(cache.setAsync(cacheKey, 1, +key));
|
|
|
} else if (+results[cacheKey] > +val) {
|
|
|
} else if (+results[key] > +val) {
|
|
|
|
|
|
// ip限制1小时
|
|
|
operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, limiterIpTime));
|
...
|
...
|
@@ -50,6 +52,7 @@ module.exports = (limiter, policy) => { |
|
|
});
|
|
|
|
|
|
Promise.all(operation);
|
|
|
|
|
|
// 不满足任何限制规则,继续访问
|
|
|
return Promise.resolve(true);
|
|
|
}).catch(err=>{
|
...
|
...
|
|