From 11ceb5fc8527ab1880b0bb0a5791107e54a01dd0 Mon Sep 17 00:00:00 2001 From: Singee Date: Thu, 29 Jun 2023 15:26:54 +0800 Subject: [PATCH] disable org && support docker official images --- src/handler.ts | 51 +++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/handler.ts b/src/handler.ts index 22158a0..d5ebbc3 100644 --- a/src/handler.ts +++ b/src/handler.ts @@ -3,11 +3,11 @@ import { Backend } from './backend' const PROXY_HEADER_ALLOW_LIST: string[] = ["accept", "user-agent", "accept-encoding"] -const ORG_NAME_BACKEND:{ [key: string]: string; } = { - "gcr": "https://gcr.io", - "k8sgcr": "https://k8s.gcr.io", - "quay": "https://quay.io", -} +// const ORG_NAME_BACKEND:{ [key: string]: string; } = { +// "gcr": "https://gcr.io", +// "k8sgcr": "https://k8s.gcr.io", +// "quay": "https://quay.io", +// } const DEFAULT_BACKEND_HOST: string = "https://registry-1.docker.io" @@ -26,35 +26,44 @@ function copyProxyHeaders(inputHeaders: Headers) : Headers { } function orgNameFromPath(pathname: string): string|null { - const splitedPath: string[] = pathname.split("/", 3) - if (splitedPath.length === 3 && splitedPath[0] === "" && splitedPath[1] === "v2") { - return splitedPath[2].toLowerCase() - } + // const splitedPath: string[] = pathname.split("/", 3) + // if (splitedPath.length === 3 && splitedPath[0] === "" && splitedPath[1] === "v2") { + // return splitedPath[2].toLowerCase() + // } return null } function hostByOrgName(orgName: string|null): string { - if (orgName !== null && orgName in ORG_NAME_BACKEND) { - return ORG_NAME_BACKEND[orgName] - } + // if (orgName !== null && orgName in ORG_NAME_BACKEND) { + // return ORG_NAME_BACKEND[orgName] + // } return DEFAULT_BACKEND_HOST } -function rewritePathByOrg(orgName: string|null, pathname: string): string { - if (orgName === null || !(orgName in ORG_NAME_BACKEND)) { - return pathname +function rewritePath(orgName: string | null, pathname: string): string { + let splitedPath = pathname.split("/"); + + // /v2/repo -> /v2/library/repo + if (orgName === null && splitedPath.length === 3) { + splitedPath = [splitedPath[0], splitedPath[1], "library", splitedPath[2]] } - const splitedPath: string[] = pathname.split("/") - const cleanSplitedPath = splitedPath.filter(function(value: string, index: number) { - return value !== orgName || index !== 2; - }) - return cleanSplitedPath.join("/") + + return splitedPath.join("/") + + // if (orgName === null || !(orgName in ORG_NAME_BACKEND)) { + // return pathname + // } + // + // const cleanSplitedPath = splitedPath.filter(function(value: string, index: number) { + // return value !== orgName || index !== 2; + // }) + // return cleanSplitedPath.join("/") } async function handleRegistryRequest(request: Request): Promise { const reqURL = new URL(request.url) const orgName = orgNameFromPath(reqURL.pathname) - const pathname = rewritePathByOrg(orgName, reqURL.pathname) + const pathname = rewritePath(orgName, reqURL.pathname) const host = hostByOrgName(orgName) const tokenProvider = new TokenProvider() const backend = new Backend(host, tokenProvider)