1
0
Fork 0

Minor simplification of internal/proxy/proxy.go

- re-use ProxifiedUrl to implement AbsoluteProxifyURL, reducing the copy-pasta
- reduce the internal indentation of ProxifiedUrl by inverting some conditions
This commit is contained in:
jvoisin 2024-03-13 23:16:58 +01:00 committed by Frédéric Guillot
parent e0ee28c013
commit 66b8483791

View file

@ -19,51 +19,34 @@ import (
// ProxifyURL generates a relative URL for a proxified resource. // ProxifyURL generates a relative URL for a proxified resource.
func ProxifyURL(router *mux.Router, link string) string { func ProxifyURL(router *mux.Router, link string) string {
if link != "" { if link == "" {
proxyImageUrl := config.Opts.ProxyUrl() return ""
}
if proxyImageUrl := config.Opts.ProxyUrl(); proxyImageUrl != "" {
proxyUrl, err := url.Parse(proxyImageUrl)
if err != nil {
return ""
}
proxyUrl.Path = path.Join(proxyUrl.Path, base64.URLEncoding.EncodeToString([]byte(link)))
return proxyUrl.String()
}
if proxyImageUrl == "" {
mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey()) mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey())
mac.Write([]byte(link)) mac.Write([]byte(link))
digest := mac.Sum(nil) digest := mac.Sum(nil)
return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link))) return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
} }
proxyUrl, err := url.Parse(proxyImageUrl)
if err != nil {
return ""
}
proxyUrl.Path = path.Join(proxyUrl.Path, base64.URLEncoding.EncodeToString([]byte(link)))
return proxyUrl.String()
}
return ""
}
// AbsoluteProxifyURL generates an absolute URL for a proxified resource. // AbsoluteProxifyURL generates an absolute URL for a proxified resource.
func AbsoluteProxifyURL(router *mux.Router, host, link string) string { func AbsoluteProxifyURL(router *mux.Router, host, link string) string {
if link != "" { proxifiedUrl := ProxifyURL(router, link)
proxyImageUrl := config.Opts.ProxyUrl()
if proxyImageUrl == "" { if config.Opts.ProxyUrl() == "" {
mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey()) return proxifiedUrl
mac.Write([]byte(link)) }
digest := mac.Sum(nil)
path := route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link)))
if config.Opts.HTTPS { if config.Opts.HTTPS {
return "https://" + host + path return "https://" + host + proxifiedUrl
} else {
return "http://" + host + path
} }
} return "http://" + host + proxifiedUrl
proxyUrl, err := url.Parse(proxyImageUrl)
if err != nil {
return ""
}
proxyUrl.Path = path.Join(proxyUrl.Path, base64.URLEncoding.EncodeToString([]byte(link)))
return proxyUrl.String()
}
return ""
} }