From e168469603a7cb7aab30c79b97bc2c8b0127ff2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=B7=E6=84=8F=E6=B4=8B=E6=B4=8B?= <862860000@qq.com> Date: Sun, 22 Sep 2024 17:19:13 +0800 Subject: [PATCH] Fix containerd unauthorized response header Copy Code: https://github.com/ciiiii/cloudflare-docker-proxy/pull/63 --- src/index.js | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 0ed4c749a..b2029144f 100644 --- a/src/index.js +++ b/src/index.js @@ -57,24 +57,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") { @@ -121,7 +106,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) { @@ -152,3 +141,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, + }); +}