disable org && support docker official images

This commit is contained in:
Singee 2023-06-29 15:26:54 +08:00
parent 413a3a4c6b
commit 11ceb5fc85
No known key found for this signature in database
GPG Key ID: 4EC3FD10E03041D7

View File

@ -3,11 +3,11 @@ import { Backend } from './backend'
const PROXY_HEADER_ALLOW_LIST: string[] = ["accept", "user-agent", "accept-encoding"] const PROXY_HEADER_ALLOW_LIST: string[] = ["accept", "user-agent", "accept-encoding"]
const ORG_NAME_BACKEND:{ [key: string]: string; } = { // const ORG_NAME_BACKEND:{ [key: string]: string; } = {
"gcr": "https://gcr.io", // "gcr": "https://gcr.io",
"k8sgcr": "https://k8s.gcr.io", // "k8sgcr": "https://k8s.gcr.io",
"quay": "https://quay.io", // "quay": "https://quay.io",
} // }
const DEFAULT_BACKEND_HOST: string = "https://registry-1.docker.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 { function orgNameFromPath(pathname: string): string|null {
const splitedPath: string[] = pathname.split("/", 3) // const splitedPath: string[] = pathname.split("/", 3)
if (splitedPath.length === 3 && splitedPath[0] === "" && splitedPath[1] === "v2") { // if (splitedPath.length === 3 && splitedPath[0] === "" && splitedPath[1] === "v2") {
return splitedPath[2].toLowerCase() // return splitedPath[2].toLowerCase()
} // }
return null return null
} }
function hostByOrgName(orgName: string|null): string { function hostByOrgName(orgName: string|null): string {
if (orgName !== null && orgName in ORG_NAME_BACKEND) { // if (orgName !== null && orgName in ORG_NAME_BACKEND) {
return ORG_NAME_BACKEND[orgName] // return ORG_NAME_BACKEND[orgName]
} // }
return DEFAULT_BACKEND_HOST return DEFAULT_BACKEND_HOST
} }
function rewritePathByOrg(orgName: string|null, pathname: string): string { function rewritePath(orgName: string | null, pathname: string): string {
if (orgName === null || !(orgName in ORG_NAME_BACKEND)) { let splitedPath = pathname.split("/");
return pathname
// /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 splitedPath.join("/")
return value !== orgName || index !== 2;
}) // if (orgName === null || !(orgName in ORG_NAME_BACKEND)) {
return cleanSplitedPath.join("/") // 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<Response> { async function handleRegistryRequest(request: Request): Promise<Response> {
const reqURL = new URL(request.url) const reqURL = new URL(request.url)
const orgName = orgNameFromPath(reqURL.pathname) const orgName = orgNameFromPath(reqURL.pathname)
const pathname = rewritePathByOrg(orgName, reqURL.pathname) const pathname = rewritePath(orgName, reqURL.pathname)
const host = hostByOrgName(orgName) const host = hostByOrgName(orgName)
const tokenProvider = new TokenProvider() const tokenProvider = new TokenProvider()
const backend = new Backend(host, tokenProvider) const backend = new Backend(host, tokenProvider)