13 Commits

Author SHA1 Message Date
ciiiii
001a9fdd51 Redirect for index route 2025-06-01 16:13:12 +08:00
ciiiii
dfac79db55 Handle dockerhub blob redirect
Some checks failed
Deploy to Cloudflare Workers / Build & Deploy (push) Has been cancelled
2025-02-16 18:14:20 -06:00
ciiiii
e176bc4b29 Remove stage deployment 2025-02-16 12:35:01 -06:00
Yisheng Cai
47001590eb Change redirect mode from follow to manual (#104) 2025-02-17 02:30:59 +08: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
8 changed files with 3609 additions and 566 deletions

View File

@@ -16,8 +16,12 @@ jobs:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Publish - name: Publish
uses: cloudflare/wrangler-action@v3 uses: cloudflare/wrangler-action@v3
env:
CUSTOM_DOMAIN: ${{ secrets.CUSTOM_DOMAIN || 'libcuda.so' }}
with: with:
apiToken: ${{ secrets.CF_API_TOKEN }} apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{secrets.CF_ACCOUNT_ID}} accountId: ${{secrets.CF_ACCOUNT_ID}}
vars:
CUSTOM_DOMAIN
command: deploy --env production --minify src/index.js command: deploy --env production --minify src/index.js
environment: production environment: production

View File

@@ -1,23 +0,0 @@
name: Deploy to Cloudflare Workers(Staging)
on:
pull_request:
branches:
- master
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
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{secrets.CF_ACCOUNT_ID}}
command: deploy --env staging --minify src/index.js
environment: staging

View File

