mirror of
https://github.com/ImSingee/hammal.git
synced 2024-11-21 21:59:22 +08:00
disable org && support docker official images
This commit is contained in:
parent
413a3a4c6b
commit
11ceb5fc85
@ -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<Response> {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user