mirror of
https://github.com/ciiiii/cloudflare-docker-proxy.git
synced 2026-03-19 09:29:46 +08:00
Fix containerd unauthorized response header (#63)
This commit is contained in:
44
src/index.js
44
src/index.js
@@ -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) {
|
||||
@@ -153,3 +142,22 @@ async function fetchToken(wwwAuthenticate, scope, 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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user