rewriter.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, host, htmlDocument string) string {
  18. proxifyFunction := func(router *mux.Router, url string) string {
  19. return ProxifyAbsoluteURL(router, host, url)
  20. }
  21. return genericProxyRewriter(router, proxifyFunction, htmlDocument)
  22. }
  23. func genericProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, htmlDocument string) string {
  24. proxyOption := config.Opts.MediaProxyMode()
  25. if proxyOption == "none" {
  26. return htmlDocument
  27. }
  28. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDocument))
  29. if err != nil {
  30. return htmlDocument
  31. }
  32. for _, mediaType := range config.Opts.MediaProxyResourceTypes() {
  33. switch mediaType {
  34. case "image":
  35. doc.Find("img, picture source").Each(func(i int, img *goquery.Selection) {
  36. if srcAttrValue, ok := img.Attr("src"); ok {
  37. if shouldProxy(srcAttrValue, proxyOption) {
  38. img.SetAttr("src", proxifyFunction(router, srcAttrValue))
  39. }
  40. }
  41. if srcsetAttrValue, ok := img.Attr("srcset"); ok {
  42. proxifySourceSet(img, router, proxifyFunction, proxyOption, srcsetAttrValue)
  43. }
  44. })
  45. if !slices.Contains(config.Opts.MediaProxyResourceTypes(), "video") {
  46. doc.Find("video").Each(func(i int, video *goquery.Selection) {
  47. if posterAttrValue, ok := video.Attr("poster"); ok {
  48. if shouldProxy(posterAttrValue, proxyOption) {
  49. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  50. }
  51. }
  52. })
  53. }
  54. case "audio":
  55. doc.Find("audio, audio source").Each(func(i int, audio *goquery.Selection) {
  56. if srcAttrValue, ok := audio.Attr("src"); ok {
  57. if shouldProxy(srcAttrValue, proxyOption) {
  58. audio.SetAttr("src", proxifyFunction(router, srcAttrValue))
  59. }
  60. }
  61. })
  62. case "video":
  63. doc.Find("video, video source").Each(func(i int, video *goquery.Selection) {
  64. if srcAttrValue, ok := video.Attr("src"); ok {
  65. if shouldProxy(srcAttrValue, proxyOption) {
  66. video.SetAttr("src", proxifyFunction(router, srcAttrValue))
  67. }
  68. }
  69. if posterAttrValue, ok := video.Attr("poster"); ok {
  70. if shouldProxy(posterAttrValue, proxyOption) {
  71. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  72. }
  73. }
  74. })
  75. }
  76. }
  77. output, err := doc.Find("body").First().Html()
  78. if err != nil {
  79. return htmlDocument
  80. }
  81. return output
  82. }
  83. func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyOption, srcsetAttrValue string) {
  84. imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
  85. for _, imageCandidate := range imageCandidates {
  86. if shouldProxy(imageCandidate.ImageURL, proxyOption) {
  87. imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
  88. }
  89. }
  90. element.SetAttr("srcset", imageCandidates.String())
  91. }
  92. func shouldProxy(attrValue, proxyOption string) bool {
  93. return !strings.HasPrefix(attrValue, "data:") &&
  94. (proxyOption == "all" || !urllib.IsHTTPS(attrValue))
  95. }