Authored by 陈峰

commit

... ... @@ -5,6 +5,7 @@ const Promise = require('bluebird');
const co = Promise.coroutine;
const config = global.yoho.config;
const _ = require('lodash');
const humanExpire = 60;
const HeaderModel = require('../../../doraemon/models/header');
... ... @@ -22,7 +23,7 @@ const removeBlack = (remoteIp) => {
operations.push(cache.delAsync(`${config.app}:limiter:${remoteIp}`));
// 验证码之后一小时之内不再限制qps
operations.push(cache.setAsync(`${config.app}:limiter:ishuman:${remoteIp}`, 1, 3600));
operations.push(cache.setAsync(`${config.app}:limiter:ishuman:${remoteIp}`, 1, humanExpire));
_.forEach(config.REQUEST_LIMIT, (val, key) => {
operations.push(cache.delAsync(`${config.app}:limiter:${key}:max:${remoteIp}`));
});
... ...
... ... @@ -11,6 +11,7 @@ const config = global.yoho.config;
const hostname = require('os').hostname();
const routeEncode = require('./route-encode');
const _ = require('lodash');
const limiterIpTime = 3600;
const forceNoCache = (res) => {
if (res) {
... ... @@ -50,7 +51,10 @@ exports.notFound = () => {
* @return {[type]}
*/
exports.serverError = () => {
return (err, req, res, next) => {
return async (err, req, res, next) => {
if (req.isCapturedError) {
return;
}
forceNoCache(res);
const uid = req.user ? req.user.uid : 0;
... ... @@ -62,7 +66,7 @@ exports.serverError = () => {
err.code = err.code || err.statusCode || 500;
if (req.isApmReport && !err.apiError && err.code !== 401) {
if (req.isApmReport && !err.apiError) { // apierror在node lib中已经上报过了 不需要再次上报
// 上报服务端错误
sender.addMessage({
measurement: 'error-report',
... ... @@ -96,28 +100,32 @@ exports.serverError = () => {
}
}
if (err.code === 9999991 || err.code === 9999992) {
let limitAPI = helpers.urlFormat('/3party/check', {refer: req.get('Referer') || ''});
let limitPage = helpers.urlFormat('/3party/check', {
refer: req.protocol + '://' + req.get('host') + req.originalUrl
});
let remoteIp = req.yoho.clientIp;
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
const isHuman = await cache.getAsync(`${config.app}:limiter:ishuman:${remoteIp}`);
remoteIp = arr[0];
}
if (!isHuman) {
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
cache.setAsync(`${config.app}:limiter:${remoteIp}`);
remoteIp = arr[0];
}
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
if (req.xhr) {
return res.status(510).json({
code: err.code,
data: {refer: limitAPI}
let limitAPI = helpers.urlFormat('/3party/check', {refer: req.get('Referer') || ''});
let limitPage = helpers.urlFormat('/3party/check', {
refer: req.protocol + '://' + req.get('host') + req.originalUrl
});
}
return res.redirect(limitPage);
if (req.xhr) {
return res.status(510).json({
code: err.code,
data: {refer: limitAPI}
});
}
return res.redirect(limitPage);
}
}
if (!res.headersSent) {
... ...
... ... @@ -36,6 +36,7 @@ module.exports = (limiter, policy) => {
logger.debug(results);
if (results.human) { // 经过验证码之后1小时有效期内不再验证qps
console.log('isHuman');
return Promise.resolve(true);
}
... ...
... ... @@ -57,7 +57,7 @@
"urlencode": "^1.1.0",
"uuid": "^2.0.2",
"yoho-express-session": "^2.0.0",
"yoho-node-lib": "=0.6.1",
"yoho-node-lib": "^0.6.3",
"yoho-zookeeper": "^1.0.8"
},
"devDependencies": {
... ...