mirror of
https://github.com/ciiiii/cloudflare-docker-proxy.git
synced 2025-12-06 14:42:51 +08:00
Compare commits
5 Commits
yscai/dock
...
yscai/ci-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
803d1b49d2 | ||
|
|
da1c68867e | ||
|
|
fbc76c1ae6 | ||
|
|
00b8c83650 | ||
|
|
7fbc589095 |
14
.github/workflows/deploy.yaml
vendored
14
.github/workflows/deploy.yaml
vendored
@@ -13,15 +13,11 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
name: Build & Deploy
|
name: Build & Deploy
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v4
|
||||||
- name: Use Node.js
|
|
||||||
uses: actions/setup-node@v1
|
|
||||||
with:
|
|
||||||
node-version: "12.x"
|
|
||||||
- run: npm install
|
|
||||||
- name: Publish
|
- name: Publish
|
||||||
uses: cloudflare/wrangler-action@2.0.0
|
uses: cloudflare/wrangler-action@v3
|
||||||
with:
|
with:
|
||||||
apiToken: ${{ secrets.CF_API_TOKEN }}
|
apiToken: ${{ secrets.CF_API_TOKEN }}
|
||||||
env:
|
accountId: ${{secrets.CF_ACCOUNT_ID}}
|
||||||
CF_ACCOUNT_ID: ${{secrets.CF_ACCOUNT_ID}}
|
command: deploy --env production --minify src/index.js
|
||||||
|
environment: production
|
||||||
21
.github/workflows/stage.yaml
vendored
Normal file
21
.github/workflows/stage.yaml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
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
|
||||||
|
with:
|
||||||
|
apiToken: ${{ secrets.CF_API_TOKEN }}
|
||||||
|
accountId: ${{secrets.CF_ACCOUNT_ID}}
|
||||||
|
command: deploy --env staging --minify src/index.js
|
||||||
|
environment: staging
|
||||||
40
src/index.js
40
src/index.js
@@ -3,14 +3,20 @@ addEventListener("fetch", (event) => {
|
|||||||
event.respondWith(handleRequest(event.request));
|
event.respondWith(handleRequest(event.request));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const dockerHub = "https://registry-1.docker.io";
|
||||||
|
|
||||||
const routes = {
|
const routes = {
|
||||||
"docker.libcuda.so": "https://registry-1.docker.io",
|
// production
|
||||||
|
"docker.libcuda.so": dockerHub,
|
||||||
"quay.libcuda.so": "https://quay.io",
|
"quay.libcuda.so": "https://quay.io",
|
||||||
"gcr.libcuda.so": "https://gcr.io",
|
"gcr.libcuda.so": "https://gcr.io",
|
||||||
"k8s-gcr.libcuda.so": "https://k8s.gcr.io",
|
"k8s-gcr.libcuda.so": "https://k8s.gcr.io",
|
||||||
"k8s.libcuda.so": "https://registry.k8s.io",
|
"k8s.libcuda.so": "https://registry.k8s.io",
|
||||||
"ghcr.libcuda.so": "https://ghcr.io",
|
"ghcr.libcuda.so": "https://ghcr.io",
|
||||||
"cloudsmith.libcuda.so": "https://docker.cloudsmith.io",
|
"cloudsmith.libcuda.so": "https://docker.cloudsmith.io",
|
||||||
|
|
||||||
|
// staging
|
||||||
|
"docker-staging.libcuda.so": dockerHub,
|
||||||
};
|
};
|
||||||
|
|
||||||
function routeByHosts(host) {
|
function routeByHosts(host) {
|
||||||
@@ -36,6 +42,7 @@ async function handleRequest(request) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
const isDockerHub = upstream == dockerHub;
|
||||||
const authorization = request.headers.get("Authorization");
|
const authorization = request.headers.get("Authorization");
|
||||||
if (url.pathname == "/v2/") {
|
if (url.pathname == "/v2/") {
|
||||||
const newUrl = new URL(upstream + "/v2/");
|
const newUrl = new URL(upstream + "/v2/");
|
||||||
@@ -84,7 +91,28 @@ async function handleRequest(request) {
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
const wwwAuthenticate = parseAuthenticate(authenticateStr);
|
const wwwAuthenticate = parseAuthenticate(authenticateStr);
|
||||||
return await fetchToken(wwwAuthenticate, url.searchParams, authorization);
|
let scope = url.searchParams.get("scope");
|
||||||
|
// autocomplete repo part into scope for DockerHub library images
|
||||||
|
// Example: repository:busybox:pull => repository:library/busybox:pull
|
||||||
|
if (scope && isDockerHub) {
|
||||||
|
let scopeParts = scope.split(":");
|
||||||
|
if (scopeParts.length == 3 && !scopeParts[1].includes("/")) {
|
||||||
|
scopeParts[1] = "library/" + scopeParts[1];
|
||||||
|
scope = scopeParts.join(":");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return await fetchToken(wwwAuthenticate, scope, authorization);
|
||||||
|
}
|
||||||
|
// redirect for DockerHub library images
|
||||||
|
// Example: /v2/busybox/manifests/latest => /v2/library/busybox/manifests/latest
|
||||||
|
if (isDockerHub) {
|
||||||
|
const pathParts = url.pathname.split("/");
|
||||||
|
if (pathParts.length == 5) {
|
||||||
|
pathParts.splice(2, 0, "library");
|
||||||
|
const redirectUrl = new URL(url);
|
||||||
|
redirectUrl.pathname = pathParts.join("/");
|
||||||
|
return Response.redirect(redirectUrl, 301);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// foward requests
|
// foward requests
|
||||||
const newUrl = new URL(upstream + url.pathname);
|
const newUrl = new URL(upstream + url.pathname);
|
||||||
@@ -101,7 +129,7 @@ function parseAuthenticate(authenticateStr) {
|
|||||||
// match strings after =" and before "
|
// match strings after =" and before "
|
||||||
const re = /(?<=\=")(?:\\.|[^"\\])*(?=")/g;
|
const re = /(?<=\=")(?:\\.|[^"\\])*(?=")/g;
|
||||||
const matches = authenticateStr.match(re);
|
const matches = authenticateStr.match(re);
|
||||||
if (matches === null || matches.length < 2) {
|
if (matches == null || matches.length < 2) {
|
||||||
throw new Error(`invalid Www-Authenticate Header: ${authenticateStr}`);
|
throw new Error(`invalid Www-Authenticate Header: ${authenticateStr}`);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -110,13 +138,13 @@ function parseAuthenticate(authenticateStr) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchToken(wwwAuthenticate, searchParams, authorization) {
|
async function fetchToken(wwwAuthenticate, scope, authorization) {
|
||||||
const url = new URL(wwwAuthenticate.realm);
|
const url = new URL(wwwAuthenticate.realm);
|
||||||
if (wwwAuthenticate.service.length) {
|
if (wwwAuthenticate.service.length) {
|
||||||
url.searchParams.set("service", wwwAuthenticate.service);
|
url.searchParams.set("service", wwwAuthenticate.service);
|
||||||
}
|
}
|
||||||
if (searchParams.get("scope")) {
|
if (scope) {
|
||||||
url.searchParams.set("scope", searchParams.get("scope"));
|
url.searchParams.set("scope", scope);
|
||||||
}
|
}
|
||||||
headers = new Headers();
|
headers = new Headers();
|
||||||
if (authorization) {
|
if (authorization) {
|
||||||
|
|||||||
@@ -1,18 +1,35 @@
|
|||||||
name = "cloudflare-docker-proxy"
|
name = "cloudflare-docker-proxy"
|
||||||
workers_dev = true
|
compatibility_date = "2023-12-01"
|
||||||
main = "src/index.js"
|
|
||||||
compatibility_date = "2021-12-07"
|
|
||||||
|
|
||||||
[dev]
|
[dev]
|
||||||
ip = "0.0.0.0"
|
ip = "0.0.0.0"
|
||||||
port = 8787
|
port = 8787
|
||||||
local_protocol = "http"
|
local_protocol = "http"
|
||||||
upstream_protocol="https"
|
|
||||||
|
|
||||||
[vars]
|
|
||||||
MODE="production"
|
|
||||||
TARGET_UPSTREAM=""
|
|
||||||
|
|
||||||
[env.dev.vars]
|
[env.dev.vars]
|
||||||
MODE = "debug"
|
MODE = "debug"
|
||||||
TARGET_UPSTREAM = "https://registry-1.docker.io"
|
TARGET_UPSTREAM = "https://registry-1.docker.io"
|
||||||
|
|
||||||
|
[env.production]
|
||||||
|
name = "cloudflare-docker-proxy"
|
||||||
|
routes = [
|
||||||
|
{ pattern = "docker.libcuda.so", custom_domain = true },
|
||||||
|
{ pattern = "quey.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 = ""
|
||||||
|
|||||||
Reference in New Issue
Block a user