media_proxy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, picture source").Each(func(i int, img *goquery.Selection) {
  37. if srcAttrValue, ok := img.Attr("src"); ok {
  38. if shouldProxy(srcAttrValue, proxyOption) {
  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("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. case "audio":
  54. doc.Find("audio, audio source").Each(func(i int, audio *goquery.Selection) {
  55. if srcAttrValue, ok := audio.Attr("src"); ok {
  56. if shouldProxy(srcAttrValue, proxyOption) {
  57. audio.SetAttr("src", proxifyFunction(router, srcAttrValue))
  58. }
  59. }
  60. })
  61. case "video":
  62. doc.Find("video, video source").Each(func(i int, video *goquery.Selection) {
  63. if srcAttrValue, ok := video.Attr("src"); ok {
  64. if shouldProxy(srcAttrValue, proxyOption) {
  65. video.SetAttr("src", proxifyFunction(router, srcAttrValue))
  66. }
  67. }
  68. if posterAttrValue, ok := video.Attr("poster"); ok {
  69. if shouldProxy(posterAttrValue, proxyOption) {
  70. video.SetAttr("poster", proxifyFunction(router, posterAttrValue))
  71. }
  72. }
  73. })
  74. }
  75. }
  76. output, err := doc.Find("body").First().Html()
  77. if err != nil {
  78. return data
  79. }
  80. return output
  81. }
  82. func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyOption, srcsetAttrValue string) {
  83. imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
  84. for _, imageCandidate := range imageCandidates {
  85. if shouldProxy(imageCandidate.ImageURL, proxyOption) {
  86. imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
  87. }
  88. }
  89. element.SetAttr("srcset", imageCandidates.String())
  90. }
  91. func shouldProxy(attrValue, proxyOption string) bool {
  92. return !strings.HasPrefix(attrValue, "data:") &&
  93. (proxyOption == "all" || !urllib.IsHTTPS(attrValue))
  94. }