diff --git a/src/index.js b/src/index.js index 97b088521..9e2616d43 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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") { @@ -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) { @@ -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, + }); +} diff --git a/wrangler.toml b/wrangler.toml index c3077a0ff..bcb22eb49 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -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] @@ -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"