-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudflare.js
176 lines (162 loc) · 5.37 KB
/
cloudflare.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 这里编写你的代理规则
const proxyMyJS = [
{
type: "GET",
url: "https://httpbin.org",
urlReplace: ["/1", "/get"],
contextReplace: [{ search: "args", replace: "genshin" }],
contextType: "application/javascript",
paramReplace: [
{ search: "mio_key", replace: "sentry_key" },
],
customAllowHeaders: ["X-Powered-By"]
},
{
type: "POST",
url: "https://httpbin.org",
urlReplace: ["/2", "/post"],
contextType: "application/json",
paramReplace: [
{ search: "mio_key", replace: "sentry_key" },
],
}
]
// 开启Get缓存
const enableCache = true;
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Max-Age': '86400',
'Access-Control-Allow-Headers': 'Content-Type'
};
addEventListener('fetch', event => {
event.passThroughOnException();
event.respondWith(handleRequest(event));
})
// 处理下path
const handlePath = (path) => {
if (path.substring(path.length - 1, path.length) === '/') {
return path.substring(0, path.length - 1)
}
return path;
}
const handleRequest = async (event) => {
const { method, url } = event.request
const pathname = handlePath(new URL(url).pathname)
// 循环判断该执行那个
for (let i = 0; i < proxyMyJS.length; i++) {
if (proxyMyJS[i].urlReplace[0].test(pathname)) {
if (method === "OPTIONS") {
return handleResponse(new Response(null, {}), null, proxyMyJS[i])
}
if (proxyMyJS[i].type === method) {
switch (method) {
case "GET":
return getScript(event, proxyMyJS[i], pathname)
case "POST":
return postData(event, proxyMyJS[i], pathname)
}
}
}
}
// 对于预检请求一律返回200
if (method === "OPTIONS") {
return new Response(null, {
status: 200, headers: {
...corsHeaders
}
})
}
return new Response(null, { status: 404 })
}
const getScript = async (event, item, pathname) => {
// 缓存功能
let response = await caches.default.match(event.request);
if (!response && enableCache) {
// get 原始地址
let params = new URL(event.request.url).search;
// 过滤params
params = handleParamReplace(item, params)
if (params && params !== '') {
// 带params的,不缓存
response = await fetch(item.url + handleUrlReplace(pathname, item.urlReplace) + params);
} else {
response = await fetch(item.url + handleUrlReplace(pathname, item.urlReplace));
}
let js = await response.text();
// 过滤body结果
js = handleContextReplace(item, js);
//
response = handleResponse(response, js, item)
// 不带参数的get就缓存
if (!params || params === '') {
event.waitUntil(caches.default.put(event.request, response.clone()));
}
}
return response;
}
const postData = async (event, item, pathname) => {
const request = new Request(event.request);
request.headers.delete('cookie');
let params = new URL(event.request.url).search;
// 过滤params
params = handleParamReplace(item, params)
if (params && params !== '') {
// 带params的,不缓存
response = await fetch(item.url + handleUrlReplace(pathname, item.urlReplace) + params, request);
} else {
response = await fetch(item.url + handleUrlReplace(pathname, item.urlReplace), request);
}
let js = await response.text();
// 过滤body结果
js = handleContextReplace(item, js);
response = handleResponse(response, js, item)
return response;
}
const handleContextReplace = (item, context) => {
let newContext = context;
// 存在则替换
if (item?.contextReplace) {
for (let h = 0; h < item.contextReplace.length; h++) {
newContext = newContext.replace(item.contextReplace[h].search, item.contextReplace[h].replace);
}
}
return newContext
}
// 处理
const handleParamReplace = (item, params) => {
let newParams = params
if (item?.paramReplace && newParams && newParams !== '') {
for (let h = 0; h < item.paramReplace.length; h++) {
newParams = newParams.replace(item.paramReplace[h].search, item.paramReplace[h].replace);
}
}
return newParams;
}
const handleResponse = (response, body, item) => {
// 生成Allow-Headers头
let allowHeaders = []
response.headers.forEach((...list) => {
allowHeaders.push(list[1])
})
if (item?.customAllowHeaders && item.customAllowHeaders.length > 0) {
allowHeaders = [...allowHeaders, ...item.customAllowHeaders]
}
// 替换结果
let resultHeaders = {
...response.headers,
...corsHeaders,
}
resultHeaders['Access-Control-Allow-Headers'] += ',' + allowHeaders.join(',')
if (item?.contextType) {
resultHeaders["content-type"] = item.contextType;
}
response = new Response(body, {
headers: resultHeaders
})
return response
}
// 正则处理url替换
const handleUrlReplace = (text, list) => {
return text.replace(list[0], list[1])
}