Authored by 徐炜

爬虫限制重构规则策略

... ... @@ -45,21 +45,30 @@ module.exports = (req, res, next) => {
limiter(qpsLimiter, captchaPolicy, context),
limiter(fakerLimiter, reporterPolicy, context)
]).then((results) => {
let allPass = true, exclusion = false;
let allPass = true, exclusion = false, policy = null;
_.forEach(results, (result) => {
if (typeof result === 'object') {
exclusion = result.exclusion || false;
if (typeof result === 'object' && !exclusion) {
exclusion = result.exclusion;
}
if (!excluded && !result) {
if (!excluded && typeof result === 'function') {
allPass = false;
}
if (typeof result === 'function') {
policy = result;
}
});
if (allPass || exclusion) {
if (exclusion) {
return next();
} else if (!allPass && policy) {
policy(req, res, next);
} else {
return next();
}
}).catch((err) => {
logger.error(err);
return next();
... ...
... ... @@ -18,8 +18,6 @@ module.exports = (limiter, policy) => {
});
return cache.getAsync(key).then((result) => {
logger.info('faker like:', result);
if (result) {
if (result > 100) {
return policy(req, res, next);
... ...
... ... @@ -53,7 +53,7 @@ module.exports = (limiter, policy) => {
cache.touch(key, ONE_DAY);
logger.info('req limit', key);
return policy(req, res, next);
return Promise.resolve(policy);
} else {
cache.incrAsync(key, 1); // qps + 1
return Promise.resolve(true);
... ...
... ... @@ -43,7 +43,7 @@ module.exports = (limiter, policy) => {
};
if (test(blacklist)) {
return policy(req, res, next);//Promise.resolve(false);
return Promise.resolve(policy);
} else if (test(whitelist)) {
return Promise.resolve({
exclusion: true
... ...