Authored by htoooth

change limit

... ... @@ -16,7 +16,7 @@ const index = (req, res, next) => {
const check = captchaService.requiredAPI;
const isHuman = (req, res, next) => {
const isHuman = (req, res) => {
let remoteIp = req.get('X-Forwarded-For') || req.ip;
if (remoteIp.indexOf(',') > 0) {
... ... @@ -26,11 +26,11 @@ const isHuman = (req, res, next) => {
}
robotCheckService.removeBlack(remoteIp).then(() => {
res.json({
return res.json({
code: 200
});
}).catch(err => {
res.json({
}).catch(() => {
return res.json({
code: 400
});
});
... ...
... ... @@ -14,11 +14,11 @@ const index = co(function* (channel) {
};
});
const removeBlack = co(function* (remoteIp) {
const removeBlack = (remoteIp) => {
let key = `pc:limiter:${remoteIp}`;
return cache.delAsync(key);
});
};
module.exports = {
index,
... ...
<div class="robot-check-page yoho-page">
<div class="captcha">
<div class="title">请输入正确的验证码,继续访问</div>
<div class="captcha-wrap">
</div>
<div class="captcha-wrap"> </div>
<a class="btn confirm">确定</a>
</div>
</div>
... ...
... ... @@ -117,7 +117,8 @@ module.exports = {
apiCache: {
cache: false
},
zookeeperServer: '192.168.102.168:2188'
zookeeperServer: '192.168.102.168:2188',
maxQps: 1800
};
if (isProduction) {
... ...
... ... @@ -4,9 +4,11 @@
const cache = global.yoho.cache.master;
const _ = require('lodash');
const logger = global.yoho.logger;
const config = global.yoho.config;
const helpers = global.yoho.helpers;
let ONE_DAY = 60 * 60 * 24;
const MAX_QPS = config.maxQps;
let pages = {
'/product/\\/pro_([\\d]+)_([\\d]+)\\/(.*)/': 5,
... ... @@ -35,8 +37,6 @@ module.exports = (req, res, next) => {
if (remoteIp && !_.get(req.app.locals, 'pc.sys.noLimiter')) { // 判断获取remoteIp成功,并且开关未关闭
let key = `pc:limiter:${remoteIp}`;
logger.debug(`request limiter key=${key}`);
res.on('render', function() {
let route = req.route ? req.route.path : '';
let appPath = req.app.mountpath;
... ... @@ -53,12 +53,11 @@ module.exports = (req, res, next) => {
}
});
function limiterPage() {
const limiterPage = () => {
let refer = req.method === 'GET' ? req.get('Referer') : '';
let limitAPI = helpers.urlFormat('/3party/check', {refer: refer});
let limitPage = helpers.urlFormat('/3party/check', {refer: req.protocol + '://' + req.get('host') + req.originalUrl});
if (_.indexOf(['/3party/check', '/passport/imagesNode', '/passport/cert/headerTip'], req.path) >= 0) {
return next();
}
... ... @@ -71,24 +70,23 @@ module.exports = (req, res, next) => {
}
return res.redirect(limitPage);
}
};
cache.getAsync(key).then(result => {
cache.getAsync(key).then((result) => {
if (result && _.isNumber(result)) {
if (result > 1800) { // 判断 qps
if (result > MAX_QPS) { // 判断 qps
cache.touch(key, ONE_DAY);
console.log('req limit', key);
return limiterPage();
} else {
cache.incrAsync(key, 1); // qps + 1
return next();
}
} else {
cache.setAsync(key, 1, 60); // 设置key,1s失效
cache.setAsync(key, 1, 60); // 设置key,1m失效
return next();
}
}).catch(e => {
logger.error(`request limiter get key[${key}] from cache error.`, e);
}).catch((err) => {
logger.error(`request limiter get key[${key}] from cache error.`, err);
return next();
});
} else {
... ...