2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2023-02-25 09:36:19 +01:00
|
|
|
|
2024-03-21 04:59:09 +01:00
|
|
|
package mediaproxy // import "miniflux.app/v2/internal/mediaproxy"
|
2023-02-25 09:36:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/reader/sanitizer"
|
2023-08-14 04:09:01 +02:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2023-02-25 09:36:19 +01:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type urlProxyRewriter func(router *mux.Router, url string) string
|
|
|
|
|
2024-07-23 10:08:18 +02:00
|
|
|
func RewriteDocumentWithRelativeProxyURL(router *mux.Router, htmlDocument string, feedSiteURL ...string) string {
|
|
|
|
proxifyFunction := func(router *mux.Router, mediaURL string) string {
|
|
|
|
return ProxifyRelativeURL(router, mediaURL, feedSiteURL...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return genericProxyRewriter(router, proxifyFunction, htmlDocument)
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|
|
|
|
|
2024-07-23 10:08:18 +02:00
|
|
|
func RewriteDocumentWithAbsoluteProxyURL(router *mux.Router, host, htmlDocument string, feedSiteURL ...string) string {
|
2023-02-25 09:36:19 +01:00
|
|
|
proxifyFunction := func(router *mux.Router, url string) string {
|
2024-07-23 10:08:18 +02:00
|
|
|
return ProxifyAbsoluteURL(router, host, url, feedSiteURL...)
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|
2024-03-21 04:59:09 +01:00
|
|
|
return genericProxyRewriter(router, proxifyFunction, htmlDocument)
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 04:59:09 +01:00
|
|
|
func genericProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, htmlDocument string) string {
|
|
|
|
proxyOption := config.Opts.MediaProxyMode()
|
2023-02-25 09:36:19 +01:00
|
|
|
if proxyOption == "none" {
|
2024-03-21 04:59:09 +01:00
|
|
|
return htmlDocument
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 04:59:09 +01:00
|
|
|
doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDocument))
|
2023-02-25 09:36:19 +01:00
|
|
|
if err != nil {
|
2024-03-21 04:59:09 +01:00
|
|
|
return htmlDocument
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 04:59:09 +01:00
|
|
|
for _, mediaType := range config.Opts.MediaProxyResourceTypes() {
|
2023-02-25 09:36:19 +01:00
|
|
|
switch mediaType {
|
|
|
|
case "image":
|
2024-03-03 23:39:24 +01:00
|
|
|
doc.Find("img, picture source").Each(func(i int, img *goquery.Selection) {
|
2023-02-25 09:36:19 +01:00
|
|
|
if srcAttrValue, ok := img.Attr("src"); ok {
|
2024-03-03 23:39:24 +01:00
|
|
|
if shouldProxy(srcAttrValue, proxyOption) {
|
2023-02-25 09:36:19 +01:00
|
|
|
img.SetAttr("src", proxifyFunction(router, srcAttrValue))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if srcsetAttrValue, ok := img.Attr("srcset"); ok {
|
|
|
|
proxifySourceSet(img, router, proxifyFunction, proxyOption, srcsetAttrValue)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-02-24 03:11:09 +01:00
|
|
|
doc.Find("video").Each(func(i int, video *goquery.Selection) {
|
|
|
|
if posterAttrValue, ok := video.Attr("poster"); ok {
|
2024-03-03 23:39:24 +01:00
|
|
|
if shouldProxy(posterAttrValue, proxyOption) {
|
2024-02-24 03:11:09 +01:00
|
|
|
video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-02-25 09:36:19 +01:00
|
|
|
case "audio":
|
2024-03-03 23:39:24 +01:00
|
|
|
doc.Find("audio, audio source").Each(func(i int, audio *goquery.Selection) {
|
2023-02-25 09:36:19 +01:00
|
|
|
if srcAttrValue, ok := audio.Attr("src"); ok {
|
2024-03-03 23:39:24 +01:00
|
|
|
if shouldProxy(srcAttrValue, proxyOption) {
|
2023-02-25 09:36:19 +01:00
|
|
|
audio.SetAttr("src", proxifyFunction(router, srcAttrValue))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
case "video":
|
2024-03-03 23:39:24 +01:00
|
|
|
doc.Find("video, video source").Each(func(i int, video *goquery.Selection) {
|
2023-02-25 09:36:19 +01:00
|
|
|
if srcAttrValue, ok := video.Attr("src"); ok {
|
2024-03-03 23:39:24 +01:00
|
|
|
if shouldProxy(srcAttrValue, proxyOption) {
|
2023-02-25 09:36:19 +01:00
|
|
|
video.SetAttr("src", proxifyFunction(router, srcAttrValue))
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 03:11:09 +01:00
|
|
|
|
|
|
|
if posterAttrValue, ok := video.Attr("poster"); ok {
|
2024-03-03 23:39:24 +01:00
|
|
|
if shouldProxy(posterAttrValue, proxyOption) {
|
2024-02-24 03:11:09 +01:00
|
|
|
video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 09:36:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := doc.Find("body").First().Html()
|
|
|
|
if err != nil {
|
2024-03-21 04:59:09 +01:00
|
|
|
return htmlDocument
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyOption, srcsetAttrValue string) {
|
|
|
|
imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
|
|
|
|
|
|
|
|
for _, imageCandidate := range imageCandidates {
|
2024-03-03 23:39:24 +01:00
|
|
|
if shouldProxy(imageCandidate.ImageURL, proxyOption) {
|
2023-02-25 09:36:19 +01:00
|
|
|
imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.SetAttr("srcset", imageCandidates.String())
|
|
|
|
}
|
|
|
|
|
2024-03-03 23:39:24 +01:00
|
|
|
func shouldProxy(attrValue, proxyOption string) bool {
|
|
|
|
return !strings.HasPrefix(attrValue, "data:") &&
|
|
|
|
(proxyOption == "all" || !urllib.IsHTTPS(attrValue))
|
2023-02-25 09:36:19 +01:00
|
|
|
}
|