|
|
/**
|
|
|
* 限制页面访问次数,如超过限制次数,返回相应策略(目前是ip加入黑名单,跳转图形验证码页面,解除限制)
|
|
|
* 当前规则只针对未登录用户
|
|
|
*/
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
const logger = global.yoho.logger;
|
|
|
const cache = global.yoho.cache.master;
|
|
|
const config = global.yoho.config;
|
|
|
const ONE_DAY = 60 * 60 * 24;
|
|
|
const MAX_QPS = config.maxQps;
|
|
|
const MAX_QPS_10m = config.maxQps10m;
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
const PAGES = {
|
|
|
'/product/^\\/(\\d+)\\.html/': 5,
|
|
|
'/product/list/index': 5,
|
|
|
'/product/index/index': 5,
|
|
|
'/product/search/list': 5
|
|
|
};
|
|
|
// 超出访问限制ip限制访问1小时
|
|
|
const limiterIpTime = 3600;
|
|
|
|
|
|
function urlJoin(a, b) {
|
|
|
if (_.endsWith(a, '/') && _.startsWith(b, '/')) {
|
|
|
return a + b.substring(1, b.length);
|
|
|
} else if (!_.endsWith(a, '/') && !_.startsWith(b, '/')) {
|
|
|
return a + '/' + b;
|
|
|
} else {
|
|
|
return a + b;
|
|
|
}
|
|
|
}
|
|
|
// 页面访问限制
|
|
|
const MAX_TIMES = config.REQUEST_LIMIT;
|
|
|
|
|
|
module.exports = (limiter, policy) => {
|
|
|
const req = limiter.req,
|
|
|
res = limiter.res;
|
|
|
|
|
|
const key = `pc:limiter:${limiter.remoteIp}`;
|
|
|
const key10m = `pc:limiter:10m:${limiter.remoteIp}`;
|
|
|
|
|
|
res.on('render', function() {
|
|
|
let route = req.route ? req.route.path : '';
|
|
|
let appPath = req.app.mountpath;
|
|
|
|
|
|
if (_.isArray(route) && route.length > 0) {
|
|
|
route = route[0];
|
|
|
}
|
|
|
// 存储规则的cache keys
|
|
|
let ruleKeys = {};
|
|
|
let getOp = {};
|
|
|
|
|
|
let pageKey = urlJoin(appPath, route.toString()); // route may be a regexp
|
|
|
let pageIncr = PAGES[pageKey] || 0;
|
|
|
|
|
|
if (pageIncr > 0) {
|
|
|
cache.incrAsync(key, pageIncr);
|
|
|
cache.incrAsync(key10m, pageIncr);
|
|
|
}
|
|
|
_.forEach(MAX_TIMES, (val, key) => {
|
|
|
ruleKeys[key] = `${config.app}:limiter:${key}:max:${limiter.remoteIp}`;
|
|
|
getOp[key] = cache.getAsync(ruleKeys[key]);
|
|
|
});
|
|
|
|
|
|
return cache.getMultiAsync([key, key10m]).then((results) => {
|
|
|
let result = results[key];
|
|
|
let result10m = results[key10m];
|
|
|
|
|
|
logger.debug('qps limiter: ' + key + '@' + result + ' max: ' + MAX_QPS);
|
|
|
logger.debug('qps limiter 10m: ' + key10m + '@' + result10m + ' max: ' + MAX_QPS_10m);
|
|
|
|
|
|
// 默认数据设置
|
|
|
if (!result && !_.isNumber(result)) {
|
|
|
cache.setAsync(key, 1, 60); // 设置key,1m失效
|
|
|
}
|
|
|
|
|
|
if (!result10m && !_.isNumber(result10m)) {
|
|
|
cache.setAsync(key10m, 1, 600); // 设置key,10m失效
|
|
|
}
|
|
|
return Promise.props(getOp).then((results) => {
|
|
|
|
|
|
// 第一次访问,都没计数,直接过
|
|
|
if (!result && !_.isNumber(result) && !result10m && !_.isNumber(result10m)) {
|
|
|
return Promise.resolve(true);
|
|
|
}
|
|
|
logger.debug(MAX_TIMES);
|
|
|
logger.debug(_.values(ruleKeys));
|
|
|
logger.debug(results);
|
|
|
|
|
|
if (result === -1 || result10m === -1) {
|
|
|
return Promise.resolve(true);
|
|
|
}
|
|
|
// 遍历限制规则,若满足返回相应处理策略, 否则页面访问次数加1
|
|
|
let operation = [];
|
|
|
|
|
|
// 判断 qps 10分钟
|
|
|
if (result10m > MAX_QPS_10m) {
|
|
|
cache.touch(key10m, ONE_DAY);
|
|
|
logger.debug('req limit', key10m);
|
|
|
_.forEach(MAX_TIMES, (val, key) => {
|
|
|
let cacheKey = ruleKeys[key];
|
|
|
|
|
|
return Promise.resolve(policy);
|
|
|
}
|
|
|
if (!results[key]) {
|
|
|
operation.push(cache.setAsync(cacheKey, 1, +key));
|
|
|
} else if (+results[key] > +val) {
|
|
|
|
|
|
// 判断 qps 1分钟
|
|
|
if (result > MAX_QPS) {
|
|
|
cache.touch(key, ONE_DAY);
|
|
|
logger.debug('req limit', key);
|
|
|
// ip限制1小时
|
|
|
operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, limiterIpTime));
|
|
|
return Promise.resolve(policy);
|
|
|
} else {
|
|
|
operation.push(cache.incrAsync(cacheKey, 1));
|
|
|
}
|
|
|
});
|
|
|
|
|
|
return Promise.resolve(policy);
|
|
|
}
|
|
|
Promise.all(operation);
|
|
|
|
|
|
cache.incrAsync(key, 1); // qps + 1
|
|
|
cache.incrAsync(key10m, 1); // qps + 1
|
|
|
// 不满足任何限制规则,继续访问
|
|
|
return Promise.resolve(true);
|
|
|
}).catch(err=>{
|
|
|
logger.error(err);
|
|
|
});
|
|
|
}; |
...
|
...
|
|