Authored by 陈峰

限流全局提示

... ... @@ -117,8 +117,7 @@ module.exports = {
port: '4444' // influxdb port
},
console: {
close: true,
level: 'info',
level: 'debug',
colorize: 'all',
prettyPrint: true
}
... ... @@ -258,7 +257,46 @@ if (isProduction) {
host: '10.66.4.25',
port: 8086,
db: 'web_monitor',
}
},
loggers: {
infoFile: {
name: 'info',
level: 'info',
filename: '/Data/logs/yohobuy-node/info/info.log',
maxFiles: 1,
tailable: true,
maxsize: Math.pow(1024, 3),
zippedArchive: true,
timestamp() {
return new Date().toString();
}
},
errorFile: {
name: 'error',
level: 'error',
filename: '/Data/logs/yohobuy-node/error/error.log',
maxFiles: 1,
tailable: true,
maxsize: Math.pow(1024, 3),
zippedArchive: true,
handleExceptions: true,
timestamp() {
return new Date().toString();
}
},
udp: { // send by udp
measurement: 'yohobuy_node_log',
level: 'debug', // logger level
host: 'influxdblog.web.yohoops.org', // influxdb host
port: '4444' // influxdb port
},
console: {
close: true,
level: 'info',
colorize: 'all',
prettyPrint: true
}
},
});
} else if (isTest) {
Object.assign(module.exports, {
... ...
... ... @@ -10,8 +10,6 @@ const sender = global.yoho.apmSender;
const config = global.yoho.config;
const hostname = require('os').hostname();
const routeEncode = require('./route-encode');
const pathWhiteList = require('./limiter/rules/path-white-list');
const ipWhiteList = require('./limiter/rules/ip-white-list');
const _ = require('lodash');
const limiterIpTime = 3600;
... ... @@ -29,6 +27,60 @@ const forceNoCache = (res) => {
}
};
const _err510 = (req, res, code, err) => {
res.status(code);
if (req.xhr) {
return res.json({
code: 510,
message: err.message || '服务器繁忙请稍后重试!'
});
}
const renderErrPage = (result) => {
result = result || {};
res.render('error/510', {
module: 'common',
page: 'error',
err: err,
title: '服务器繁忙 | Yoho!Buy有货 | 潮流购物逛不停',
headerData: result.headerData
});
};
return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
renderErrPage();
});
};
const _err500 = (req, res, code, err) => {
res.status(code);
if (req.xhr) {
return res.json({
code: 500,
message: err.message || '服务器错误!'
});
}
const renderErrPage = (result) => {
result = result || {};
res.render('error/500', {
module: 'common',
page: 'error',
err: err,
title: '服务器错误 | Yoho!Buy有货 | 潮流购物逛不停',
headerData: result.headerData
});
};
return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
renderErrPage();
});
};
exports.notFound = () => {
return (req, res, next) => {
forceNoCache(res);
... ... @@ -66,9 +118,7 @@ exports.serverError = () => {
const udid = _.get(req, 'cookies.udid', 'yoho');
let errorCode = 500;
err = err || {
code: 500
};
err = err || {};
err.code = parseInt(err.code || err.statusCode, 10) || 500;
... ... @@ -111,74 +161,34 @@ exports.serverError = () => {
} else {
return res.redirect(helpers.urlFormat('/signin.html', {refer: req.fullUrl()}));
}
} else if (err.apiRisk || err.code === 9999991 || err.code === 9999992) {
} else if (err.apiRisk) {
let remoteIp = req.yoho.clientIp;
if (err.apiRisk || (!_.includes(pathWhiteList(), req.path) && !(await ipWhiteList(remoteIp)))) {
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = arr[0];
}
let sessionLimitKey;
let isHuman;
if (err.apiRisk) {
sessionLimitKey = 'apiRiskValidate';
} else {
sessionLimitKey = 'apiLimitValidate';
isHuman = await cache.getAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`);
}
if (!isHuman) {
if (_.get(req.app.locals, 'pc.open.apmrisk', false)) {
cache.setAsync(`${config.app}:limit2:${remoteIp}`, 1, limiterIpTime);
} else {
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
}
req.session[sessionLimitKey] = true;
if (req.xhr) {
return res.status(510).json({
code: err.code,
data: {refer: checkRefer.replace(replaceKey, req.get('Referer') || '')}
});
}
return res.redirect(checkRefer.replace(replaceKey, req.protocol + '://' + req.get('host') + req.originalUrl));
}
if (_.get(req.app.locals, 'pc.open.apmrisk', false)) {
cache.setAsync(`${config.app}:limit2:${remoteIp}`, 1, limiterIpTime);
} else {
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
}
errorCode = 510;
}
req.session.apiRiskValidate = true;
if (!res.headersSent) {
if (req.xhr) {
return res.status(errorCode).json({
code: errorCode,
message: err.message || `服务器${errorCode === 510 ? '繁忙' : '错误'}!`
return res.status(510).json({
code: err.code,
data: {refer: checkRefer.replace(replaceKey, req.get('Referer') || '')}
});
}
const renderErrPage = (result) => {
result = result || {};
return res.redirect(checkRefer.replace(replaceKey, req.protocol + '://' + req.get('host') + req.originalUrl));
res.status(errorCode).render(`error/${errorCode}`, {
module: 'common',
page: 'error',
err: err,
title: `服务器${errorCode === 510 ? '繁忙' : '错误'} | Yoho!Buy有货 | 潮流购物逛不停`,
headerData: result.headerData
});
};
} else if (err.code === 9999991 || err.code === 9999992) {
return _err510(req, res, 510, err);
}
return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
renderErrPage();
});
if (!res.headersSent) {
return _err500(req, res, errorCode, err);
}
next(err);
return next(err);
};
};
... ...
... ... @@ -57,7 +57,7 @@
"urlencode": "^1.1.0",
"uuid": "^2.0.2",
"yoho-express-session": "^2.0.0",
"yoho-node-lib": "=0.6.33",
"yoho-node-lib": "=0.6.34",
"yoho-zookeeper": "^1.0.10",
"yo-cli": "=1.0.7"
},
... ...