| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package mediaproxy // import "miniflux.app/v2/internal/mediaproxy"
- import (
- "slices"
- "strings"
- "miniflux.app/v2/internal/config"
- "miniflux.app/v2/internal/reader/sanitizer"
- "miniflux.app/v2/internal/urllib"
- "github.com/PuerkitoBio/goquery"
- "github.com/gorilla/mux"
- )
- type urlProxyRewriter func(router *mux.Router, url string) string
- func RewriteDocumentWithRelativeProxyURL(router *mux.Router, htmlDocument string) string {
- return genericProxyRewriter(router, ProxifyRelativeURL, htmlDocument)
- }
- func RewriteDocumentWithAbsoluteProxyURL(router *mux.Router, htmlDocument string) string {
- return genericProxyRewriter(router, ProxifyAbsoluteURL, htmlDocument)
- }
- func genericProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, htmlDocument string) string {
- proxyOption := config.Opts.MediaProxyMode()
- if proxyOption == "none" {
- return htmlDocument
- }
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDocument))
- if err != nil {
- return htmlDocument
- }
- for _, mediaType := range config.Opts.MediaProxyResourceTypes() {
- switch mediaType {
- case "image":
- doc.Find("img, picture source").Each(func(i int, img *goquery.Selection) {
- if srcAttrValue, ok := img.Attr("src"); ok {
- if shouldProxy(srcAttrValue, proxyOption) {
- img.SetAttr("src", proxifyFunction(router, srcAttrValue))
- }
- }
- if srcsetAttrValue, ok := img.Attr("srcset"); ok {
- proxifySourceSet(img, router, proxifyFunction, proxyOption, srcsetAttrValue)
- }
- })
- if !slices.Contains(config.Opts.MediaProxyResourceTypes(), "video") {
- doc.Find("video").Each(func(i int, video *goquery.Selection) {
- if posterAttrValue, ok := video.Attr("poster"); ok {
- if shouldProxy(posterAttrValue, proxyOption) {
- video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
- }
- }
- })
- }
- case "audio":
- doc.Find("audio, audio source").Each(func(i int, audio *goquery.Selection) {
- if srcAttrValue, ok := audio.Attr("src"); ok {
- if shouldProxy(srcAttrValue, proxyOption) {
- audio.SetAttr("src", proxifyFunction(router, srcAttrValue))
- }
- }
- })
- case "video":
- doc.Find("video, video source").Each(func(i int, video *goquery.Selection) {
- if srcAttrValue, ok := video.Attr("src"); ok {
- if shouldProxy(srcAttrValue, proxyOption) {
- video.SetAttr("src", proxifyFunction(router, srcAttrValue))
- }
- }
- if posterAttrValue, ok := video.Attr("poster"); ok {
- if shouldProxy(posterAttrValue, proxyOption) {
- video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
- }
- }
- })
- }
- }
- output, err := doc.FindMatcher(goquery.Single("body")).Html()
- if err != nil {
- return htmlDocument
- }
- return output
- }
- func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyOption, srcsetAttrValue string) {
- imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
- for _, imageCandidate := range imageCandidates {
- if shouldProxy(imageCandidate.ImageURL, proxyOption) {
- imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
- }
- }
- element.SetAttr("srcset", imageCandidates.String())
- }
- func shouldProxy(attrValue, proxyOption string) bool {
- if strings.HasPrefix(attrValue, "data:") {
- return false
- }
- if proxyOption == "all" {
- return true
- }
- if !urllib.IsHTTPS(attrValue) {
- return true
- }
- return false
- }
|