robot-check-service.js
2.52 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
'use strict';
const url = require('url');
const cache = global.yoho.cache.master;
const Promise = require('bluebird');
const config = global.yoho.config;
const logger = global.yoho.logger;
const _ = require('lodash');
const humanExpire = 3600;
const PAGE = 'PC';
const limitKey = 'limit2';
/**
* note: 这里要注意,新的计数是指APM处理的情况
*/
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
removeBlack(remoteIp, apiLimitValidate, referer) {
let operations = [];
if (referer) {
let pid = _.get(url.parse(referer, true), 'query.pid');
pid && operations.push(cache.delAsync(`${pid}:${remoteIp}`));
}
const isOpenApmrisk = _.get(this.ctx.req.app.locals, 'pc.open.apmrisk', false);
// 新的计数
if (isOpenApmrisk) {
operations.push(cache.delAsync(`${config.app}:${limitKey}:${remoteIp}`));
// 验证码之后一小时之内不再限制qps
if (apiLimitValidate) {
operations.push(cache.setAsync(`${config.app}:limiter:api:ishuman:${remoteIp}`, 1, humanExpire));
} else {
operations.push(cache.setAsync(`${config.app}:${limitKey}:ishuman:${remoteIp}`, 1, humanExpire));
}
_.forEach(config.REQUEST_LIMIT, (val, key) => {
operations.push(cache.delAsync(`${config.app}:${limitKey}:${key}:max:${remoteIp}`));
});
} else {
operations.push(cache.delAsync(`${config.app}:limiter:${remoteIp}`));
// 验证码之后一小时之内不再限制qps
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}`));
});
}
return Promise.all(operations);
}
verifyImgCheckRisk(udid, degrees) {
return this.get({
data: {
method: 'app.graphic.verify',
udid: udid,
fromPage: PAGE,
degrees: degrees
}
}).then(result => {
logger.info(`app.graphic.verify result: ${JSON.stringify(result)}`);
return result;
});
}
};