1
0
Fork 0
miniflux/internal/mediaproxy/url.go

69 lines
1.7 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2020-10-08 01:21:45 -04:00
package mediaproxy // import "miniflux.app/v2/internal/mediaproxy"
2020-10-08 01:21:45 -04:00
import (
2022-10-15 02:17:17 -04:00
"crypto/hmac"
"crypto/sha256"
2020-10-08 01:21:45 -04:00
"encoding/base64"
"log/slog"
2022-08-29 23:33:47 -04:00
"net/url"
2020-10-08 01:21:45 -04:00
"github.com/gorilla/mux"
2022-08-29 23:33:47 -04:00
"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/http/route"
2020-10-08 01:21:45 -04:00
)
func ProxifyRelativeURL(router *mux.Router, mediaURL string) string {
if mediaURL == "" {
return ""
}
2022-10-15 02:17:17 -04:00
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != "" {
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
2022-10-15 02:17:17 -04:00
}
mac := hmac.New(sha256.New, config.Opts.MediaProxyPrivateKey())
mac.Write([]byte(mediaURL))
digest := mac.Sum(nil)
return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(mediaURL)))
2022-10-15 02:17:17 -04:00
}
func ProxifyAbsoluteURL(router *mux.Router, mediaURL string) string {
if mediaURL == "" {
return ""
}
2022-08-29 23:33:47 -04:00
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != "" {
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
}
proxifiedUrl := ProxifyRelativeURL(router, mediaURL)
absoluteURL, err := url.JoinPath(config.Opts.BaseURL(), proxifiedUrl)
if err != nil {
return mediaURL
}
return absoluteURL
}
func proxifyURLWithCustomProxy(mediaURL, customProxyURL string) string {
if customProxyURL == "" {
return mediaURL
2022-07-06 00:13:40 -04:00
}
absoluteURL, err := url.JoinPath(customProxyURL, base64.URLEncoding.EncodeToString([]byte(mediaURL)))
if err != nil {
slog.Error("Incorrect custom media proxy URL",
slog.String("custom_proxy_url", customProxyURL),
slog.Any("error", err),
)
return mediaURL
}
return absoluteURL
2020-10-08 01:21:45 -04:00
}