-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.js
54 lines (46 loc) · 1.9 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run "npm run dev" in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run "npm run deploy" to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// 定义你要代理的Maven仓库
const repositories = {
'central': 'https://repo.maven.apache.org/maven2',
'google': 'https://maven.google.com',
'jitpack': 'https://jitpack.io',
'gradle-plugins': 'https://plugins.gradle.org/m2',
'jcenter': 'https://jcenter.bintray.com',
'spring-plugins': 'https://repo.spring.io/plugins-release',
'spring-milestones': 'https://repo.spring.io/milestone',
'spring-snapshots': 'https://repo.spring.io/snapshot',
'atlassian': 'https://maven.atlassian.com/repository/public',
'apache-snapshots': 'https://repository.apache.org/snapshots',
'redhat-ga': 'https://maven.repository.redhat.com/ga',
'redhat-ea': 'https://maven.repository.redhat.com/earlyaccess/all'
};
// 解析请求路径,确定要代理的仓库
const repo = Object.keys(repositories).find(key => url.pathname.startsWith(`/${key}`));
console.log(`repo: ${repo}`);
if (!repo) {
return new Response('Repository not found', { status: 404 });
}
// 构建新的URL
const targetUrl = repositories[repo] + url.pathname.replace(`/${repo}`, '') + url.search;
// 进行代理请求
const response = await fetch(targetUrl, {
method: request.method,
headers: request.headers
});
return new Response(response.body, {
status: response.status,
headers: response.headers
});
},
};