Skip to content
Closed

test #102

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});

const dockerHub = "https://registry-1.docker.io";
const dockerHub = "https://hub.docker.com/";

const routes = {
// production
"docker.libcuda.so": dockerHub,
"quay.libcuda.so": "https://quay.io",
"gcr.libcuda.so": "https://gcr.io",
"k8s-gcr.libcuda.so": "https://k8s.gcr.io",
"k8s.libcuda.so": "https://registry.k8s.io",
"ghcr.libcuda.so": "https://ghcr.io",
"cloudsmith.libcuda.so": "https://docker.cloudsmith.io",
"ecr.libcuda.so": "https://public.ecr.aws",
["docker." + CUSTOM_DOMAIN]: dockerHub,
["quay." + CUSTOM_DOMAIN]: "https://quay.io",
["gcr." + CUSTOM_DOMAIN]: "https://gcr.io",
["k8s-gcr." + CUSTOM_DOMAIN]: "https://k8s.gcr.io",
["k8s." + CUSTOM_DOMAIN]: "https://registry.k8s.io",
["ghcr." + CUSTOM_DOMAIN]: "https://ghcr.io",
["cloudsmith." + CUSTOM_DOMAIN]: "https://docker.cloudsmith.io",
["ecr." + CUSTOM_DOMAIN]: "https://public.ecr.aws",

// staging
"docker-staging.libcuda.so": dockerHub,
["docker-staging." + CUSTOM_DOMAIN]: dockerHub,
};

function routeByHosts(host) {
Expand Down Expand Up @@ -58,24 +58,9 @@ async function handleRequest(request) {
redirect: "follow",
});
if (resp.status === 401) {
if (MODE == "debug") {
headers.set(
"Www-Authenticate",
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
);
} else {
headers.set(
"Www-Authenticate",
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
);
}
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
status: 401,
headers: headers,
});
} else {
return resp;
return responseUnauthorized(url);
}
return resp;
}
// get token
if (url.pathname == "/v2/auth") {
Expand Down Expand Up @@ -122,7 +107,11 @@ async function handleRequest(request) {
headers: request.headers,
redirect: "follow",
});
return await fetch(newReq);
const resp = await fetch(newReq);
if (resp.status == 401) {
return responseUnauthorized(url);
}
return resp;
}

function parseAuthenticate(authenticateStr) {
Expand All @@ -147,9 +136,28 @@ async function fetchToken(wwwAuthenticate, scope, authorization) {
if (scope) {
url.searchParams.set("scope", scope);
}
headers = new Headers();
const headers = new Headers();
if (authorization) {
headers.set("Authorization", authorization);
}
return await fetch(url, { method: "GET", headers: headers });
}

function responseUnauthorized(url) {
const headers = new(Headers);
if (MODE == "debug") {
headers.set(
"Www-Authenticate",
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
);
} else {
headers.set(
"Www-Authenticate",
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
);
}
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
status: 401,
headers: headers,
});
}
16 changes: 8 additions & 8 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ TARGET_UPSTREAM = "https://registry-1.docker.io"
[env.production]
name = "cloudflare-docker-proxy"
# routes = [
# { pattern = "docker.libcuda.so", custom_domain = true },
# { pattern = "quay.libcuda.so", custom_domain = true },
# { pattern = "gcr.libcuda.so", custom_domain = true },
# { pattern = "k8s-gcr.libcuda.so", custom_domain = true },
# { pattern = "k8s.libcuda.so", custom_domain = true },
# { pattern = "ghcr.libcuda.so", custom_domain = true },
# { pattern = "cloudsmith.libcuda.so", custom_domain = true },
# { pattern = "docker.wbshi.top", custom_domain = true },
# { pattern = "quay.wbshi.top", custom_domain = true },
# { pattern = "gcr.wbshi.top", custom_domain = true },
# { pattern = "k8s-gcr.wbshi.top", custom_domain = true },
# { pattern = "k8s.wbshi.top", custom_domain = true },
# { pattern = "ghcr.wbshi.top", custom_domain = true },
# { pattern = "cloudsmith.wbshi.top", custom_domain = true },
# ]

[env.production.vars]
Expand All @@ -28,7 +28,7 @@ TARGET_UPSTREAM = ""

[env.staging]
name = "cloudflare-docker-proxy-staging"
# route = { pattern = "docker-staging.libcuda.so", custom_domain = true }
# route = { pattern = "docker-staging.wbshi.top", custom_domain = true }

[env.staging.vars]
MODE = "staging"
Expand Down