Authored by 姜枫

fix limiter page

... ... @@ -25,9 +25,15 @@ const isHuman = (req, res, next) => {
remoteIp = arr[0];
}
robotCheckService.removeBlack(remoteIp).then((result) => {
return res.json(result);
}).catch(next);
robotCheckService.removeBlack(remoteIp).then(() => {
res.json({
code: 200
});
}).catch(err => {
res.json({
code: 400
});
});
};
module.exports = {
... ... @@ -35,5 +41,3 @@ module.exports = {
check,
isHuman
};
... ...
'use strict';
const cache = global.yoho.cache.master;
const Promise = require('bluebird');
const co = Promise.coroutine;
const HeaderModel = require('../../../doraemon/models/header');
const index = co(function * (channel) {
const index = co(function* (channel) {
const header = yield HeaderModel.requestHeaderData(channel);
return {
... ... @@ -13,10 +14,10 @@ const index = co(function * (channel) {
};
});
const removeBlack = co(function * (ip) {
return {
code: 200
};
const removeBlack = co(function* (remoteIp) {
let key = `pc:limiter:${remoteIp}`;
return cache.delAsync(key)
});
module.exports = {
... ...
... ... @@ -4,6 +4,7 @@
const cache = global.yoho.cache.master;
const _ = require('lodash');
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
let ONE_DAY = 60 * 60 * 24;
... ... @@ -52,12 +53,32 @@ module.exports = (req, res, next) => {
}
});
function 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();
}
if (req.xhr) {
return res.json({
code: 400,
data: {refer: limitAPI}
});
}
return res.redirect(limitPage);
}
cache.getAsync(key).then(result => {
if (result && _.isNumber(result)) {
if (result > 1800) { // 判断 qps
cache.touch(key, ONE_DAY);
console.log('req limit', key);
res.status(403).end();
return limiterPage();
} else {
cache.incrAsync(key, 1); // qps + 1
return next();
... ...