2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-10-08 01:21:45 -04:00
|
|
|
|
2024-03-20 23:59:09 -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"
|
2024-03-18 23:18:09 -04:00
|
|
|
"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
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
2024-07-29 17:46:56 -04:00
|
|
|
"miniflux.app/v2/internal/http/route"
|
2020-10-08 01:21:45 -04:00
|
|
|
)
|
|
|
|
|
2024-03-20 23:59:09 -04:00
|
|
|
func ProxifyRelativeURL(router *mux.Router, mediaURL string) string {
|
2024-03-18 23:18:09 -04:00
|
|
|
if mediaURL == "" {
|
2024-03-13 18:16:58 -04:00
|
|
|
return ""
|
|
|
|
}
|
2022-10-15 02:17:17 -04:00
|
|
|
|
2024-03-20 23:59:09 -04:00
|
|
|
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != "" {
|
|
|
|
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
|
2022-10-15 02:17:17 -04:00
|
|
|
}
|
2024-03-13 18:16:58 -04:00
|
|
|
|
2024-03-20 23:59:09 -04:00
|
|
|
mac := hmac.New(sha256.New, config.Opts.MediaProxyPrivateKey())
|
2024-03-18 23:18:09 -04:00
|
|
|
mac.Write([]byte(mediaURL))
|
2024-03-13 18:16:58 -04:00
|
|
|
digest := mac.Sum(nil)
|
2024-03-18 23:18:09 -04:00
|
|
|
return route.Path(router, "proxy", "encodedDigest", base64.URLEncoding.EncodeToString(digest), "encodedURL", base64.URLEncoding.EncodeToString([]byte(mediaURL)))
|
2022-10-15 02:17:17 -04:00
|
|
|
}
|
|
|
|
|
2024-07-29 17:46:56 -04:00
|
|
|
func ProxifyAbsoluteURL(router *mux.Router, mediaURL string) string {
|
2024-03-18 23:18:09 -04:00
|
|
|
if mediaURL == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-29 23:33:47 -04:00
|
|
|
|
2024-03-20 23:59:09 -04:00
|
|
|
if customProxyURL := config.Opts.MediaCustomProxyURL(); customProxyURL != "" {
|
|
|
|
return proxifyURLWithCustomProxy(mediaURL, customProxyURL)
|
2024-03-13 18:16:58 -04:00
|
|
|
}
|
2024-03-18 23:18:09 -04:00
|
|
|
|
2024-03-20 23:59:09 -04:00
|
|
|
proxifiedUrl := ProxifyRelativeURL(router, mediaURL)
|
2024-07-29 17:46:56 -04:00
|
|
|
|
|
|
|
absoluteURL, err := url.JoinPath(config.Opts.BaseURL(), proxifiedUrl)
|
|
|
|
if err != nil {
|
|
|
|
return mediaURL
|
2024-03-18 23:18:09 -04:00
|
|
|
}
|
|
|
|
|
2024-07-29 17:46:56 -04:00
|
|
|
return absoluteURL
|
2024-03-18 23:18:09 -04:00
|
|
|
}
|
|
|
|
|
2024-03-20 23:59:09 -04:00
|
|
|
func proxifyURLWithCustomProxy(mediaURL, customProxyURL string) string {
|
2024-03-18 23:18:09 -04:00
|
|
|
if customProxyURL == "" {
|
|
|
|
return mediaURL
|
2022-07-06 00:13:40 -04:00
|
|
|
}
|
2024-03-18 23:18:09 -04:00
|
|
|
|
2024-07-29 17:46:56 -04:00
|
|
|
absoluteURL, err := url.JoinPath(customProxyURL, base64.URLEncoding.EncodeToString([]byte(mediaURL)))
|
2024-03-18 23:18:09 -04:00
|
|
|
if err != nil {
|
|
|
|
slog.Error("Incorrect custom media proxy URL",
|
|
|
|
slog.String("custom_proxy_url", customProxyURL),
|
|
|
|
slog.Any("error", err),
|
|
|
|
)
|
|
|
|
return mediaURL
|
|
|
|
}
|
|
|
|
|
2024-07-29 17:46:56 -04:00
|
|
|
return absoluteURL
|
2020-10-08 01:21:45 -04:00
|
|
|
}
|