4
0

rewriter.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "strings"
  6. "miniflux.app/v2/internal/config"
  7. "miniflux.app/v2/internal/reader/sanitizer"
  8. "miniflux.app/v2/internal/urllib"
  9. "github.com/PuerkitoBio/goquery"
  10. "github.com/gorilla/mux"
  11. )
  12. type urlProxyRewriter func(router *mux.Router, url string) string
  13. func RewriteDocumentWithRelativeProxyURL(router *mux.Router, htmlDocument string) string {
  14. return genericProxyRewriter(router, ProxifyRelativeURL, htmlDocument)
  15. }
  16. func RewriteDocumentWithAbsoluteProxyURL(router *mux.Router, host, htmlDocument string) string {
  17. proxifyFunction := func(router *mux.Router, url string) string {
  18. return ProxifyAbsoluteURL(router, host, url)
  19. }
  20. return genericProxyRewriter(router, proxifyFunction, htmlDocument)
  21. }
  22. func genericProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, htmlDocument string) string {
  23. proxyOption := config.Opts.MediaProxyMode()
  24. if proxyOption == "none" {
  25. return htmlDocument
  26. }
  27. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlDocument))
  28. if err != nil {
  29. return htmlDocument
  30. }
  31. for _, mediaType := range config.Opts.MediaProxyResourceTypes() {
  32. switch mediaType {
  33. case "image":
  34. doc.Find("img, picture source").Each(func(i int, img *goquery.Selection) {
  35. if srcAttrValue, ok := img.Attr("src"); ok {
  36. if shouldProxy(srcAttrValue, proxyOption) {
  37. img.SetAttr("src", proxifyFunction(router, srcAttrValue))
  38. }
  39. }
  40. if srcsetAttrValue, ok := img.Attr("srcset"); ok {
  41. proxifySourceSet(img, router, proxifyFunction, proxyOption, srcsetAttrValue)
  42. }
  43. })
  44. doc.Find("video").Each(func(i int, video *goquery.Selection) {
  45. if posterAttrValue, ok := video.Attr("poster"); ok {
  46. if shouldProxy(posterAttrValue, proxyOption) {
  47. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  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 shouldProxy(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 shouldProxy(srcAttrValue, proxyOption) {
  63. video.SetAttr("src", proxifyFunction(router, srcAttrValue))
  64. }
  65. }
  66. if posterAttrValue, ok := video.Attr("poster"); ok {
  67. if shouldProxy(posterAttrValue, proxyOption) {
  68. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  69. }
  70. }
  71. })
  72. }
  73. }
  74. output, err := doc.Find("body").First().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 shouldProxy(imageCandidate.ImageURL, proxyOption) {
  84. imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
  85. }
  86. }
  87. element.SetAttr("srcset", imageCandidates.String())
  88. }
  89. func shouldProxy(attrValue, proxyOption string) bool {
  90. return !strings.HasPrefix(attrValue, "data:") &&
  91. (proxyOption == "all" || !urllib.IsHTTPS(attrValue))
  92. }