From f6acad4f3f59dbe887de2f014aa9577f44aedb5e Mon Sep 17 00:00:00 2001 From: ciiiii Date: Tue, 22 Mar 2022 04:00:50 +0800 Subject: [PATCH] improve use of worker variables --- src/index.js | 19 ++++++++++++------- wrangler.toml | 11 +++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index d8f9d1e..8f7e77b 100644 --- a/src/index.js +++ b/src/index.js @@ -14,26 +14,31 @@ function routeByHosts(host) { if (host in routes) { return routes[host]; } - return TARGET_UPSTREAM ? TARGET_UPSTREAM : ""; + if (MODE == "debug") { + return TARGET_UPSTREAM; + } + return ""; } async function handleRequest(request) { const url = new URL(request.url); if (url.pathname == "/v2/") { - const resp = new Response({}, { status: 401 }); - resp.headers = new Headers(); - if (DEBUG) { - resp.headers.set( + const headers = new Headers(); + if (MODE == "debug") { + headers.set( "Www-Authenticate", `Bearer realm="${LOCAL_ADDRESS}/v2/auth",service="cloudflare-docker-proxy"` ); } else { - resp.headers.set( + headers.set( "Www-Authenticate", `Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"` ); } - return resp; + return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), { + status: 401, + headers: headers, + }); } const upstream = routeByHosts(url.hostname); if (upstream === "") { diff --git a/wrangler.toml b/wrangler.toml index d3ce805..5bff45b 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -16,5 +16,12 @@ port = 8787 local_protocol="http" upstream_protocol="https" -[env.dev] -vars={DEBUG=true, LOCAL_ADDRESS="http://192.168.50.160:8787", TARGET_UPSTREAM="https://registry-1.docker.io"} \ No newline at end of file +[vars] +MODE="production" +LOCAL_ADDRESS="" +TARGET_UPSTREAM="" + +[env.dev.vars] +MODE="debug" +LOCAL_ADDRESS="http://192.168.50.160:8787" +TARGET_UPSTREAM="https://registry-1.docker.io" \ No newline at end of file