media_proxy.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package proxy // import "miniflux.app/v2/internal/proxy"
  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. // ProxyRewriter replaces media URLs with internal proxy URLs.
  14. func ProxyRewriter(router *mux.Router, data string) string {
  15. return genericProxyRewriter(router, ProxifyURL, data)
  16. }
  17. // AbsoluteProxyRewriter do the same as ProxyRewriter except it uses absolute URLs.
  18. func AbsoluteProxyRewriter(router *mux.Router, host, data string) string {
  19. proxifyFunction := func(router *mux.Router, url string) string {
  20. return AbsoluteProxifyURL(router, host, url)
  21. }
  22. return genericProxyRewriter(router, proxifyFunction, data)
  23. }
  24. func genericProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, data string) string {
  25. proxyOption := config.Opts.ProxyOption()
  26. if proxyOption == "none" {
  27. return data
  28. }
  29. doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
  30. if err != nil {
  31. return data
  32. }
  33. for _, mediaType := range config.Opts.ProxyMediaTypes() {
  34. switch mediaType {
  35. case "image":
  36. doc.Find("img").Each(func(i int, img *goquery.Selection) {
  37. if srcAttrValue, ok := img.Attr("src"); ok {
  38. if !isDataURL(srcAttrValue) && (proxyOption == "all" || !urllib.IsHTTPS(srcAttrValue)) {
  39. img.SetAttr("src", proxifyFunction(router, srcAttrValue))
  40. }
  41. }
  42. if srcsetAttrValue, ok := img.Attr("srcset"); ok {
  43. proxifySourceSet(img, router, proxifyFunction, proxyOption, srcsetAttrValue)
  44. }
  45. })
  46. doc.Find("picture source").Each(func(i int, sourceElement *goquery.Selection) {
  47. if srcsetAttrValue, ok := sourceElement.Attr("srcset"); ok {
  48. proxifySourceSet(sourceElement, router, proxifyFunction, proxyOption, srcsetAttrValue)
  49. }
  50. })
  51. case "audio":
  52. doc.Find("audio").Each(func(i int, audio *goquery.Selection) {
  53. if srcAttrValue, ok := audio.Attr("src"); ok {
  54. if !isDataURL(srcAttrValue) && (proxyOption == "all" || !urllib.IsHTTPS(srcAttrValue)) {
  55. audio.SetAttr("src", proxifyFunction(router, srcAttrValue))
  56. }
  57. }
  58. })
  59. doc.Find("audio source").Each(func(i int, sourceElement *goquery.Selection) {
  60. if srcAttrValue, ok := sourceElement.Attr("src"); ok {
  61. if !isDataURL(srcAttrValue) && (proxyOption == "all" || !urllib.IsHTTPS(srcAttrValue)) {
  62. sourceElement.SetAttr("src", proxifyFunction(router, srcAttrValue))
  63. }
  64. }
  65. })
  66. case "video":
  67. doc.Find("video").Each(func(i int, video *goquery.Selection) {
  68. if srcAttrValue, ok := video.Attr("src"); ok {
  69. if !isDataURL(srcAttrValue) && (proxyOption == "all" || !urllib.IsHTTPS(srcAttrValue)) {
  70. video.SetAttr("src", proxifyFunction(router, srcAttrValue))
  71. }
  72. }
  73. })
  74. doc.Find("video source").Each(func(i int, sourceElement *goquery.Selection) {
  75. if srcAttrValue, ok := sourceElement.Attr("src"); ok {
  76. if !isDataURL(srcAttrValue) && (proxyOption == "all" || !urllib.IsHTTPS(srcAttrValue)) {
  77. sourceElement.SetAttr("src", proxifyFunction(router, srcAttrValue))
  78. }
  79. }
  80. })
  81. }
  82. }
  83. output, err := doc.Find("body").First().Html()
  84. if err != nil {
  85. return data
  86. }
  87. return output
  88. }
  89. func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyOption, srcsetAttrValue string) {
  90. imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
  91. for _, imageCandidate := range imageCandidates {
  92. if !isDataURL(imageCandidate.ImageURL) && (proxyOption == "all" || !urllib.IsHTTPS(imageCandidate.ImageURL)) {
  93. imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
  94. }
  95. }
  96. element.SetAttr("srcset", imageCandidates.String())
  97. }
  98. func isDataURL(s string) bool {
  99. return strings.HasPrefix(s, "data:")
  100. }