From 66b8483791e58030c0cc3b5c5d82b0b0d5e87cec Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 13 Mar 2024 23:16:58 +0100 Subject: [PATCH] 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 --- internal/proxy/proxy.go | 49 ++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 8b177348..512431df 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -19,51 +19,34 @@ import ( // ProxifyURL generates a relative URL for a proxified resource. func ProxifyURL(router *mux.Router, link string) string { - if link != "" { - proxyImageUrl := config.Opts.ProxyUrl() - - if proxyImageUrl == "" { - mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey()) - mac.Write([]byte(link)) - digest := mac.Sum(nil) - return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link))) - } + if link == "" { + 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() } - return "" + + mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey()) + mac.Write([]byte(link)) + digest := mac.Sum(nil) + return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(link))) } // AbsoluteProxifyURL generates an absolute URL for a proxified resource. func AbsoluteProxifyURL(router *mux.Router, host, link string) string { - if link != "" { - proxyImageUrl := config.Opts.ProxyUrl() + proxifiedUrl := ProxifyURL(router, link) - if proxyImageUrl == "" { - mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey()) - 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 { - return "https://" + host + path - } else { - return "http://" + host + path - } - } - - proxyUrl, err := url.Parse(proxyImageUrl) - if err != nil { - return "" - } - - proxyUrl.Path = path.Join(proxyUrl.Path, base64.URLEncoding.EncodeToString([]byte(link))) - return proxyUrl.String() + if config.Opts.ProxyUrl() == "" { + return proxifiedUrl } - return "" + if config.Opts.HTTPS { + return "https://" + host + proxifiedUrl + } + return "http://" + host + proxifiedUrl }