image_proxy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2020 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package proxy // import "miniflux.app/proxy"
  5. import (
  6. "strings"
  7. "miniflux.app/config"
  8. "miniflux.app/reader/sanitizer"
  9. "miniflux.app/url"
  10. "github.com/PuerkitoBio/goquery"
  11. "github.com/gorilla/mux"
  12. )
  13. type urlProxyRewriter func(router *mux.Router, url string) string
  14. // ImageProxyRewriter replaces image URLs with internal proxy URLs.
  15. func ImageProxyRewriter(router *mux.Router, data string) string {
  16. return genericImageProxyRewriter(router, ProxifyURL, data)
  17. }
  18. // AbsoluteImageProxyRewriter do the same as ImageProxyRewriter except it uses absolute URLs.
  19. func AbsoluteImageProxyRewriter(router *mux.Router, host, data string) string {
  20. proxifyFunction := func(router *mux.Router, url string) string {
  21. return AbsoluteProxifyURL(router, host, url)
  22. }
  23. return genericImageProxyRewriter(router, proxifyFunction, data)
  24. }
  25. func genericImageProxyRewriter(router *mux.Router, proxifyFunction urlProxyRewriter, data string) string {
  26. proxyImages := config.Opts.ProxyImages()
  27. if proxyImages == "none" {
  28. return data
  29. }
  30. doc, err := goquery.NewDocumentFromReader(strings.NewReader(data))
  31. if err != nil {
  32. return data
  33. }
  34. doc.Find("img").Each(func(i int, img *goquery.Selection) {
  35. if srcAttrValue, ok := img.Attr("src"); ok {
  36. if !isDataURL(srcAttrValue) && (proxyImages == "all" || !url.IsHTTPS(srcAttrValue)) {
  37. img.SetAttr("src", proxifyFunction(router, srcAttrValue))
  38. }
  39. }
  40. if srcsetAttrValue, ok := img.Attr("srcset"); ok {
  41. proxifySourceSet(img, router, proxifyFunction, proxyImages, srcsetAttrValue)
  42. }
  43. })
  44. doc.Find("picture source").Each(func(i int, sourceElement *goquery.Selection) {
  45. if srcsetAttrValue, ok := sourceElement.Attr("srcset"); ok {
  46. proxifySourceSet(sourceElement, router, proxifyFunction, proxyImages, srcsetAttrValue)
  47. }
  48. })
  49. output, err := doc.Find("body").First().Html()
  50. if err != nil {
  51. return data
  52. }
  53. return output
  54. }
  55. func proxifySourceSet(element *goquery.Selection, router *mux.Router, proxifyFunction urlProxyRewriter, proxyImages, srcsetAttrValue string) {
  56. imageCandidates := sanitizer.ParseSrcSetAttribute(srcsetAttrValue)
  57. for _, imageCandidate := range imageCandidates {
  58. if !isDataURL(imageCandidate.ImageURL) && (proxyImages == "all" || !url.IsHTTPS(imageCandidate.ImageURL)) {
  59. imageCandidate.ImageURL = proxifyFunction(router, imageCandidate.ImageURL)
  60. }
  61. }
  62. element.SetAttr("srcset", imageCandidates.String())
  63. }
  64. func isDataURL(s string) bool {
  65. return strings.HasPrefix(s, "data:")
  66. }