rewriter.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mediaproxy // import "miniflux.app/v2/internal/mediaproxy"
  4. import (
  5. "slices"
  6. "strings"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/reader/sanitizer"
  9. "miniflux.app/v2/internal/urllib"
  10. "github.com/PuerkitoBio/goquery"
  11. "github.com/gorilla/mux"
  12. )
  13. type urlProxyRewriter func(router *mux.Router, url string) string
  14. func RewriteDocumentWithRelativeProxyURL(router *mux.Router, htmlDocument string) string {
  15. return genericProxyRewriter(router, ProxifyRelativeURL, htmlDocument)
  16. }
  17. func RewriteDocumentWithAbsoluteProxyURL(router *mux.Router, htmlDocument string) string {
  18. return genericProxyRewriter(router, ProxifyAbsoluteURL, htmlDocument)
  19. }
  20. func genericProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, htmlDocument string) string {
  21. proxyOption := config.Opts.MediaProxyMode()
  22. if proxyOption == "none" {
  23. return htmlDocument
  24. }
  25. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDocument))
  26. if err != nil {
  27. return htmlDocument
  28. }
  29. for _, mediaType := range config.Opts.MediaProxyResourceTypes() {
  30. switch mediaType {
  31. case "image":
  32. doc.Find("img, picture source").Each(func(i int, img *goquery.Selection) {
  33. if srcAttrValue, ok := img.Attr("src"); ok {
  34. if shouldProxifyURL(srcAttrValue, proxyOption) {
  35. img.SetAttr("src", proxifyFunction(router, srcAttrValue))
  36. }
  37. }
  38. if srcsetAttrValue, ok := img.Attr("srcset"); ok {
  39. proxifySourceSet(img, router, proxifyFunction, proxyOption, srcsetAttrValue)
  40. }
  41. })
  42. if !slices.Contains(config.Opts.MediaProxyResourceTypes(), "video") {
  43. doc.Find("video").Each(func(i int, video *goquery.Selection) {
  44. if posterAttrValue, ok := video.Attr("poster"); ok {
  45. if shouldProxifyURL(posterAttrValue, proxyOption) {
  46. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  47. }
  48. }
  49. })
  50. }
  51. case "audio":
  52. doc.Find("audio, audio source").Each(func(i int, audio *goquery.Selection) {
  53. if srcAttrValue, ok := audio.Attr("src"); ok {
  54. if shouldProxifyURL(srcAttrValue, proxyOption) {
  55. audio.SetAttr("src", proxifyFunction(router, srcAttrValue))
  56. }
  57. }
  58. })
  59. case "video":
  60. doc.Find("video, video source").Each(func(i int, video *goquery.Selection) {
  61. if srcAttrValue, ok := video.Attr("src"); ok {
  62. if shouldProxifyURL(srcAttrValue, proxyOption) {
  63. video.SetAttr("src", proxifyFunction(router, srcAttrValue))
  64. }
  65. }
  66. if posterAttrValue, ok := video.Attr("poster"); ok {
  67. if shouldProxifyURL(posterAttrValue, proxyOption) {
  68. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  69. }
  70. }
  71. })
  72. }
  73. }
  74. output, err := doc.FindMatcher(goquery.Single("body")).Html()
  75. if err != nil {
  76. return htmlDocument
  77. }
  78. return output
  79. }
  80. func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyOption, srcsetAttrValue string) {
  81. imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
  82. for _, imageCandidate := range imageCandidates {
  83. if shouldProxifyURL(imageCandidate.ImageURL, proxyOption) {
  84. imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
  85. }
  86. }
  87. element.SetAttr("srcset", imageCandidates.String())
  88. }
  89. // shouldProxifyURL checks if the media URL should be proxified based on the media proxy option and URL scheme.
  90. func shouldProxifyURL(mediaURL, mediaProxyOption string) bool {
  91. switch {
  92. case mediaURL == "":
  93. return false
  94. case strings.HasPrefix(mediaURL, "data:"):
  95. return false
  96. case mediaProxyOption == "all":
  97. return true
  98. case mediaProxyOption != "none" && !urllib.IsHTTPS(mediaURL):
  99. return true
  100. default:
  101. return false
  102. }
  103. }
  104. // ShouldProxifyURLWithMimeType checks if the media URL should be proxified based on the media proxy option, URL scheme, and MIME type.
  105. func ShouldProxifyURLWithMimeType(mediaURL, mediaMimeType, mediaProxyOption string, mediaProxyResourceTypes []string) bool {
  106. if !shouldProxifyURL(mediaURL, mediaProxyOption) {
  107. return false
  108. }
  109. for _, mediaType := range mediaProxyResourceTypes {
  110. if strings.HasPrefix(mediaMimeType, mediaType+"/") {
  111. return true
  112. }
  113. }
  114. return false
  115. }