check.js
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header');
const checkModel = require('..//models/check');
const decodeURIComponent = require('../../../utils/string-process').decodeURIComponent;
const logger = global.yoho.logger;
const Geetest = require('geetest');
const config = global.yoho.config;
const co = Promise.coroutine;
const cache = global.yoho.cache.master;
const captcha = new Geetest({
geetest_id: 'bce95d796bc3058615fdf2ec2c0aef29',
geetest_key: '124c41d3a22aa48f36ae3cd609c51db5'
});
exports.index = (req, res) => {
req.yoho.captchaShow = false;
if (req.session.apiRiskValidate) {
res.locals.useRiskImg = true;
} else {
res.locals.useGeetest = true;
}
if (_.has(res, 'locals.loadJsBefore')) {
res.locals.loadJsBefore.push({
src: global.yoho.config.geetestJs
});
} else {
res.locals.loadJsBefore = [
{
src: global.yoho.config.geetestJs
}
];
}
res.render('check', {
pageHeader: headerModel.setNav({
navTitle: '友情提醒'
}),
width750: true,
localCss: true
});
};
const submitValidate = {
errRes: {
code: 400,
message: '验证码错误',
captchaShow: true,
changeCaptcha: true
},
clearLimitIp(req) {
let remoteIp = req.yoho.clientIp;
if (remoteIp.indexOf(',') > 0) {
let arr = remoteIp.split(',');
remoteIp = arr[0];
}
// pc:limiter:IP 和PC端共用
let operations = [cache.delAsync(`pc:limiter:${remoteIp}`)];
// 验证码之后一小时之内不再限制qps
if (req.session.apiLimitValidate || req.session.apiRiskValidate) {
operations.push(cache.setAsync(
`${config.app}:limiter:api:ishuman:${remoteIp}`,
1,
config.LIMITER_IP_TIME
));
} else {
operations.push(cache.setAsync(
`${config.app}:limiter:ishuman:${remoteIp}`,
1,
config.LIMITER_IP_TIME
));
}
delete req.session.apiLimitValidate;
delete req.session.apiRiskValidate;
if (req.body.pid) {
let riskPid = decodeURIComponent(req.body.pid) + ':' + _.get(req.yoho, 'clientIp', '');
operations.push(cache.delAsync(riskPid));
}
_.forEach(config.REQUEST_LIMIT, (val, key) => {
operations.push(cache.delAsync(`${config.app}:limiter:${key}:max:${remoteIp}`));
});
return Promise.all(operations);
},
geetest(req, res) {
const self = this;
co(function * () {
let challenge = req.body.geetest_challenge,
validate = req.body.geetest_validate,
seccode = req.body.geetest_seccode;
if (!challenge || !validate || !seccode) {
return res.json(self.errRes);
}
let geetestRes = yield captcha.validate({
challenge,
validate,
seccode
});
if (geetestRes) {
logger.info('geetest success');
yield self.clearLimitIp(req);
return res.json({
code: 200
});
} else {
logger.info('geetest faild');
return res.json(self.errRes);
}
})();
},
imgCheckRisk(req, res) {
const self = this;
co(function * () {
let result = yield req.ctx(checkModel).verifyImgCheckRisk(req.cookies.udid, req.body.captcha);
if (result.code === 200) {
yield self.clearLimitIp(req);
return res.json(result);
} else {
logger.info('api risk img verify faild');
return res.json(self.errRes);
}
})();
}
};
exports.submit = (req, res) => {
let validateType = 'geetest';
if (req.session.apiRiskValidate && req.body.apiRiskValidate) {
validateType = 'imgCheckRisk';
}
try {
return submitValidate[validateType](req, res);
} catch (err) {
return res.json({
code: 400
});
}
};