Authored by 陈峰

Merge branch 'release/11.11' into 'release/6.7.9'

限流全局提示



See merge request !384
@@ -117,8 +117,7 @@ module.exports = { @@ -117,8 +117,7 @@ module.exports = {
117 port: '4444' // influxdb port 117 port: '4444' // influxdb port
118 }, 118 },
119 console: { 119 console: {
120 - close: true,  
121 - level: 'info', 120 + level: 'debug',
122 colorize: 'all', 121 colorize: 'all',
123 prettyPrint: true 122 prettyPrint: true
124 } 123 }
@@ -258,7 +257,46 @@ if (isProduction) { @@ -258,7 +257,46 @@ if (isProduction) {
258 host: '10.66.4.25', 257 host: '10.66.4.25',
259 port: 8086, 258 port: 8086,
260 db: 'web_monitor', 259 db: 'web_monitor',
  260 + },
  261 + loggers: {
  262 + infoFile: {
  263 + name: 'info',
  264 + level: 'info',
  265 + filename: '/Data/logs/yohobuy-node/info/info.log',
  266 + maxFiles: 1,
  267 + tailable: true,
  268 + maxsize: Math.pow(1024, 3),
  269 + zippedArchive: true,
  270 + timestamp() {
  271 + return new Date().toString();
261 } 272 }
  273 + },
  274 + errorFile: {
  275 + name: 'error',
  276 + level: 'error',
  277 + filename: '/Data/logs/yohobuy-node/error/error.log',
  278 + maxFiles: 1,
  279 + tailable: true,
  280 + maxsize: Math.pow(1024, 3),
  281 + zippedArchive: true,
  282 + handleExceptions: true,
  283 + timestamp() {
  284 + return new Date().toString();
  285 + }
  286 + },
  287 + udp: { // send by udp
  288 + measurement: 'yohobuy_node_log',
  289 + level: 'debug', // logger level
  290 + host: 'influxdblog.web.yohoops.org', // influxdb host
  291 + port: '4444' // influxdb port
  292 + },
  293 + console: {
  294 + close: true,
  295 + level: 'info',
  296 + colorize: 'all',
  297 + prettyPrint: true
  298 + }
  299 + },
262 }); 300 });
263 } else if (isTest) { 301 } else if (isTest) {
264 Object.assign(module.exports, { 302 Object.assign(module.exports, {
@@ -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 };
@@ -57,7 +57,7 @@ @@ -57,7 +57,7 @@
57 "urlencode": "^1.1.0", 57 "urlencode": "^1.1.0",
58 "uuid": "^2.0.2", 58 "uuid": "^2.0.2",
59 "yoho-express-session": "^2.0.0", 59 "yoho-express-session": "^2.0.0",
60 - "yoho-node-lib": "=0.6.33", 60 + "yoho-node-lib": "=0.6.34",
61 "yoho-zookeeper": "^1.0.10", 61 "yoho-zookeeper": "^1.0.10",
62 "yo-cli": "=1.0.7" 62 "yo-cli": "=1.0.7"
63 }, 63 },