...
|
...
|
@@ -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,35 +161,16 @@ 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;
|
|
|
req.session.apiRiskValidate = true;
|
|
|
|
|
|
if (req.xhr) {
|
|
|
return res.status(510).json({
|
...
|
...
|
@@ -149,36 +180,15 @@ exports.serverError = () => { |
|
|
}
|
|
|
|
|
|
return res.redirect(checkRefer.replace(replaceKey, req.protocol + '://' + req.get('host') + req.originalUrl));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
errorCode = 510;
|
|
|
} else if (err.code === 9999991 || err.code === 9999992) {
|
|
|
return _err510(req, res, 510, err);
|
|
|
}
|
|
|
|
|
|
if (!res.headersSent) {
|
|
|
if (req.xhr) {
|
|
|
return res.status(errorCode).json({
|
|
|
code: errorCode,
|
|
|
message: err.message || `服务器${errorCode === 510 ? '繁忙' : '错误'}!`
|
|
|
});
|
|
|
return _err500(req, res, errorCode, err);
|
|
|
}
|
|
|
|
|
|
const renderErrPage = (result) => {
|
|
|
result = result || {};
|
|
|
|
|
|
res.status(errorCode).render(`error/${errorCode}`, {
|
|
|
module: 'common',
|
|
|
page: 'error',
|
|
|
err: err,
|
|
|
title: `服务器${errorCode === 510 ? '繁忙' : '错误'} | Yoho!Buy有货 | 潮流购物逛不停`,
|
|
|
headerData: result.headerData
|
|
|
});
|
|
|
};
|
|
|
|
|
|
return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
|
|
|
renderErrPage();
|
|
|
});
|
|
|
}
|
|
|
next(err);
|
|
|
return next(err);
|
|
|
};
|
|
|
}; |
...
|
...
|
|