forked from Map987/github-proxy_by_cloudflare_pages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_worker.js
More file actions
118 lines (113 loc) · 4.09 KB
/
Copy path_worker.js
File metadata and controls
118 lines (113 loc) · 4.09 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
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
const hostnames = [
"raw.githubusercontent.com",
"objects.githubusercontent.com",
"alive.github.com",
"api.github.com",
"assets-cdn.github.com",
"avatars.githubusercontent.com",
"avatars0.githubusercontent.com",
"avatars1.githubusercontent.com",
"avatars2.githubusercontent.com",
"avatars3.githubusercontent.com",
"avatars4.githubusercontent.com",
"avatars5.githubusercontent.com",
"camo.githubusercontent.com",
"central.github.com",
"cloud.githubusercontent.com",
"codeload.github.com",
"collector.github.com",
"desktop.githubusercontent.com",
"favicons.githubusercontent.com",
"gist.github.com",
"github-cloud.s3.amazonaws.com",
"github.zerozr99.workers.dev.s3.amazonaws.com",
"github-production-release-asset-2e65be.s3.amazonaws.com",
"github-production-repository-file-5c1aeb.s3.amazonaws.com",
"github-production-user-asset-6210df.s3.amazonaws.com",
"github.blog",
"github.community",
"github.githubassets.com",
"github.global.ssl.fastly.net",
"github.io",
"github.map.fastly.net",
"githubstatus.com",
"live.github.com",
"media.githubusercontent.com",
"objects.githubusercontent.com",
"pipelines.actions.githubusercontent.com",
"raw.githubusercontent.com",
"user-images.githubusercontent.com",
"education.github.com",
];
function updateUrlForHost(url, hostnames) {
for (const hostname of hostnames) {
if (url.pathname.includes(hostname)) {
const prefixes = [
`/https://${hostname}/`,
`/http://${hostname}/`,
`/${hostname}/`,
"/github.com/",
];
for (const prefix of prefixes) {
if (url.pathname.startsWith(prefix)) {
url.pathname = url.pathname.substring(prefix.length - 1);
break;
}
}
url.hostname = hostname;
break;
}
}
}
export default {
async fetch(request, env) {
let url = new URL(request.url);
if (hostnames.some((hostname) => url.pathname.includes(hostname))) {
updateUrlForHost(url, hostnames);
} else {
url.hostname = "github.com";
const hostname_prefixes = [
"/https://github.com/",
"/http://github.com/",
"/github.com/",
];
for (const hostname_prefix of hostname_prefixes) {
if (url.pathname.startsWith(hostname_prefix)) {
url.pathname = url.pathname.substring(
hostname_prefix.length - 1
);
break;
}
}
if (url.pathname.includes("releases/download")) {
let headers = new Headers(request.headers);
// Create a new request for the HEAD method
let headRequest = new Request(url, {
method: "HEAD",
headers: headers,
redirect: "manual",
});
let response = await fetch(headRequest);
if (response.status >= 300 && response.status < 400) {
let location = response.headers.get("Location");
if (location) {
url = new URL(location);
}
}
} else if (url.pathname.startsWith("/login")) {
url.pathname = "";
return; // 禁止访问login
} else if (url.pathname.startsWith("/signup")) {
url.pathname = "";
return; // 禁止访问signup
} else if (url.pathname == "/") {
url.pathname = "";
return; // 禁止直接访问
} else if (url.pathname == "") {
url.pathname = "";
return; // 禁止直接访问
}
}
return fetch(new Request(url, request));
},
};