-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
175 lines (158 loc) · 6.14 KB
/
worker.js
File metadata and controls
175 lines (158 loc) · 6.14 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
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
export default {
async fetch(request) {
const url = new URL(request.url);
// 处理根目录
if (url.pathname === '/') {
try {
const indexResponse = await fetch('https://heplex.github.io/github-proxy/');
const html = await indexResponse.text();
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
});
} catch (error) {
return new Response('Failed to load index page', { status: 500 });
}
}
let targetURL = request.url;
// 处理 releases/latest/download 的特殊情况
if (url.pathname.includes('/releases/latest/download/')) {
const [_, user, repo, ...rest] = url.pathname.split('/');
const fileName = rest[rest.length - 1];
try {
const apiResponse = await fetch(`https://api.github.com/repos/${user}/${repo}/releases/latest`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'GitHub-Proxy'
}
});
if (apiResponse.ok) {
const releaseData = await apiResponse.json();
const downloadUrl = `https://github.com/${user}/${repo}/releases/download/${releaseData.tag_name}/${fileName}`;
// 直接获取下载内容而不是重定向
const downloadResponse = await fetch(downloadUrl, {
headers: {
'User-Agent': 'GitHub-Proxy'
}
});
if (downloadResponse.ok) {
const responseHeaders = new Headers(downloadResponse.headers);
responseHeaders.set('Access-Control-Allow-Origin', '*');
responseHeaders.set('Content-Disposition', `attachment; filename=${fileName}`);
return new Response(downloadResponse.body, {
status: 200,
headers: responseHeaders
});
}
}
} catch (error) {
console.error('Failed to fetch latest release for download:', error);
return new Response('下载失败', { status: 500 });
}
}
// 处理 releases/latest 的特殊情况
if (url.pathname.includes('/releases/latest')) {
const repoPath = url.pathname.split('/releases/latest')[0].slice(1);
try {
const apiResponse = await fetch(`https://api.github.com/repos/${repoPath}/releases/latest`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'GitHub-Proxy'
}
});
if (apiResponse.ok) {
const releaseData = await apiResponse.json();
return Response.redirect(`https://${url.host}/${repoPath}/releases/tag/${releaseData.tag_name}`, 302);
}
} catch (error) {
console.error('Failed to fetch latest release:', error);
}
}
// 处理 API 请求
if (url.pathname.startsWith('/api/')) {
const apiPath = url.pathname.replace('/api/', '');
targetURL = `https://api.github.com/${apiPath}${url.search}`;
} else {
// GitHub 特殊链接处理规则
const patterns = {
'/raw/': (parts) => {
const pathParts = url.pathname.split('/');
const user = pathParts[1];
const repo = pathParts[2];
const branch = pathParts[4];
const filePath = pathParts.slice(5).join('/');
return `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filePath}`;
},
'/info/': (parts) => {
return `https://github.com${url.pathname}${url.search}`;
},
'/git-upload-pack': (parts) => {
return `https://github.com${url.pathname}${url.search}`;
},
'/releases/download/': (parts) => {
const [_, user, repo, , , tag, ...filePath] = parts;
return `https://github.com/${user}/${repo}/releases/download/${tag}/${filePath.join('/')}`;
},
'/media/': (parts) => {
return `https://media.githubusercontent.com${url.pathname}`;
},
'/gist/': (parts) => {
return `https://gist.githubusercontent.com${url.pathname.replace('/gist/', '/')}`;
},
'/archive/': (parts) => {
return `https://codeload.github.com${url.pathname}`;
},
'/avatars/': (parts) => {
return `https://avatars.githubusercontent.com${url.pathname}`;
},
'/blob/': (parts) => {
const [_, user, repo, , branch, ...filePath] = parts;
return `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filePath.join('/')}`;
}
};
if (!Object.keys(patterns).some(pattern => {
if (url.pathname.includes(pattern)) {
targetURL = patterns[pattern](url.pathname.split('/'));
return true;
}
return false;
})) {
targetURL = `https://github.com${url.pathname}${url.search}`;
}
}
try {
const newHeaders = new Headers(request.headers);
newHeaders.set('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
newHeaders.set('User-Agent', 'GitHub-Proxy');
const response = await fetch(targetURL, {
method: request.method,
headers: newHeaders,
body: request.body
});
console.log(`Proxy request to ${targetURL}, status: ${response.status}`);
const responseHeaders = new Headers(response.headers);
responseHeaders.set('Access-Control-Allow-Origin', '*');
if (response.redirected) {
const newUrl = new URL(response.url);
console.log(`Redirecting to: ${newUrl.pathname}${newUrl.search}`);
return Response.redirect(`https://${url.host}${newUrl.pathname}${newUrl.search}`, 302);
}
return new Response(response.body, {
status: response.status,
headers: responseHeaders
});
} catch (err) {
console.error(`Proxy request failed: ${err.message}`, {
targetURL,
error: err
});
return new Response(`代理请求失败: ${err.message}`, {
status: 500,
headers: {
'Content-Type': 'text/plain;charset=UTF-8'
}
});
}
}
}