-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
45 lines (37 loc) · 1.48 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
// Cloudflare Worker Script with Ping Endpoint
async function handleRequest(request) {
const { pathname } = new URL(request.url);
// Check if the request is for the "/ping" endpoint
if (pathname === "/ping") {
return new Response("pong", {
status: 200,
headers: {
"Content-Type": "text/plain",
},
});
}
// Docker Hub mirror logic
const dockerHubURL = `https://registry-1.docker.io${pathname}`;
const modifiedHeaders = new Headers(request.headers);
modifiedHeaders.set("Host", "registry-1.docker.io");
const response = await fetch(dockerHubURL, {
method: request.method,
headers: modifiedHeaders,
body: request.method !== "GET" && request.method !== "HEAD" ? request.body : null,
});
const workerURL = `https://${request.headers.get("host")}`;
const modifiedResponseHeaders = new Headers(response.headers);
modifiedResponseHeaders.set("Access-Control-Allow-Origin", "*");
if (modifiedResponseHeaders.has("location")) {
const location = modifiedResponseHeaders.get("location");
const newLocation = location.replace("https://registry-1.docker.io", workerURL);
modifiedResponseHeaders.set("location", newLocation);
}
return new Response(response.body, {
status: response.status,
headers: modifiedResponseHeaders,
});
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});