-
Notifications
You must be signed in to change notification settings - Fork 5
/
hub-workers.js
189 lines (178 loc) · 5.41 KB
/
hub-workers.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
177
178
179
180
181
182
183
184
185
186
187
188
189
const hub_host = "registry-1.docker.io";
const auth_url = "https://auth.docker.io";
const workers_url = "https://hub.rat.dev";
const banned_list = [];
export default {
async fetch(request) {
const url = new URL(request.url);
const getReqHeader = (key) => request.headers.get(key);
if (url.pathname === '/') {
return index();
}
if (banned_list.some((word) => url.pathname.includes(word))) {
return new Response("this image is not allowed", { status: 403 });
}
if (url.pathname === '/token') {
let token_parameter = {
headers: {
'Host': 'auth.docker.io',
'User-Agent': getReqHeader("User-Agent"),
'Accept': getReqHeader("Accept"),
'Accept-Language': getReqHeader("Accept-Language"),
'Accept-Encoding': getReqHeader("Accept-Encoding"),
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0'
},
cf: {
cacheTtlByStatus: { "200-299": 120, "404": 10, "500-599": 3 }
}
};
let token_url = auth_url + url.pathname + url.search
return fetch(new Request(token_url, request), token_parameter)
}
url.hostname = hub_host;
let parameter = {
headers: {
'Host': hub_host,
'User-Agent': getReqHeader("User-Agent"),
'Accept': getReqHeader("Accept"),
'Accept-Language': getReqHeader("Accept-Language"),
'Accept-Encoding': getReqHeader("Accept-Encoding"),
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0'
},
redirect: "follow",
cf: {
cacheTtlByStatus: { "200-299": 2592000, 404: 10, "500-599": 3 },
},
};
if (request.headers.has("Authorization")) {
parameter.headers.Authorization = getReqHeader("Authorization");
}
let res = await fetch(new Request(url, request), parameter);
res = new Response(res.body, res);
if (res.headers.has("Www-Authenticate")) {
let auth = res.headers.get("Www-Authenticate");
let re = new RegExp(auth_url, "g");
res.headers.set(
"Www-Authenticate",
res.headers.get("Www-Authenticate").replace(re, workers_url)
);
}
return res;
},
};
async function index() {
return new Response(
`<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hub Proxy</title>
<style>
body {
background-color: #f9f9f9;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 2em auto;
background-color: #ffffff;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
h1 {
font-size: 2.5em;
margin-top: 0;
margin-bottom: 20px;
text-align: center;
color: #333;
border-bottom: 2px solid #ddd;
padding-bottom: 0.5em;
}
p {
color: #555;
line-height: 1.8;
text-align: center;
}
a {
text-decoration: none;
color: #007bff;
}
@media screen and (max-width: 768px) {
.container {
padding: 15px;
margin: 2em 15px;
}
h1 {
font-size: 1.8em;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Hub Proxy</h1>
<p>
由
<a target="_blank" href="https://github.com/TheTNB/panel">耗子面板</a>
强力驱动
</p>
<p><b>本站开启 UA 白名单,如果您的客户端无法拉取镜像可向我们反馈</b></p>
<p><b>同时本站有一定速率限制,如需大量请求建议您 <a target="_blank" href="https://github.com/TheTNB/proxy-conf">自建代理</a> 使用</b></p>
<p>交流反馈群:<a target="_blank" href="https://jq.qq.com/?_wv=1027&k=I1oJKSTH">12370907</a></p>
<p id="cf"></p>
</div>
<script>
async function updateCloudflareInfo() {
try {
const response = await fetch("/cdn-cgi/trace");
if (response.ok) {
const data = await response.text();
const lines = data.split("\\n");
const info = {};
lines.forEach((line) => {
const parts = line.split("=");
if (parts.length === 2) {
info[parts[0]] = parts[1];
}
});
const cfElement = document.getElementById("cf");
const displayText =
info.loc +
" " +
info.ip +
" | " +
info.colo +
" | " +
info.http +
" | " +
info.visit_scheme +
" | " +
info.tls +
" | " +
info.kex;
cfElement.textContent = displayText;
}
} catch (error) {
console.error("获取Cloudflare节点信息失败: ", error);
}
}
window.addEventListener("load", updateCloudflareInfo);
</script>
</body>
</html>`,
{
headers: {
"Content-Type": "text/html",
"Cache-Control": "public, max-age=2592000",
},
cf: {
cacheTtl: 2592000,
cacheEverything: true,
},
}
);
}