12 Commits

Author SHA1 Message Date
ciiiii
143b47184a Change redirect mode from follow to manual 2025-02-16 12:27:48 -06:00
shxyke
703fae4e63 chore: Update deployment configuration for custom domain (#66)
Some checks failed
Deploy to Cloudflare Workers / Build & Deploy (push) Has been cancelled
2024-10-08 11:31:11 +08:00
简简aw
24d7c9fc90 Fix headers variable initialization (#71) 2024-10-08 11:30:07 +08:00
Yisheng Cai
696009dd69 Trigger staging deploy with pull_request_target (#24)
* Trigger with pull_request_target

* Fix event type

* Remove conditions
2024-10-08 11:27:23 +08:00
STARRY-S
1bc56391bb Fix containerd unauthorized response header (#63) 2024-10-08 11:24:49 +08:00
Yisheng Cai
aa61ad58cf Fix routes 2024-06-25 03:14:44 +08:00
Yisheng Cai
d82c47d53a Add public ecr registry 2024-06-25 03:08:08 +08:00
Yisheng Cai
74b03d2aaf Update README.md 2024-06-24 00:39:28 +08:00
Yisheng Cai
8df9982c2b Remove routes from config (#39) 2024-06-21 16:59:10 +08:00
意琦行
d1d3bc252c fix: domain typo (#23) 2024-06-21 16:52:51 +08:00
Yisheng Cai
00b8c83650 Deploy staging env (#21)
* Deploy staging env

* Fix ci

* Fix staging configuration

* Fix production configuration
2024-06-14 02:33:52 +08:00
Yisheng Cai
7fbc589095 Fix DockerHub library images default pulling case (#20) 2024-06-14 01:46:36 +08:00
5 changed files with 109 additions and 50 deletions

View File

@@ -13,15 +13,15 @@ jobs:
runs-on: ubuntu-latest
name: Build & Deploy
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: "12.x"
- run: npm install
- uses: actions/checkout@v4
- name: Publish
uses: cloudflare/wrangler-action@2.0.0
uses: cloudflare/wrangler-action@v3
env:
CUSTOM_DOMAIN: ${{ secrets.CUSTOM_DOMAIN || 'libcuda.so' }}
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
env:
CF_ACCOUNT_ID: ${{secrets.CF_ACCOUNT_ID}}
accountId: ${{secrets.CF_ACCOUNT_ID}}
vars:
CUSTOM_DOMAIN
command: deploy --env production --minify src/index.js
environment: production

25
.github/workflows/stage.yaml vendored Normal file
View File

@@ -0,0 +1,25 @@
name: Deploy to Cloudflare Workers(Staging)
on:
pull_request_target:
paths-ignore:
- '**.md'
repository_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
name: Build & Deploy
steps:
- 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

View File

@@ -8,13 +8,13 @@
## Deploy
1. fork this project
2. modify the link of the above button to your fork url
3. click the button, you will be redirected to the deploy page
1. click the "Deploy With Workers" button
2. follow the instructions to fork and deploy
3. update routes as you requirement
[![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/ciiiii/cloudflare-docker-proxy)
## Config tutorial
## Routes configuration tutorial
1. use cloudflare worker host: only support proxy one registry
```javascript

View File

@@ -6,13 +6,18 @@ addEventListener("fetch", (event) => {
const dockerHub = "https://registry-1.docker.io";
const routes = {
"docker.libcuda.so": dockerHub,
"quay.libcuda.so": "https://quay.io",
"gcr.libcuda.so": "https://gcr.io",
"k8s-gcr.libcuda.so": "https://k8s.gcr.io",
"k8s.libcuda.so": "https://registry.k8s.io",
"ghcr.libcuda.so": "https://ghcr.io",
"cloudsmith.libcuda.so": "https://docker.cloudsmith.io",
// production
["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." + CUSTOM_DOMAIN]: dockerHub,
};
function routeByHosts(host) {
@@ -53,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") {
@@ -115,9 +105,13 @@ async function handleRequest(request) {
const newReq = new Request(newUrl, {
method: request.method,
headers: request.headers,
redirect: "follow",
redirect: "manual",
});
return await fetch(newReq);
const resp = await fetch(newReq);
if (resp.status == 401) {
return responseUnauthorized(url);
}
return resp;
}
function parseAuthenticate(authenticateStr) {
@@ -142,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,
});
}

View File

@@ -1,18 +1,39 @@
name = "cloudflare-docker-proxy"
workers_dev = true
main = "src/index.js"
compatibility_date = "2021-12-07"
compatibility_date = "2023-12-01"
[dev]
ip = "0.0.0.0"
port = 8787
local_protocol="http"
upstream_protocol="https"
local_protocol = "http"
[vars]
MODE="production"
TARGET_UPSTREAM=""
[env.vars]
CUSTOM_DOMAIN = "libcuda.so"
[env.dev.vars]
MODE="debug"
TARGET_UPSTREAM="https://registry-1.docker.io"
MODE = "debug"
TARGET_UPSTREAM = "https://registry-1.docker.io"
CUSTOM_DOMAIN = "exmaple.com"
[env.production]
name = "cloudflare-docker-proxy"
# routes = [
# { pattern = "docker.libcuda.so", custom_domain = true },
# { pattern = "quay.libcuda.so", custom_domain = true },
# { pattern = "gcr.libcuda.so", custom_domain = true },
# { pattern = "k8s-gcr.libcuda.so", custom_domain = true },
# { pattern = "k8s.libcuda.so", custom_domain = true },
# { pattern = "ghcr.libcuda.so", custom_domain = true },
# { pattern = "cloudsmith.libcuda.so", custom_domain = true },
# ]
[env.production.vars]
MODE = "production"
TARGET_UPSTREAM = ""
[env.staging]
name = "cloudflare-docker-proxy-staging"
# route = { pattern = "docker-staging.libcuda.so", custom_domain = true }
[env.staging.vars]
MODE = "staging"
TARGET_UPSTREAM = ""