Authored by 周少峰

Merge branch 'feature/anti-spider' into feature/captch-change

... ... @@ -20,14 +20,17 @@ const limiter = (rule, policy, context) => {
module.exports = (req, res, next) => {
let remoteIp = req.get('X-Forwarded-For') || req.connection.remoteAddress;
logger.debug('request remote ip: ', remoteIp);
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = arr[0];
remoteIp = arr[arr.length - 1];
}
remoteIp = _.trim(remoteIp);
logger.info('request remote ip: ', remoteIp);
const excluded = _.includes(IP_WHITE_LIST, remoteIp);
const enabled = !_.get(req.app.locals, 'pc.sys.noLimiter');
... ...
... ... @@ -6,6 +6,7 @@ const _ = require('lodash');
const WHITE_LIST = [
'/3party/check',
'/passport/imagesNode',
'/passport/images.png',
'/passport/cert/headerTip'
];
... ... @@ -18,6 +19,10 @@ module.exports = (req, res, next) => {
return next();
}
if (res.statusCode == 403) {
return res.end();
}
if (req.xhr) {
return res.json({
code: 400,
... ...
... ... @@ -29,7 +29,7 @@ module.exports = (limiter, policy) => {
const key = `pc:limiter:${limiter.remoteIp}`;
res.on('render', function() {
res.on('render', function () {
let route = req.route ? req.route.path : '';
let appPath = req.app.mountpath;
... ... @@ -56,7 +56,10 @@ module.exports = (limiter, policy) => {
return Promise.resolve(true);
}
if (result > MAX_QPS) { // 判断 qps
if (result === 9999) {
res.statusCode = 403;
return Promise.resolve(policy);
} else if (result > MAX_QPS) { // 判断 qps
cache.touch(key, ONE_DAY);
logger.debug('req limit', key);
... ... @@ -67,7 +70,7 @@ module.exports = (limiter, policy) => {
}
} else {
cache.setAsync(key, 1, 60); // 设置key,1m失效
cache.setAsync(key, 1, 60); // 设置key,1m失效
return Promise.resolve(true);
}
});
... ...