avoid nil *ptr & fix path

This commit is contained in:
wjqserver 2025-05-21 12:08:17 +08:00
parent 31d435bfa0
commit 430e313d47

View file

@ -104,8 +104,7 @@ func GhcrWithImageRouting(cfg *config.Config) app.HandlerFunc {
func GhcrToTarget(ctx context.Context, c *app.RequestContext, cfg *config.Config, target string, path string, image *imageInfo) { func GhcrToTarget(ctx context.Context, c *app.RequestContext, cfg *config.Config, target string, path string, image *imageInfo) {
if cfg.Docker.Enabled { if cfg.Docker.Enabled {
if target != "" { if target != "" {
GhcrRequest(ctx, c, "https://"+target+"/v2/"+path+string(c.Request.QueryString()), image, cfg, target) GhcrRequest(ctx, c, "https://"+target+"/v2/"+path+"?"+string(c.Request.QueryString()), image, cfg, target)
} else { } else {
if cfg.Docker.Target == "ghcr" { if cfg.Docker.Target == "ghcr" {
GhcrRequest(ctx, c, "https://"+ghcrTarget+string(c.Request.RequestURI()), image, cfg, ghcrTarget) GhcrRequest(ctx, c, "https://"+ghcrTarget+string(c.Request.RequestURI()), image, cfg, ghcrTarget)
@ -166,10 +165,12 @@ func GhcrRequest(ctx context.Context, c *app.RequestContext, u string, image *im
}) })
req.Header.Set("Host", target) req.Header.Set("Host", target)
token, exist := cache.Get(image.Image) if image != nil {
if exist { token, exist := cache.Get(image.Image)
logDebug("Use Cache Token: %s", token) if exist {
req.Header.Set("Authorization", "Bearer "+token) logDebug("Use Cache Token: %s", token)
req.Header.Set("Authorization", "Bearer "+token)
}
} }
resp, err = ghcrclient.Do(req) resp, err = ghcrclient.Do(req)
@ -183,6 +184,10 @@ func GhcrRequest(ctx context.Context, c *app.RequestContext, u string, image *im
// 请求target /v2/路径 // 请求target /v2/路径
if string(c.Request.URI().Path()) != "/v2/" { if string(c.Request.URI().Path()) != "/v2/" {
resp.Body.Close() resp.Body.Close()
if image == nil {
ErrorPage(c, NewErrorWithStatusLookup(401, "Unauthorized"))
return
}
token := ChallengeReq(target, image, ctx, c) token := ChallengeReq(target, image, ctx, c)
// 更新kv // 更新kv