From 9dfe8b6b62ad4251dfa2825252f172862d25215e Mon Sep 17 00:00:00 2001 From: Holger Huo Date: Thu, 26 Jun 2025 14:54:37 +0800 Subject: [PATCH] fix: host-based mapping with local backend --- handler/router.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/handler/router.go b/handler/router.go index 50a81c09..0dc56db7 100644 --- a/handler/router.go +++ b/handler/router.go @@ -73,10 +73,20 @@ func Convert(c *fiber.Ctx) error { // Rewrite the target backend if a mapping rule matches the hostname if hostMap, hostMapFound := config.Config.ImageMap[reqHost]; hostMapFound { log.Debugf("Found host mapping %s -> %s", reqHostname, hostMap) - targetHostUrl, _ := url.Parse(hostMap) - targetHostName = targetHostUrl.Host - targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host - proxyMode = true + + // Check if hostMap is a URL or local path + if strings.HasPrefix(hostMap, "http://") || strings.HasPrefix(hostMap, "https://") { + // Remote URL mapping + targetHostUrl, _ := url.Parse(hostMap) + targetHostName = targetHostUrl.Host + targetHost = targetHostUrl.Scheme + "://" + targetHostUrl.Host + proxyMode = true + } else { + // Local path mapping + targetHost = hostMap + proxyMode = false + mapMode = true + } } else { // There's not matching host mapping, now check for any URI map that apply httpRegexpMatcher := regexp.MustCompile(config.HttpRegexp) @@ -121,7 +131,11 @@ func Convert(c *fiber.Ctx) error { // Since here we've already sent non-image file, "raw" is not supported by default in the following code if config.AllowAllExtensions && !helper.CheckImageExtension(filename) { if !proxyMode { - return c.SendFile(path.Join(config.Config.ImgPath, reqURI)) + if !mapMode { + return c.SendFile(path.Join(config.Config.ImgPath, reqURI)) + } else { + return c.SendFile(path.Join(targetHost, reqURI)) + } } else { // If the file is not in the ImgPath, we'll have to use the proxy mode to download it _ = fetchRemoteImg(realRemoteAddr, targetHostName)