Authored by yyq

Merge branch 'hotfix/limit' into release/0815

@@ -61,8 +61,7 @@ const submitValidate = { @@ -61,8 +61,7 @@ const submitValidate = {
61 remoteIp = arr[0]; 61 remoteIp = arr[0];
62 } 62 }
63 63
64 - // pc:limiter:IP 和PC端共用  
65 - let operations = [cache.delAsync(`pc:limiter:${remoteIp}`)]; 64 + let operations = [cache.delAsync(`${config.app}:limiter:${remoteIp}`)];
66 65
67 // 验证码之后一小时之内不再限制qps 66 // 验证码之后一小时之内不再限制qps
68 if (req.session.apiLimitValidate || req.session.apiRiskValidate) { 67 if (req.session.apiLimitValidate || req.session.apiRiskValidate) {
1 'use strict'; 1 'use strict';
2 2
3 const cache = global.yoho.cache.master; 3 const cache = global.yoho.cache.master;
4 -const _ = require('lodash');  
5 const logger = global.yoho.logger; 4 const logger = global.yoho.logger;
  5 +const config = global.yoho.config;
6 6
7 module.exports = (limiter, policy) => { 7 module.exports = (limiter, policy) => {
8 - // 和pc共用  
9 - const key = `pc:limiter:${limiter.remoteIp}`; 8 + const ipBlackKey = `pc:limiter:${limiter.remoteIp}`; // ci ip黑名单
  9 + const ipLimitKey = `${config.app}:limiter:${limiter.remoteIp}`; // 业务黑名单
10 10
11 - return cache.getAsync(key).then((result) => {  
12 - logger.debug(key, result); 11 + return Promise.all([
  12 + cache.getAsync(ipBlackKey),
  13 + cache.getAsync(ipLimitKey)
  14 + ]).then(result => {
  15 + let ipBlackRes = result[0];
  16 + let ipLimitRes = result[1];
13 17
14 - if (result && _.isNumber(result) && result !== -1) { 18 + logger.debug(ipBlackKey, ipBlackRes);
  19 + logger.debug(ipLimitKey, ipLimitRes);
  20 +
  21 + if ((ipBlackRes && +ipBlackRes > 0) || (ipLimitRes && +ipLimitRes > 0)) {
15 return Promise.resolve(policy); 22 return Promise.resolve(policy);
16 } else { 23 } else {
17 return Promise.resolve(true); 24 return Promise.resolve(true);
@@ -45,10 +45,10 @@ module.exports = (limiter, policy) => { @@ -45,10 +45,10 @@ module.exports = (limiter, policy) => {
45 if (!results[key]) { 45 if (!results[key]) {
46 operation.push(cache.setAsync(cacheKey, 1, +key)); 46 operation.push(cache.setAsync(cacheKey, 1, +key));
47 } else if (+results[key] > +val) { 47 } else if (+results[key] > +val) {
48 - logger.warn(`pc:limiter:${limiter.remoteIp}`); 48 + logger.warn(`${config.app}:limiter:${limiter.remoteIp}`);
49 49
50 // ip限制1小时 50 // ip限制1小时
51 - operation.push(cache.setAsync(`pc:limiter:${limiter.remoteIp}`, 1, config.LIMITER_IP_TIME)); 51 + operation.push(cache.setAsync(`${config.app}:limiter:${limiter.remoteIp}`, 1, config.LIMITER_IP_TIME));
52 return Promise.resolve(policy); 52 return Promise.resolve(policy);
53 } else { 53 } else {
54 operation.push(cache.incrAsync(cacheKey, 1)); 54 operation.push(cache.incrAsync(cacheKey, 1));
@@ -9,6 +9,7 @@ const cache = global.yoho.cache.master; @@ -9,6 +9,7 @@ const cache = global.yoho.cache.master;
9 const helpers = global.yoho.helpers; 9 const helpers = global.yoho.helpers;
10 const pathToRegexp = require('path-to-regexp'); 10 const pathToRegexp = require('path-to-regexp');
11 const logger = global.yoho.logger; 11 const logger = global.yoho.logger;
  12 +const md5 = require('yoho-md5');
12 13
13 const statusCode = { 14 const statusCode = {
14 code: 4403, 15 code: 4403,
@@ -83,7 +84,7 @@ module.exports = () => { @@ -83,7 +84,7 @@ module.exports = () => {
83 return next(); 84 return next();
84 } 85 }
85 86
86 - let keyPath = `${_.trim(path, '/').replace(/\//g, ':')}`; 87 + let keyPath = md5(`${router.regRoute}`);
87 let limitKey = `wap:risk:limit:${keyPath}:${ip}`; 88 let limitKey = `wap:risk:limit:${keyPath}:${ip}`;
88 let configKey = `wap:risk:${keyPath}:${ip}`; 89 let configKey = `wap:risk:${keyPath}:${ip}`;
89 let checkUrl = helpers.urlFormat('/3party/check', { 90 let checkUrl = helpers.urlFormat('/3party/check', {