Authored by 陈峰

api limit

... ... @@ -26,9 +26,12 @@ const isHuman = (req, res) => {
remoteIp = arr[0];
}
const apiLimitValidate = req.session.apiLimitValidate;
delete req.session.apiLimitValidate;
logger.info('isHuman', remoteIp);
robotCheckService.removeBlack(remoteIp).then(() => {
return robotCheckService.removeBlack(remoteIp, apiLimitValidate).then(() => {
return res.json({
code: 200
});
... ...
... ... @@ -5,7 +5,7 @@ const Promise = require('bluebird');
const co = Promise.coroutine;
const config = global.yoho.config;
const _ = require('lodash');
const humanExpire = 60;
const humanExpire = 3600;
const HeaderModel = require('../../../doraemon/models/header');
... ... @@ -17,13 +17,18 @@ const index = co(function* (channel) {
};
});
const removeBlack = (remoteIp) => {
const removeBlack = (remoteIp, apiLimitValidate) => {
let operations = [];
operations.push(cache.delAsync(`${config.app}:limiter:${remoteIp}`));
// 验证码之后一小时之内不再限制qps
operations.push(cache.setAsync(`${config.app}:limiter:ishuman:${remoteIp}`, 1, humanExpire));
if (apiLimitValidate) {
operations.push(cache.setAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`, 1, humanExpire));
} else {
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}`));
});
... ...
... ... @@ -4,6 +4,13 @@
<div class="captcha-wrap"> </div>
<a class="btn confirm">确定</a>
</div>
<div class="download">
<div class="bottom-down">
<div class="qr-code right"></div>
<a href="http://itunes.apple.com/us/app/id490655927?ls=1&amp;mt=8" target="_blank" class="down-app-btn down-btns"></a>
<a href="http://yoho-apps.qiniudn.com/YohoBuy_YOHO.apk" target="_blank" class="down-apk-btn down-btns"></a>
</div>
</div>
</div>
{{> gee-captcha}}
... ...
... ... @@ -59,6 +59,7 @@ exports.serverError = () => {
const uid = req.user ? req.user.uid : 0;
const udid = _.get(req, 'cookies.udid', 'yoho');
let errorCode = 500;
err = err || {
code: 500
... ... @@ -102,7 +103,7 @@ exports.serverError = () => {
if (err.code === 9999991 || err.code === 9999992) {
let remoteIp = req.yoho.clientIp;
const isHuman = await cache.getAsync(`${config.app}:limiter:ishuman:${remoteIp}`);
const isHuman = await cache.getAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`);
if (!isHuman) {
if (remoteIp.indexOf(',') > 0) {
... ... @@ -117,6 +118,7 @@ exports.serverError = () => {
refer: req.protocol + '://' + req.get('host') + req.originalUrl
});
req.session.apiLimitValidate = true;
if (req.xhr) {
return res.status(510).json({
code: err.code,
... ... @@ -126,12 +128,13 @@ exports.serverError = () => {
return res.redirect(limitPage);
}
errorCode = 510;
}
if (!res.headersSent) {
if (req.xhr) {
return res.status(500).json({
code: 500,
return res.status(errorCode).json({
code: errorCode,
message: '服务器错误!'
});
}
... ... @@ -139,7 +142,7 @@ exports.serverError = () => {
const renderErrPage = (result) => {
result = result || {};
res.status(500).render('error/500', {
res.status(errorCode).render(`error/${errorCode}`, {
module: 'common',
page: 'error',
err: err,
... ...
... ... @@ -36,7 +36,6 @@ module.exports = (limiter, policy) => {
logger.debug(results);
if (results.human) { // 经过验证码之后1小时有效期内不再验证qps
console.log('isHuman');
return Promise.resolve(true);
}
... ...
<div class="wrapper screen">
<div class="clear wrapper-404">
<div class="left"><img src="http://static.yohobuy.com/images/v3/index/404.png"></div>
<div class="right right-tips">
<p class="text1">服务器繁忙请重试!</p>
<p class="text2">
</p>
<p class="text3"><a href="http://www.yohobuy.com/product/new" class="button">浏览新品</a>&nbsp;&nbsp;<a href="http://www.yohobuy.com/" class="button">返回首页</a></p>
</div>
</div>
</div>
\ No newline at end of file
... ...
... ... @@ -3,7 +3,7 @@ const $ = require('yoho-jquery');
// 注册ajaxError处理服务端异常
$(document).ajaxError((event, xhr) => {
if (xhr.responseJSON) {
if (xhr.status === 510) {
if (xhr.status === 510 && xhr.responseJSON.data && xhr.responseJSON.data.refer) {
window.location.href = xhr.responseJSON.data.refer;
}
}
... ...
.robot-check-page {
height: 400px;
.captcha {
width: 1150px;
margin-left: auto;
margin-right: auto;
margin-top: 150px;
margin-top: 100px;
}
.download {
margin-bottom: 100px;
}
.title {
... ... @@ -34,4 +36,32 @@
cursor: pointer;
letter-spacing: 10px;
}
.bottom-down {
width: 350px;
margin: 0 auto;
overflow: hidden;
padding-top: 34px;
.qr-code {
width: 130px;
height: 130px;
background: url("/download/down-qr-code.png");
}
.down-btns {
width: 200px;
height: 60px;
display: block;
}
.down-app-btn {
margin-bottom: 8px;
background: url("/download/app-down.png");
}
.down-apk-btn {
background: url("/download/apk-down.png");
}
}
}
... ...