-
Notifications
You must be signed in to change notification settings - Fork 13
/
屑186的代码.js
84 lines (76 loc) · 2.79 KB
/
屑186的代码.js
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
let config = {
basic: {
upstream: ["https://<Your URL1>/"],//上游地址 可填写多个
},
optimization: {
cacheTtl: 1000, //该项决定边缘缓存时间 可调大
},
om: {
return: ["CN"],//如果需要重定向到世纪互联盘,请修改内容为世纪互联盘盘符Tag,不需要此功能请留空
return_upstream: ["?admin", "?setup"],//不用动
bypass: ["nocache"],//此处填写需要略过缓存的文件夹&&文件名
}
};
addEventListener("fetch", (event) => {
event.respondWith(fetchAndApply(event.request));
});
async function fetchAndApply(request) {
let requestURL = new URL(request.url);
let upstreamURL = new URL(config.basic.upstream[randomNum(0, config.basic.upstream.length-1)]);
requestURL.protocol = upstreamURL.protocol;
requestURL.host = upstreamURL.host;
requestURL.pathname = upstreamURL.pathname + requestURL.pathname;
for (let i in config.om.return) {
if (requestURL.pathname.search(config.om.bypass[i]) != -1) {
config.optimization.cacheEverything = false;
} else {
config.optimization.cacheEverything = true;
}
}
for (let i in config.om.return) {
if (requestURL.pathname.search(config.om.return[i]) != -1) {
upstreamURL.pathname = requestURL.pathname;
return new Response(null, { status: 302, headers: { Location: upstreamURL } });
}
}
for (let i in config.om.return_upstream) {
if (requestURL.search === config.om.return_upstream[i]) {
upstreamURL.pathname = requestURL.pathname;
return new Response(null, { status: 302, headers: { Location: upstreamURL } });
}
}
let fetchedResponse = await fetch(
new Request(requestURL, {
cf: {
cacheEverything: config.optimization.cacheEverything,
cacheTtl: config.optimization.cacheTtl,
mirage: true,
polish: "on",
minify: {
javascript: true,
css: true,
html: true,
}
},
method: request.method,
headers: request.headers,
body: request.body,
})
);
let modifiedResponseHeaders = new Headers(fetchedResponse.headers);
return new Response(fetchedResponse.body, {
headers: modifiedResponseHeaders,
status: fetchedResponse.status,
statusText: fetchedResponse.statusText,
});
}
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
default:
return 0;
}
}