forked from GitHub/cloudflare-docker-proxy
improve use of worker variables
This commit is contained in:
19
src/index.js
19
src/index.js
@@ -14,26 +14,31 @@ function routeByHosts(host) {
|
|||||||
if (host in routes) {
|
if (host in routes) {
|
||||||
return routes[host];
|
return routes[host];
|
||||||
}
|
}
|
||||||
return TARGET_UPSTREAM ? TARGET_UPSTREAM : "";
|
if (MODE == "debug") {
|
||||||
|
return TARGET_UPSTREAM;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleRequest(request) {
|
async function handleRequest(request) {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
if (url.pathname == "/v2/") {
|
if (url.pathname == "/v2/") {
|
||||||
const resp = new Response({}, { status: 401 });
|
const headers = new Headers();
|
||||||
resp.headers = new Headers();
|
if (MODE == "debug") {
|
||||||
if (DEBUG) {
|
headers.set(
|
||||||
resp.headers.set(
|
|
||||||
"Www-Authenticate",
|
"Www-Authenticate",
|
||||||
`Bearer realm="${LOCAL_ADDRESS}/v2/auth",service="cloudflare-docker-proxy"`
|
`Bearer realm="${LOCAL_ADDRESS}/v2/auth",service="cloudflare-docker-proxy"`
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
resp.headers.set(
|
headers.set(
|
||||||
"Www-Authenticate",
|
"Www-Authenticate",
|
||||||
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
|
`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);
|
const upstream = routeByHosts(url.hostname);
|
||||||
if (upstream === "") {
|
if (upstream === "") {
|
||||||
|
|||||||
@@ -16,5 +16,12 @@ port = 8787
|
|||||||
local_protocol="http"
|
local_protocol="http"
|
||||||
upstream_protocol="https"
|
upstream_protocol="https"
|
||||||
|
|
||||||
[env.dev]
|
[vars]
|
||||||
vars={DEBUG=true, LOCAL_ADDRESS="http://192.168.50.160:8787", TARGET_UPSTREAM="https://registry-1.docker.io"}
|
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"
|
||||||
Reference in New Issue
Block a user