...
|
...
|
@@ -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();
|
...
|
...
|
|