mirror of
https://github.com/miniflux/v2.git
synced 2025-08-16 18:01:37 +00:00
refactor(mediaproxy): use *url.URL for MEDIA_PROXY_CUSTOM_URL
Same behaviour as for HTTP_CLIENT_PROXY.
This commit is contained in:
parent
40fa77851c
commit
ce6cadc176
5 changed files with 17 additions and 33 deletions
|
@ -1601,7 +1601,8 @@ func TestMediaProxyCustomURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
expected := "http://example.org/proxy"
|
expected := "http://example.org/proxy"
|
||||||
result := opts.MediaCustomProxyURL()
|
result := opts.MediaCustomProxyURL()
|
||||||
if result != expected {
|
|
||||||
|
if result == nil || result.String() != expected {
|
||||||
t.Fatalf(`Unexpected MEDIA_PROXY_CUSTOM_URL value, got %q instead of %q`, result, expected)
|
t.Fatalf(`Unexpected MEDIA_PROXY_CUSTOM_URL value, got %q instead of %q`, result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,7 +148,7 @@ type options struct {
|
||||||
mediaProxyHTTPClientTimeout int
|
mediaProxyHTTPClientTimeout int
|
||||||
mediaProxyMode string
|
mediaProxyMode string
|
||||||
mediaProxyResourceTypes []string
|
mediaProxyResourceTypes []string
|
||||||
mediaProxyCustomURL string
|
mediaProxyCustomURL *url.URL
|
||||||
fetchBilibiliWatchTime bool
|
fetchBilibiliWatchTime bool
|
||||||
fetchNebulaWatchTime bool
|
fetchNebulaWatchTime bool
|
||||||
fetchOdyseeWatchTime bool
|
fetchOdyseeWatchTime bool
|
||||||
|
@ -229,7 +229,7 @@ func NewOptions() *options {
|
||||||
mediaProxyHTTPClientTimeout: defaultMediaProxyHTTPClientTimeout,
|
mediaProxyHTTPClientTimeout: defaultMediaProxyHTTPClientTimeout,
|
||||||
mediaProxyMode: defaultMediaProxyMode,
|
mediaProxyMode: defaultMediaProxyMode,
|
||||||
mediaProxyResourceTypes: []string{defaultMediaResourceTypes},
|
mediaProxyResourceTypes: []string{defaultMediaResourceTypes},
|
||||||
mediaProxyCustomURL: defaultMediaProxyURL,
|
mediaProxyCustomURL: nil,
|
||||||
filterEntryMaxAgeDays: defaultFilterEntryMaxAgeDays,
|
filterEntryMaxAgeDays: defaultFilterEntryMaxAgeDays,
|
||||||
fetchBilibiliWatchTime: defaultFetchBilibiliWatchTime,
|
fetchBilibiliWatchTime: defaultFetchBilibiliWatchTime,
|
||||||
fetchNebulaWatchTime: defaultFetchNebulaWatchTime,
|
fetchNebulaWatchTime: defaultFetchNebulaWatchTime,
|
||||||
|
@ -563,7 +563,7 @@ func (o *options) MediaProxyResourceTypes() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MediaCustomProxyURL returns the custom proxy URL for medias.
|
// MediaCustomProxyURL returns the custom proxy URL for medias.
|
||||||
func (o *options) MediaCustomProxyURL() string {
|
func (o *options) MediaCustomProxyURL() *url.URL {
|
||||||
return o.mediaProxyCustomURL
|
return o.mediaProxyCustomURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -170,7 +170,10 @@ func (p *parser) parseLines(lines []string) (err error) {
|
||||||
rand.Read(randomKey)
|
rand.Read(randomKey)
|
||||||
p.opts.mediaProxyPrivateKey = parseBytes(value, randomKey)
|
p.opts.mediaProxyPrivateKey = parseBytes(value, randomKey)
|
||||||
case "MEDIA_PROXY_CUSTOM_URL":
|
case "MEDIA_PROXY_CUSTOM_URL":
|
||||||
p.opts.mediaProxyCustomURL = parseString(value, defaultMediaProxyURL)
|
p.opts.mediaProxyCustomURL, err = url.Parse(parseString(value, defaultMediaProxyURL))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("config: invalid MEDIA_PROXY_CUSTOM_URL value: %w", err)
|
||||||
|
}
|
||||||
case "CREATE_ADMIN":
|
case "CREATE_ADMIN":
|
||||||
p.opts.createAdmin = parseBool(value, defaultCreateAdmin)
|
p.opts.createAdmin = parseBool(value, defaultCreateAdmin)
|
||||||
case "ADMIN_USERNAME":
|
case "ADMIN_USERNAME":
|
||||||
|
|
|
@ -278,19 +278,8 @@ func TestProxyFilterWithHttpsAlwaysAndIncorrectCustomProxyServer(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
parser := config.NewParser()
|
parser := config.NewParser()
|
||||||
config.Opts, err = parser.ParseEnvironmentVariables()
|
config.Opts, err = parser.ParseEnvironmentVariables()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
t.Fatalf(`Parsing failure: %v`, err)
|
t.Fatalf(`Incorrect proxy URL silently accepted (MEDIA_PROXY_CUSTOM_URL=%q): %q`, os.Getenv("MEDIA_PROXY_CUSTOM_URL"), config.Opts.MediaCustomProxyURL())
|
||||||
}
|
|
||||||
|
|
||||||
r := mux.NewRouter()
|
|
||||||
r.HandleFunc("/proxy/{encodedDigest}/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy")
|
|
||||||
|
|
||||||
input := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
||||||
output := RewriteDocumentWithRelativeProxyURL(r, input)
|
|
||||||
expected := `<p><img src="https://website/folder/image.png" alt="Test"/></p>`
|
|
||||||
|
|
||||||
if expected != output {
|
|
||||||
t.Errorf(`Not expected output: got %q instead of %q`, output, expected)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"log/slog"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
@ -21,7 +20,7 @@ func ProxifyRelativeURL(router *mux.Router, mediaURL string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != "" {
|
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != nil {
|
||||||
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
|
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +35,7 @@ func ProxifyAbsoluteURL(router *mux.Router, mediaURL string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != "" {
|
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != nil {
|
||||||
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
|
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,19 +49,11 @@ func ProxifyAbsoluteURL(router *mux.Router, mediaURL string) string {
|
||||||
return absoluteURL
|
return absoluteURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxifyURLWithCustomProxy(mediaURL, customProxyURL string) string {
|
func proxifyURLWithCustomProxy(mediaURL string, customProxyURL *url.URL) string {
|
||||||
if customProxyURL == "" {
|
if customProxyURL == nil {
|
||||||
return mediaURL
|
return mediaURL
|
||||||
}
|
}
|
||||||
|
|
||||||
absoluteURL, err := url.JoinPath(customProxyURL, base64.URLEncoding.EncodeToString([]byte(mediaURL)))
|
absoluteURL := customProxyURL.JoinPath(base64.URLEncoding.EncodeToString([]byte(mediaURL)))
|
||||||
if err != nil {
|
return absoluteURL.String()
|
||||||
slog.Error("Incorrect custom media proxy URL",
|
|
||||||
slog.String("custom_proxy_url", customProxyURL),
|
|
||||||
slog.Any("error", err),
|
|
||||||
)
|
|
||||||
return mediaURL
|
|
||||||
}
|
|
||||||
|
|
||||||
return absoluteURL
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue