@@ -25,27 +25,6 @@ function routeByHosts(host) {
25
25
26
26
async function handleRequest ( request ) {
27
27
const url = new URL ( request . url ) ;
28
- const authorization = request . headers . get ( "Authorization" ) ;
29
- if ( url . pathname == "/v2/" ) {
30
- if ( authorization === null || authorization === "" ) {
31
- const headers = new Headers ( ) ;
32
- if ( MODE == "debug" ) {
33
- headers . set (
34
- "Www-Authenticate" ,
35
- `Bearer realm="http://${ url . host } /v2/auth",service="cloudflare-docker-proxy"`
36
- ) ;
37
- } else {
38
- headers . set (
39
- "Www-Authenticate" ,
40
- `Bearer realm="https://${ url . hostname } /v2/auth",service="cloudflare-docker-proxy"`
41
- ) ;
42
- }
43
- return new Response ( JSON . stringify ( { message : "UNAUTHORIZED" } ) , {
44
- status : 401 ,
45
- headers : headers ,
46
- } ) ;
47
- }
48
- }
49
28
const upstream = routeByHosts ( url . hostname ) ;
50
29
if ( upstream === "" ) {
51
30
return new Response (
@@ -57,20 +36,24 @@ async function handleRequest(request) {
57
36
}
58
37
) ;
59
38
}
60
- // check if need to authenticate
39
+ const authorization = request . headers . get ( "Authorization" ) ;
61
40
if ( url . pathname == "/v2/" ) {
62
41
const newUrl = new URL ( upstream + "/v2/" ) ;
42
+ const headers = new Headers ( ) ;
43
+ if ( authorization ) {
44
+ headers . set ( "Authorization" , authorization ) ;
45
+ }
46
+ // check if need to authenticate
63
47
const resp = await fetch ( newUrl . toString ( ) , {
64
48
method : "GET" ,
49
+ headers : headers ,
65
50
redirect : "follow" ,
66
51
} ) ;
67
- if ( resp . status === 200 ) {
68
- } else if ( resp . status === 401 ) {
69
- const headers = new Headers ( ) ;
52
+ if ( resp . status === 401 ) {
70
53
if ( MODE == "debug" ) {
71
54
headers . set (
72
55
"Www-Authenticate" ,
73
- `Bearer realm="${ LOCAL_ADDRESS } /v2/auth",service="cloudflare-docker-proxy"`
56
+ `Bearer realm="http:// ${ url . host } /v2/auth",service="cloudflare-docker-proxy"`
74
57
) ;
75
58
} else {
76
59
headers . set (
0 commit comments