whitelist.ts
1.59 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
import Trace from "../monitoring/traces";
/**
* 检查URL是否在白名单中
* @param {string} url 当前请求的URL
* @param {string} [customAddress] 自定义白名单地址
* @returns {Promise<boolean>} 是否在白名单中
*/
export async function checkWhitelist(url: string, customAddress?: string): Promise<boolean> {
// 如果没有URL参数,返回false
if (!url) {
return false;
}
try {
// 如果提供了自定义地址,更新TraceManager配置
if (customAddress) {
Trace.updateConfig({ url: customAddress });
}
// 获取白名单URL列表
const whitelistUrls = await Trace.getTraceUrls();
// 检查当前请求的URL是否在白名单中
return whitelistUrls.includes(url);
} catch (error) {
console.error('\x1b[31m❌ %s\x1b[0m', '检查白名单URL失败:', error);
return false;
}
}
/**
* 预加载白名单URL列表
* @param {string} [customAddress] 自定义白名单地址
* @returns {Promise<void>}
*/
export async function preloadWhitelist(customAddress?: string): Promise<void> {
try {
// 如果提供了自定义地址,更新TraceManager配置
if (customAddress) {
Trace.updateConfig({ url: customAddress });
}
await Trace.preloadTraceUrls();
console.log('\x1b[32m✅ %s\x1b[0m', '白名单URL预加载完成');
} catch (error) {
console.warn('\x1b[33m⚠️ %s\x1b[0m', '白名单URL预加载失败:', error);
}
}
/**
* 获取白名单预加载状态
* @returns {boolean} 白名单是否已预加载
*/
export function isWhitelistPreloaded(): boolean {
return Trace.isPreloaded;
}