...
|
...
|
@@ -6,18 +6,13 @@ |
|
|
|
|
|
const _ = require('lodash');
|
|
|
const cache = global.yoho.cache.master;
|
|
|
const config = global.yoho.config;
|
|
|
const logger = global.yoho.logger;
|
|
|
const md5 = require('md5');
|
|
|
const pathToRegexp = require('path-to-regexp');
|
|
|
const Promise = require('bluebird');
|
|
|
|
|
|
const zk = {};
|
|
|
|
|
|
if (config.zookeeperServer) {
|
|
|
require('yoho-zookeeper')(config.zookeeperServer, 'pc', zk.pc = {}, global.yoho.cache);
|
|
|
require('yoho-zookeeper')(config.zookeeperServer, 'wap', zk.wap = {}, global.yoho.cache);
|
|
|
}
|
|
|
const zk = require('./zk');
|
|
|
const {limitKey} = require('./vars');
|
|
|
|
|
|
const INVALIDTIME = 3600 * 24; // 24h
|
|
|
const IP_WHITE_LIST = [
|
...
|
...
|
@@ -29,8 +24,6 @@ const IP_WHITE_LIST = [ |
|
|
'218.94.77.166'
|
|
|
];
|
|
|
|
|
|
const key = 'risk1';
|
|
|
|
|
|
module.exports = async({user}, next) => {
|
|
|
if (!user.app || !user.path || !user.ip) {
|
|
|
return next();
|
...
|
...
|
@@ -44,10 +37,10 @@ module.exports = async({user}, next) => { |
|
|
|
|
|
const ip = user.ip;
|
|
|
const path = user.path;
|
|
|
// const risks = _.get(zk, `${app}.json.risk`, [{route: '/product/(.*).html', interval: 5000, requests: 10}]);
|
|
|
const risks = _.get(zk, `${app}.json.risk`, []);
|
|
|
let router = {};
|
|
|
|
|
|
logger.debug(`risk => risks: ${JSON.stringify(risks)}, path: ${path}, ip: ${ip}`); // eslint-disable-line
|
|
|
if (_.isEmpty(path) || _.isEmpty(risks) || IP_WHITE_LIST.indexOf(ip) > -1) {
|
|
|
return next();
|
|
|
}
|
...
|
...
|
@@ -71,47 +64,43 @@ module.exports = async({user}, next) => { |
|
|
return false;
|
|
|
});
|
|
|
|
|
|
logger.debug(`risk => router: ${JSON.stringify(router)}, path: ${path}`); // eslint-disable-line
|
|
|
logger.debug(`risk==> router: ${JSON.stringify(router)}, path: ${path}`); // eslint-disable-line
|
|
|
|
|
|
if (_.isEmpty(router)) {
|
|
|
return next();
|
|
|
}
|
|
|
|
|
|
let keyPath = md5(`${router.regRoute}`);
|
|
|
let limitKey = `${app}:${key}:limit:${keyPath}:${ip}`; // 查询这个key是否生效
|
|
|
let configKey = `${app}:${key}:${keyPath}:${ip}`;
|
|
|
let limitEnable = `${app}:risk:${limitKey}:${keyPath}:${ip}`; // 查询这个key是否生效
|
|
|
let configKey = `${app}:risk:count:${keyPath}:${ip}`;
|
|
|
|
|
|
await Promise.all([
|
|
|
cache.getAsync(limitKey),
|
|
|
const inters = await Promise.all([
|
|
|
cache.getAsync(limitEnable),
|
|
|
cache.getAsync(configKey),
|
|
|
]).then(inters => {
|
|
|
logger.debug(`risk => getCache: ${JSON.stringify(inters)}, path: ${path}`); // eslint-disable-line
|
|
|
if (inters[0]) {
|
|
|
logger.info('[qps:route] this user[%o] has rejected', user);
|
|
|
return;
|
|
|
}
|
|
|
]);
|
|
|
|
|
|
if (typeof inters[1] === 'undefined') {
|
|
|
cache.setAsync(configKey, 1, router.interval || 300);
|
|
|
return;
|
|
|
}
|
|
|
logger.debug(`risk==> cache: %s %d`, limitEnable, inters[0], configKey, inters[1]); // eslint-disable-line
|
|
|
|
|
|
inters[1] = parseInt(`0${inters[1]}`, 10);
|
|
|
if (inters[1] <= router.requests) {
|
|
|
router = [];
|
|
|
cache.incrAsync(configKey, 1);
|
|
|
return;
|
|
|
}
|
|
|
if (inters[0]) {
|
|
|
logger.info('[qps:route] this user[%j] has rejected', user);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
logger.warn('[qps:route] this user[%o] is being marked as rejected', user);
|
|
|
return Promise.all([
|
|
|
cache.setAsync(limitKey, 1, INVALIDTIME),
|
|
|
cache.delAsync(configKey)
|
|
|
]);
|
|
|
}).then(result => {
|
|
|
logger.debug('[qps:route] user[%o] result[%o]', user, result); // eslint-disable-line
|
|
|
}).catch(e => {
|
|
|
logger.error(`risk => path: ${path}, err: ${e.message}`);
|
|
|
}).finally(() => {
|
|
|
if (typeof inters[1] === 'undefined') {
|
|
|
cache.setAsync(configKey, 1, router.interval || 300);
|
|
|
return next();
|
|
|
});
|
|
|
}
|
|
|
|
|
|
inters[1] = parseInt(`0${inters[1]}`, 10);
|
|
|
if (inters[1] <= router.requests) {
|
|
|
router = [];
|
|
|
cache.incrAsync(configKey, 1);
|
|
|
return next();
|
|
|
}
|
|
|
|
|
|
logger.info('[qps:route] this user[%j] is being marked as rejected', user);
|
|
|
await Promise.all([
|
|
|
cache.setAsync(limitEnable, 1, INVALIDTIME),
|
|
|
cache.delAsync(configKey)
|
|
|
]);
|
|
|
}; |
...
|
...
|
|