whitelist-path.js
2.5 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
const _ = require('lodash');
const logger = global.yoho.logger;
const cache = global.yoho.cache.master;
const get_WHITE_LIST_KEY = (t) => `${t}:limiter:whitelist:path`;
const DEFAULT_PATH_WHITE_LIST = {
pc: [
'/3party/check',
'/passport/images-risk.png',
'/passport/images.png',
'/passport/cert/headerTip',
'/common/getbanner',
'/common/suggestfeedback',
'/product/search/history',
'/product/search/suggest'
],
wap: [
'/3party/check',
'/3party/check/submit',
'/passport/captcha/get',
'/passport/img-check.jpg',
'/passport/img-check-risk.jpg',
'/passport/geetest/register',
'/activity/individuation',
'/activity/individuation/coupon',
'/activity/share',
'/activity/wechat/share',
'/activity/wechat/1111',
'/api/switch',
'/passport/login/user',
'/sw.js',
'/manifest.json',
'/activity/sw.js',
'/activity/manifest.json',
'/hfxRaNY27L.txt',
'/activity/hfxRaNY27L.txt',
'/product/shop/hfxRaNY27L.txt',
'/product/hfxRaNY27L.txt',
'/.well-known/apple-app-site-association',
'/service/sitemap.xml',
'/node/status.html',
'/product/seckill',
'/product/seckill/list',
'/product/seckill/remind',
'/product/seckill/get-product-list'
]
};
const cacheWhiteList = {
nowTime() {
return Date.parse(new Date()) / 1000;
},
async getValue(app) {
if (this.updateTime || this.nowTime() - this.updateTime > 60 * 10) {
await this.syncRemoteConfig(app);
}
return _.uniq(_.concat([], DEFAULT_PATH_WHITE_LIST[app], _.toArray(this.whiteList)));
},
async syncRemoteConfig(app) {
if (this.syncing) {
return;
}
this.syncing = true;
await cache.getAsync(get_WHITE_LIST_KEY(app)).then(res => {
this.updateTime = this.nowTime();
if (!res) {
return;
}
this.whiteList = JSON.parse(res);
this.syncing = false;
}).catch((e) => {
this.syncing = false;
logger.debug('whitelist path parse error. ' + JSON.stringfy(e));
});
}
};
module.exports = async({user}, next) => {
const paths = await cacheWhiteList.getValue(user.app);
logger.debug('whitelist-path==>', user.path);
if (paths.includes(user.path)) {
return;
}
return next();
};