rewriter.go 4.1 KB

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