更新 main.go
This commit is contained in:
38
main.go
38
main.go
@@ -115,10 +115,8 @@ func preprocessArgs() {
|
|||||||
newArgs = append(newArgs, arg)
|
newArgs = append(newArgs, arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(newArgs) > 0 {
|
if len(newArgs) > 1 {
|
||||||
os.Args = newArgs
|
os.Args = newArgs
|
||||||
} else {
|
|
||||||
logrus.Warn("命令行参数为空,使用原始参数")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +229,9 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 健康检查
|
// 健康检查
|
||||||
if path == "/health" || path == "/healthz" {
|
if path == "/health" || path == "/healthz" {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write([]byte("OK"))
|
if _, err := w.Write([]byte("OK")); err != nil {
|
||||||
|
logrus.Errorf("健康检查响应失败: %v", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,20 +262,26 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCleanHost(r *http.Request) string {
|
||||||
|
host := r.Host
|
||||||
|
if idx := strings.Index(host, ":"); idx != -1 {
|
||||||
|
return host[:idx]
|
||||||
|
}
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
|
||||||
func handleRegistryRequest(w http.ResponseWriter, r *http.Request) {
|
func handleRegistryRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
const targetHost = "registry-1.docker.io"
|
const targetHost = "registry-1.docker.io"
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
pathParts := strings.Split(r.URL.Path, "/")
|
path := strings.TrimPrefix(r.URL.Path, "/v2/")
|
||||||
v2PathParts := pathParts[2:]
|
|
||||||
pathString := strings.Join(v2PathParts, "/")
|
|
||||||
|
|
||||||
url := &url.URL{
|
url := &url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
Host: targetHost,
|
Host: targetHost,
|
||||||
Path: "/v2/" + pathString,
|
Path: "/v2/" + path,
|
||||||
RawQuery: r.URL.RawQuery,
|
RawQuery: r.URL.RawQuery,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +310,7 @@ func handleRegistryRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
respHeaders := copyHeaders(resp.Header)
|
respHeaders := copyHeaders(resp.Header)
|
||||||
|
|
||||||
if respHeaders.Get("WWW-Authenticate") != "" {
|
if respHeaders.Get("WWW-Authenticate") != "" {
|
||||||
currentDomain := r.Host
|
currentDomain := getCleanHost(r)
|
||||||
respHeaders.Set("WWW-Authenticate",
|
respHeaders.Set("WWW-Authenticate",
|
||||||
fmt.Sprintf(`Bearer realm="https://%s/auth/token", service="registry.docker.io"`, currentDomain))
|
fmt.Sprintf(`Bearer realm="https://%s/auth/token", service="registry.docker.io"`, currentDomain))
|
||||||
}
|
}
|
||||||
@@ -334,14 +340,12 @@ func handleAuthRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
pathParts := strings.Split(r.URL.Path, "/")
|
path := strings.TrimPrefix(r.URL.Path, "/auth/")
|
||||||
authPathParts := pathParts[2:]
|
|
||||||
pathString := strings.Join(authPathParts, "/")
|
|
||||||
|
|
||||||
url := &url.URL{
|
url := &url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
Host: targetHost,
|
Host: targetHost,
|
||||||
Path: "/" + pathString,
|
Path: "/" + path,
|
||||||
RawQuery: r.URL.RawQuery,
|
RawQuery: r.URL.RawQuery,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,14 +391,12 @@ func handleCloudflareRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
pathParts := strings.Split(r.URL.Path, "/")
|
path := strings.TrimPrefix(r.URL.Path, "/production-cloudflare/")
|
||||||
cfPathParts := pathParts[2:]
|
|
||||||
pathString := strings.Join(cfPathParts, "/")
|
|
||||||
|
|
||||||
url := &url.URL{
|
url := &url.URL{
|
||||||
Scheme: "https",
|
Scheme: "https",
|
||||||
Host: targetHost,
|
Host: targetHost,
|
||||||
Path: "/" + pathString,
|
Path: "/" + path,
|
||||||
RawQuery: r.URL.RawQuery,
|
RawQuery: r.URL.RawQuery,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,7 +444,7 @@ func handleAuthChallenge(w http.ResponseWriter, r *http.Request, resp *http.Resp
|
|||||||
}
|
}
|
||||||
|
|
||||||
if authHeader := w.Header().Get("WWW-Authenticate"); authHeader != "" {
|
if authHeader := w.Header().Get("WWW-Authenticate"); authHeader != "" {
|
||||||
currentDomain := r.Host
|
currentDomain := getCleanHost(r)
|
||||||
w.Header().Set("WWW-Authenticate",
|
w.Header().Set("WWW-Authenticate",
|
||||||
fmt.Sprintf(`Bearer realm="https://%s/auth/token", service="registry.docker.io"`, currentDomain))
|
fmt.Sprintf(`Bearer realm="https://%s/auth/token", service="registry.docker.io"`, currentDomain))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user