@@ -8,13 +8,13 @@
## Deploy ## Deploy
1. fork this project 1. click the "Deploy With Workers" button
2. modify the link of the above button to your fork url 2. follow the instructions to fork and deploy
3. click the button, you will be redirected to the deploy page 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) [![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 1. use cloudflare worker host: only support proxy one registry
```javascript ```javascript

3023
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,7 @@
"scripts": { "scripts": {
"format": "prettier --write '**/*.{js,css,json,md}'", "format": "prettier --write '**/*.{js,css,json,md}'",
"build": "webpack", "build": "webpack",
"dev": "wrangler dev src/index.js --env dev" "dev": "npx wrangler dev src/index.js --env dev"
}, },
"license": "MIT", "license": "MIT",
"main": "src/index.js" "main": "src/index.js"

View File

@@ -7,16 +7,17 @@ const dockerHub = "https://registry-1.docker.io";
const routes = { const routes = {
// production // production
"docker.libcuda.so": dockerHub, ["docker." + CUSTOM_DOMAIN]: dockerHub,
"quay.libcuda.so": "https://quay.io", ["quay." + CUSTOM_DOMAIN]: "https://quay.io",
"gcr.libcuda.so": "https://gcr.io", ["gcr." + CUSTOM_DOMAIN]: "https://gcr.io",
"k8s-gcr.libcuda.so": "https://k8s.gcr.io", ["k8s-gcr." + CUSTOM_DOMAIN]: "https://k8s.gcr.io",
"k8s.libcuda.so": "https://registry.k8s.io", ["k8s." + CUSTOM_DOMAIN]: "https://registry.k8s.io",
"ghcr.libcuda.so": "https://ghcr.io", ["ghcr." + CUSTOM_DOMAIN]: "https://ghcr.io",
"cloudsmith.libcuda.so": "https://docker.cloudsmith.io", ["cloudsmith." + CUSTOM_DOMAIN]: "https://docker.cloudsmith.io",
["ecr." + CUSTOM_DOMAIN]: "https://public.ecr.aws",
// staging // staging
"docker-staging.libcuda.so": dockerHub, ["docker-staging." + CUSTOM_DOMAIN]: dockerHub,
}; };
function routeByHosts(host) { function routeByHosts(host) {
@@ -31,6 +32,9 @@ function routeByHosts(host) {
async function handleRequest(request) { async function handleRequest(request) {
const url = new URL(request.url); const url = new URL(request.url);
if (url.pathname == "/") {
return Response.redirect(url.protocol + "//" + url.host + "/v2/", 301);
}
const upstream = routeByHosts(url.hostname); const upstream = routeByHosts(url.hostname);
if (upstream === "") { if (upstream === "") {
return new Response( return new Response(
@@ -57,24 +61,9 @@ async function handleRequest(request) {
redirect: "follow", redirect: "follow",
}); });
if (resp.status === 401) { if (resp.status === 401) {
if (MODE == "debug") { return responseUnauthorized(url);
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 resp;
} }
// get token // get token
if (url.pathname == "/v2/auth") { if (url.pathname == "/v2/auth") {
@@ -119,9 +108,23 @@ async function handleRequest(request) {
const newReq = new Request(newUrl, { const newReq = new Request(newUrl, {
method: request.method, method: request.method,
headers: request.headers, headers: request.headers,
redirect: "follow", // don't follow redirect to dockerhub blob upstream
redirect: isDockerHub ? "manual" : "follow",
}); });
return await fetch(newReq); const resp = await fetch(newReq);
if (resp.status == 401) {
return responseUnauthorized(url);
}
// handle dockerhub blob redirect manually
if (isDockerHub && resp.status == 307) {
const location = new URL(resp.headers.get("Location"));
const redirectResp = await fetch(location.toString(), {
method: "GET",
redirect: "follow",
});
return redirectResp;
}
return resp;
} }
function parseAuthenticate(authenticateStr) { function parseAuthenticate(authenticateStr) {
@@ -146,9 +149,28 @@ async function fetchToken(wwwAuthenticate, scope, authorization) {
if (scope) { if (scope) {
url.searchParams.set("scope", scope); url.searchParams.set("scope", scope);
} }
headers = new Headers(); const headers = new Headers();
if (authorization) { if (authorization) {
headers.set("Authorization", authorization); headers.set("Authorization", authorization);
} }
return await fetch(url, { method: "GET", headers: headers }); 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

@@ -6,21 +6,25 @@ ip = "0.0.0.0"
port = 8787 port = 8787
local_protocol = "http" local_protocol = "http"
[env.vars]
CUSTOM_DOMAIN = "libcuda.so"
[env.dev.vars] [env.dev.vars]
MODE = "debug" MODE = "debug"
TARGET_UPSTREAM = "https://registry-1.docker.io" TARGET_UPSTREAM = "https://registry-1.docker.io"
CUSTOM_DOMAIN = "exmaple.com"
[env.production] [env.production]
name = "cloudflare-docker-proxy" name = "cloudflare-docker-proxy"
routes = [ # routes = [
{ pattern = "docker.libcuda.so", custom_domain = true }, # { pattern = "docker.libcuda.so", custom_domain = true },
{ pattern = "quey.libcuda.so", custom_domain = true }, # { pattern = "quay.libcuda.so", custom_domain = true },
{ pattern = "gcr.libcuda.so", custom_domain = true }, # { pattern = "gcr.libcuda.so", custom_domain = true },
{ pattern = "k8s-gcr.libcuda.so", custom_domain = true }, # { pattern = "k8s-gcr.libcuda.so", custom_domain = true },
{ pattern = "k8s.libcuda.so", custom_domain = true }, # { pattern = "k8s.libcuda.so", custom_domain = true },
{ pattern = "ghcr.libcuda.so", custom_domain = true }, # { pattern = "ghcr.libcuda.so", custom_domain = true },
{ pattern = "cloudsmith.libcuda.so", custom_domain = true }, # { pattern = "cloudsmith.libcuda.so", custom_domain = true },
] # ]
[env.production.vars] [env.production.vars]
MODE = "production" MODE = "production"
@@ -28,7 +32,7 @@ TARGET_UPSTREAM = ""
[env.staging] [env.staging]
name = "cloudflare-docker-proxy-staging" name = "cloudflare-docker-proxy-staging"
route = { pattern = "docker-staging.libcuda.so", custom_domain = true } # route = { pattern = "docker-staging.libcuda.so", custom_domain = true }
[env.staging.vars] [env.staging.vars]
MODE = "staging" MODE = "staging"

1013
yarn.lock

File diff suppressed because it is too large Load Diff