|
@@ -10,8 +10,6 @@ const sender = global.yoho.apmSender; |
|
@@ -10,8 +10,6 @@ const sender = global.yoho.apmSender; |
10
|
const config = global.yoho.config;
|
10
|
const config = global.yoho.config;
|
11
|
const hostname = require('os').hostname();
|
11
|
const hostname = require('os').hostname();
|
12
|
const routeEncode = require('./route-encode');
|
12
|
const routeEncode = require('./route-encode');
|
13
|
-const pathWhiteList = require('./limiter/rules/path-white-list');
|
|
|
14
|
-const ipWhiteList = require('./limiter/rules/ip-white-list');
|
|
|
15
|
const _ = require('lodash');
|
13
|
const _ = require('lodash');
|
16
|
const limiterIpTime = 3600;
|
14
|
const limiterIpTime = 3600;
|
17
|
|
15
|
|
|
@@ -29,6 +27,60 @@ const forceNoCache = (res) => { |
|
@@ -29,6 +27,60 @@ const forceNoCache = (res) => { |
29
|
}
|
27
|
}
|
30
|
};
|
28
|
};
|
31
|
|
29
|
|
|
|
30
|
+const _err510 = (req, res, code, err) => {
|
|
|
31
|
+ res.status(code);
|
|
|
32
|
+
|
|
|
33
|
+ if (req.xhr) {
|
|
|
34
|
+ return res.json({
|
|
|
35
|
+ code: 510,
|
|
|
36
|
+ message: err.message || '服务器繁忙请稍后重试!'
|
|
|
37
|
+ });
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ const renderErrPage = (result) => {
|
|
|
41
|
+ result = result || {};
|
|
|
42
|
+
|
|
|
43
|
+ res.render('error/510', {
|
|
|
44
|
+ module: 'common',
|
|
|
45
|
+ page: 'error',
|
|
|
46
|
+ err: err,
|
|
|
47
|
+ title: '服务器繁忙 | Yoho!Buy有货 | 潮流购物逛不停',
|
|
|
48
|
+ headerData: result.headerData
|
|
|
49
|
+ });
|
|
|
50
|
+ };
|
|
|
51
|
+
|
|
|
52
|
+ return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
|
|
|
53
|
+ renderErrPage();
|
|
|
54
|
+ });
|
|
|
55
|
+};
|
|
|
56
|
+
|
|
|
57
|
+const _err500 = (req, res, code, err) => {
|
|
|
58
|
+ res.status(code);
|
|
|
59
|
+
|
|
|
60
|
+ if (req.xhr) {
|
|
|
61
|
+ return res.json({
|
|
|
62
|
+ code: 500,
|
|
|
63
|
+ message: err.message || '服务器错误!'
|
|
|
64
|
+ });
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ const renderErrPage = (result) => {
|
|
|
68
|
+ result = result || {};
|
|
|
69
|
+
|
|
|
70
|
+ res.render('error/500', {
|
|
|
71
|
+ module: 'common',
|
|
|
72
|
+ page: 'error',
|
|
|
73
|
+ err: err,
|
|
|
74
|
+ title: '服务器错误 | Yoho!Buy有货 | 潮流购物逛不停',
|
|
|
75
|
+ headerData: result.headerData
|
|
|
76
|
+ });
|
|
|
77
|
+ };
|
|
|
78
|
+
|
|
|
79
|
+ return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
|
|
|
80
|
+ renderErrPage();
|
|
|
81
|
+ });
|
|
|
82
|
+};
|
|
|
83
|
+
|
32
|
exports.notFound = () => {
|
84
|
exports.notFound = () => {
|
33
|
return (req, res, next) => {
|
85
|
return (req, res, next) => {
|
34
|
forceNoCache(res);
|
86
|
forceNoCache(res);
|
|
@@ -66,9 +118,7 @@ exports.serverError = () => { |
|
@@ -66,9 +118,7 @@ exports.serverError = () => { |
66
|
const udid = _.get(req, 'cookies.udid', 'yoho');
|
118
|
const udid = _.get(req, 'cookies.udid', 'yoho');
|
67
|
let errorCode = 500;
|
119
|
let errorCode = 500;
|
68
|
|
120
|
|
69
|
- err = err || {
|
|
|
70
|
- code: 500
|
|
|
71
|
- };
|
121
|
+ err = err || {};
|
72
|
|
122
|
|
73
|
err.code = parseInt(err.code || err.statusCode, 10) || 500;
|
123
|
err.code = parseInt(err.code || err.statusCode, 10) || 500;
|
74
|
|
124
|
|
|
@@ -111,35 +161,16 @@ exports.serverError = () => { |
|
@@ -111,35 +161,16 @@ exports.serverError = () => { |
111
|
} else {
|
161
|
} else {
|
112
|
return res.redirect(helpers.urlFormat('/signin.html', {refer: req.fullUrl()}));
|
162
|
return res.redirect(helpers.urlFormat('/signin.html', {refer: req.fullUrl()}));
|
113
|
}
|
163
|
}
|
114
|
- } else if (err.apiRisk || err.code === 9999991 || err.code === 9999992) {
|
164
|
+ } else if (err.apiRisk) {
|
115
|
let remoteIp = req.yoho.clientIp;
|
165
|
let remoteIp = req.yoho.clientIp;
|
116
|
|
166
|
|
117
|
- if (err.apiRisk || (!_.includes(pathWhiteList(), req.path) && !(await ipWhiteList(remoteIp)))) {
|
|
|
118
|
-
|
|
|
119
|
- if (remoteIp.indexOf(',') > 0) {
|
|
|
120
|
- let arr = remoteIp.split(',');
|
|
|
121
|
-
|
|
|
122
|
- remoteIp = arr[0];
|
|
|
123
|
- }
|
|
|
124
|
-
|
|
|
125
|
- let sessionLimitKey;
|
|
|
126
|
- let isHuman;
|
|
|
127
|
-
|
|
|
128
|
- if (err.apiRisk) {
|
|
|
129
|
- sessionLimitKey = 'apiRiskValidate';
|
|
|
130
|
- } else {
|
|
|
131
|
- sessionLimitKey = 'apiLimitValidate';
|
|
|
132
|
- isHuman = await cache.getAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`);
|
|
|
133
|
- }
|
|
|
134
|
-
|
|
|
135
|
- if (!isHuman) {
|
|
|
136
|
if (_.get(req.app.locals, 'pc.open.apmrisk', false)) {
|
167
|
if (_.get(req.app.locals, 'pc.open.apmrisk', false)) {
|
137
|
cache.setAsync(`${config.app}:limit2:${remoteIp}`, 1, limiterIpTime);
|
168
|
cache.setAsync(`${config.app}:limit2:${remoteIp}`, 1, limiterIpTime);
|
138
|
} else {
|
169
|
} else {
|
139
|
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
|
170
|
cache.setAsync(`${config.app}:limiter:${remoteIp}`, 1, limiterIpTime);
|
140
|
}
|
171
|
}
|
141
|
|
172
|
|
142
|
- req.session[sessionLimitKey] = true;
|
173
|
+ req.session.apiRiskValidate = true;
|
143
|
|
174
|
|
144
|
if (req.xhr) {
|
175
|
if (req.xhr) {
|
145
|
return res.status(510).json({
|
176
|
return res.status(510).json({
|
|
@@ -149,36 +180,15 @@ exports.serverError = () => { |
|
@@ -149,36 +180,15 @@ exports.serverError = () => { |
149
|
}
|
180
|
}
|
150
|
|
181
|
|
151
|
return res.redirect(checkRefer.replace(replaceKey, req.protocol + '://' + req.get('host') + req.originalUrl));
|
182
|
return res.redirect(checkRefer.replace(replaceKey, req.protocol + '://' + req.get('host') + req.originalUrl));
|
152
|
- }
|
|
|
153
|
- }
|
|
|
154
|
|
183
|
|
155
|
- errorCode = 510;
|
184
|
+ } else if (err.code === 9999991 || err.code === 9999992) {
|
|
|
185
|
+ return _err510(req, res, 510, err);
|
156
|
}
|
186
|
}
|
157
|
|
187
|
|
158
|
if (!res.headersSent) {
|
188
|
if (!res.headersSent) {
|
159
|
- if (req.xhr) {
|
|
|
160
|
- return res.status(errorCode).json({
|
|
|
161
|
- code: errorCode,
|
|
|
162
|
- message: err.message || `服务器${errorCode === 510 ? '繁忙' : '错误'}!`
|
|
|
163
|
- });
|
189
|
+ return _err500(req, res, errorCode, err);
|
164
|
}
|
190
|
}
|
165
|
|
191
|
|
166
|
- const renderErrPage = (result) => {
|
|
|
167
|
- result = result || {};
|
|
|
168
|
-
|
|
|
169
|
- res.status(errorCode).render(`error/${errorCode}`, {
|
|
|
170
|
- module: 'common',
|
|
|
171
|
- page: 'error',
|
|
|
172
|
- err: err,
|
|
|
173
|
- title: `服务器${errorCode === 510 ? '繁忙' : '错误'} | Yoho!Buy有货 | 潮流购物逛不停`,
|
|
|
174
|
- headerData: result.headerData
|
|
|
175
|
- });
|
|
|
176
|
- };
|
|
|
177
|
-
|
|
|
178
|
- return req.ctx(headerModel).requestHeaderData(req.yoho.channel).then(renderErrPage).catch(() => {
|
|
|
179
|
- renderErrPage();
|
|
|
180
|
- });
|
|
|
181
|
- }
|
|
|
182
|
- next(err);
|
192
|
+ return next(err);
|
183
|
};
|
193
|
};
|
184
|
}; |
194
|
}; |