diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 8473370..21f7d81 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -16,8 +16,12 @@ jobs: - uses: actions/checkout@v4 - name: Publish uses: cloudflare/wrangler-action@v3 + env: + CUSTOM_DOMAIN: ${{ secrets.CUSTOM_DOMAIN || 'libcuda.so' }} with: apiToken: ${{ secrets.CF_API_TOKEN }} accountId: ${{secrets.CF_ACCOUNT_ID}} + vars: + CUSTOM_DOMAIN command: deploy --env production --minify src/index.js environment: production \ No newline at end of file diff --git a/.github/workflows/stage.yaml b/.github/workflows/stage.yaml index db045f6..1cd233c 100644 --- a/.github/workflows/stage.yaml +++ b/.github/workflows/stage.yaml @@ -1,9 +1,7 @@ name: Deploy to Cloudflare Workers(Staging) on: - pull_request: - branches: - - master + pull_request_target: paths-ignore: - '**.md' repository_dispatch: @@ -16,8 +14,12 @@ jobs: - uses: actions/checkout@v4 - name: Publish uses: cloudflare/wrangler-action@v3 + env: + CUSTOM_DOMAIN: ${{ secrets.CUSTOM_DOMAIN || 'libcuda.so' }} with: apiToken: ${{ secrets.CF_API_TOKEN }} accountId: ${{secrets.CF_ACCOUNT_ID}} + vars: + CUSTOM_DOMAIN command: deploy --env staging --minify src/index.js environment: staging \ No newline at end of file diff --git a/src/index.js b/src/index.js index 24cfa7e..785a41a 100644 --- a/src/index.js +++ b/src/index.js @@ -7,17 +7,17 @@ const dockerHub = "https://registry-1.docker.io"; const routes = { // production - "docker.lolicon.in": dockerHub, - "quay.lolicon.in": "https://quay.io", - "gcr.lolicon.in": "https://gcr.io", - "k8s-gcr.lolicon.in": "https://k8s.gcr.io", - "k8s.lolicon.in": "https://registry.k8s.io", - "ghcr.lolicon.in": "https://ghcr.io", - "cloudsmith.lolicon.in": "https://docker.cloudsmith.io", - "ecr.lolicon.in": "https://public.ecr.aws", + ["docker." + CUSTOM_DOMAIN]: dockerHub, + ["quay." + CUSTOM_DOMAIN]: "https://quay.io", + ["gcr." + CUSTOM_DOMAIN]: "https://gcr.io", + ["k8s-gcr." + CUSTOM_DOMAIN]: "https://k8s.gcr.io", + ["k8s." + CUSTOM_DOMAIN]: "https://registry.k8s.io", + ["ghcr." + CUSTOM_DOMAIN]: "https://ghcr.io", + ["cloudsmith." + CUSTOM_DOMAIN]: "https://docker.cloudsmith.io", + ["ecr." + CUSTOM_DOMAIN]: "https://public.ecr.aws", // staging - "docker-staging.lolicon.in": dockerHub, + ["docker-staging." + CUSTOM_DOMAIN]: dockerHub, }; function routeByHosts(host) { @@ -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) { @@ -147,9 +136,28 @@ async function fetchToken(wwwAuthenticate, scope, authorization) { if (scope) { url.searchParams.set("scope", scope); } - headers = new Headers(); + const headers = new Headers(); if (authorization) { headers.set("Authorization", 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, + }); +} diff --git a/wrangler.toml b/wrangler.toml index e269251..2c8d116 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -6,6 +6,9 @@ ip = "0.0.0.0" port = 8787 local_protocol = "http" +[env.vars] +CUSTOM_DOMAIN = "libcuda.so" + [env.dev.vars] MODE = "debug" TARGET_UPSTREAM = "https://registry-1.docker.io"