-
Notifications
You must be signed in to change notification settings - Fork 1
/
_worker.js
38 lines (33 loc) · 1.14 KB
/
_worker.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
export default {
async fetch(request) {
const url = new URL(request.url);
const remoteParam = url.searchParams.get("remote");
// 检查 `remote` 参数是否存在
if (!remoteParam) {
return new Response("Empty remote", { status: 400 });
}
try {
// Base64 解码 `remote` 参数
const remoteUrl = atob(remoteParam);
// 验证解码后的结果是否是有效 URL
const targetUrl = new URL(remoteUrl);
// 代理请求到目标 URL
const response = await fetch(targetUrl.toString(), {
method: request.method,
headers: request.headers,
body: request.body,
});
// 返回目标 URL 的响应
const headers = new Headers(response.headers);
headers.set("Access-Control-Allow-Origin", "*");
headers.set("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS");
headers.set("Access-Control-Allow-Headers", "Content-Type");
return new Response(response.body, {
status: response.status,
headers,
});
} catch (error) {
return new Response(`Invalid remote URL: ${error.message}`, { status: 400 });
}
},
};