Authored by yyq

api limit withe path

... ... @@ -10,6 +10,7 @@ const sender = global.yoho.apmSender;
const config = global.yoho.config;
const hostname = require('os').hostname();
const routeEncode = require('./route-encode');
const pathWhiteList = require('./limiter/rules/path-white-list');
const _ = require('lodash');
const limiterIpTime = 3600;
... ... @@ -100,31 +101,34 @@ exports.serverError = () => {
if (err.code === 9999991 || err.code === 9999992) {
let remoteIp = req.yoho.clientIp;
const isHuman = await cache.getAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`);
if (!_.includes(pathWhiteList(), req.path)) {
const isHuman = await cache.getAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`);
if (!isHuman) {
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
if (!isHuman) {
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = arr[0];
}
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
let limitAPI = helpers.urlFormat('/3party/check', {refer: req.get('Referer') || ''});
let limitPage = helpers.urlFormat('/3party/check', {
refer: req.protocol + '://' + req.get('host') + req.originalUrl
});
remoteIp = arr[0];
}
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
req.session.apiLimitValidate = true;
if (req.xhr) {
return res.status(510).json({
code: err.code,
data: {refer: limitAPI}
let limitAPI = helpers.urlFormat('/3party/check', {refer: req.get('Referer') || ''});
let limitPage = helpers.urlFormat('/3party/check', {
refer: req.protocol + '://' + req.get('host') + req.originalUrl
});
}
return res.redirect(limitPage);
req.session.apiLimitValidate = true;
if (req.xhr) {
return res.status(510).json({
code: err.code,
data: {refer: limitAPI}
});
}
return res.redirect(limitPage);
}
}
errorCode = 510;
}
... ... @@ -132,7 +136,7 @@ exports.serverError = () => {
if (req.xhr) {
return res.status(errorCode).json({
code: errorCode,
message: '服务器错误!'
message: err.message || `服务器${errorCode === 510 ? '繁忙' : '错误'}!`
});
}
... ... @@ -143,7 +147,7 @@ exports.serverError = () => {
module: 'common',
page: 'error',
err: err,
title: '服务器错误 | Yoho!Buy有货 | 潮流购物逛不停',
title: `服务器${errorCode === 510 ? '繁忙' : '错误'} | Yoho!Buy有货 | 潮流购物逛不停`,
headerData: result.headerData
});
};
... ...
... ... @@ -5,6 +5,7 @@ const logger = global.yoho.logger;
const ip = require('./rules/ip-list');
const userAgent = require('./rules/useragent');
const ipWhiteList = require('./rules/ip-white-list');
const pathWhiteList = require('./rules/path-white-list');
const qpsLimiter = require('./rules/qps-limit');
const co = Promise.coroutine;
... ... @@ -34,17 +35,6 @@ const IP_WHITE_SEGMENT = [
'192.168.' // 内网IP段
];
const PATH_WHITE_LIST = [
'/3party/check',
'/passport/images.png',
'/passport/cert/headerTip',
'/common/getbanner',
'/common/suggestfeedback',
'/product/search/history',
'/product/search/suggest'
];
const limiter = (rule, policy, context) => {
return rule(context, policy);
};
... ... @@ -61,7 +51,7 @@ const _excluded = (req) => {
atWhiteList ||
_.includes(IP_WHITE_LIST, remoteIp) ||
_.includes(IP_WHITE_SEGMENT, remoteIpSegment) ||
_.includes(PATH_WHITE_LIST, req.path) ||
_.includes(pathWhiteList(), req.path) ||
req.xhr ||
!_.isEmpty(_.get(req, 'user.uid'))
);
... ...
const _ = require('lodash');
const logger = global.yoho.logger;
const cache = global.yoho.cache.master;
const WHITE_LIST_KEY = 'pc:limiter:whitelist:path';
const DEFAULT_PATH_WHITE_LIST = [
'/3party/check',
'/passport/images.png',
'/passport/cert/headerTip',
'/common/getbanner',
'/common/suggestfeedback',
'/product/search/history',
'/product/search/suggest'
];
const cacheWhiteList = {
nowTime() {
return Date.parse(new Date()) / 1000;
},
getValue() {
if (this.updateTime || this.nowTime() - this.updateTime > 60 * 10) {
this.syncRemoteConfig();
}
return _.uniq(_.concat([], DEFAULT_PATH_WHITE_LIST, _.toArray(this.whiteList)));
},
syncRemoteConfig() {
if (this.syncing) {
return;
}
this.syncing = true;
cache.getAsync(WHITE_LIST_KEY).then(res => {
this.updateTime = this.nowTime();
if (!res) {
return;
}
this.whiteList = JSON.parse(res);
this.syncing = false;
}).catch((e) => {
this.syncing = false;
logger.debug('whitelist path parse error. ' + JSON.stringfy(e));
});
}
};
module.exports = () => {
return cacheWhiteList.getValue();
};
... ...