Skip to content

fix: host-based mapping with local backend #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading