proxy.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2017 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 ui // import "miniflux.app/ui"
  5. import (
  6. "crypto/hmac"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "errors"
  10. "net/http"
  11. "time"
  12. "miniflux.app/config"
  13. "miniflux.app/crypto"
  14. "miniflux.app/http/request"
  15. "miniflux.app/http/response"
  16. "miniflux.app/http/response/html"
  17. "miniflux.app/logger"
  18. )
  19. func (h *handler) imageProxy(w http.ResponseWriter, r *http.Request) {
  20. // If we receive a "If-None-Match" header, we assume the image is already stored in browser cache.
  21. if r.Header.Get("If-None-Match") != "" {
  22. w.WriteHeader(http.StatusNotModified)
  23. return
  24. }
  25. encodedDigest := request.RouteStringParam(r, "encodedDigest")
  26. encodedURL := request.RouteStringParam(r, "encodedURL")
  27. if encodedURL == "" {
  28. html.BadRequest(w, r, errors.New("No URL provided"))
  29. return
  30. }
  31. decodedDigest, err := base64.URLEncoding.DecodeString(encodedDigest)
  32. if err != nil {
  33. html.BadRequest(w, r, errors.New("Unable to decode this Digest"))
  34. return
  35. }
  36. decodedURL, err := base64.URLEncoding.DecodeString(encodedURL)
  37. if err != nil {
  38. html.BadRequest(w, r, errors.New("Unable to decode this URL"))
  39. return
  40. }
  41. mac := hmac.New(sha256.New, config.Opts.ProxyPrivateKey())
  42. mac.Write(decodedURL)
  43. expectedMAC := mac.Sum(nil)
  44. if !hmac.Equal(decodedDigest, expectedMAC) {
  45. html.Forbidden(w, r)
  46. return
  47. }
  48. imageURL := string(decodedURL)
  49. logger.Debug(`[Proxy] Fetching %q`, imageURL)
  50. req, err := http.NewRequest("GET", imageURL, nil)
  51. if err != nil {
  52. html.ServerError(w, r, err)
  53. return
  54. }
  55. // Note: User-Agent HTTP header is omitted to avoid being blocked by bot protection mechanisms.
  56. req.Header.Add("Connection", "close")
  57. clt := &http.Client{
  58. Timeout: time.Duration(config.Opts.HTTPClientTimeout()) * time.Second,
  59. }
  60. resp, err := clt.Do(req)
  61. if err != nil {
  62. html.ServerError(w, r, err)
  63. return
  64. }
  65. defer resp.Body.Close()
  66. if resp.StatusCode != http.StatusOK {
  67. logger.Error(`[Proxy] Status Code is %d for URL %q`, resp.StatusCode, imageURL)
  68. html.NotFound(w, r)
  69. return
  70. }
  71. etag := crypto.HashFromBytes(decodedURL)
  72. response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
  73. b.WithHeader("Content-Security-Policy", `default-src 'self'`)
  74. b.WithHeader("Content-Type", resp.Header.Get("Content-Type"))
  75. b.WithBody(resp.Body)
  76. b.WithoutCompression()
  77. b.Write()
  78. })
  79. }