A webmail client. Forked from https://git.sr.ht/~migadu/alps
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

72 lines
1.8 KiB

  1. package alpsviewhtml
  2. import (
  3. "io"
  4. "mime"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "git.sr.ht/~emersion/alps"
  10. alpsbase "git.sr.ht/~emersion/alps/plugins/base"
  11. "github.com/labstack/echo/v4"
  12. )
  13. var (
  14. proxyEnabled = true
  15. proxyMaxSize = 5 * 1024 * 1024 // 5 MiB
  16. )
  17. func init() {
  18. p := alps.GoPlugin{Name: "viewhtml"}
  19. p.Inject("message.html", func(ctx *alps.Context, _data alps.RenderData) error {
  20. data := _data.(*alpsbase.MessageRenderData)
  21. data.Extra["RemoteResourcesAllowed"] = ctx.QueryParam("allow-remote-resources") == "1"
  22. hasRemoteResources := false
  23. if v := ctx.Get("viewhtml.hasRemoteResources"); v != nil {
  24. hasRemoteResources = v.(bool)
  25. }
  26. data.Extra["HasRemoteResources"] = hasRemoteResources
  27. return nil
  28. })
  29. p.GET("/proxy", func(ctx *alps.Context) error {
  30. if !proxyEnabled {
  31. return echo.NewHTTPError(http.StatusForbidden, "proxy disabled")
  32. }
  33. u, err := url.Parse(ctx.QueryParam("src"))
  34. if err != nil {
  35. return echo.NewHTTPError(http.StatusBadRequest, "invalid URL")
  36. }
  37. if u.Scheme != "https" {
  38. return echo.NewHTTPError(http.StatusBadRequest, "invalid scheme")
  39. }
  40. resp, err := http.Get(u.String())
  41. if err != nil {
  42. return err
  43. }
  44. defer resp.Body.Close()
  45. mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
  46. if err != nil || !strings.HasPrefix(mediaType, "image/") {
  47. return echo.NewHTTPError(http.StatusBadRequest, "invalid resource type")
  48. }
  49. size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
  50. if err != nil || size > proxyMaxSize {
  51. return echo.NewHTTPError(http.StatusBadRequest, "invalid resource length")
  52. }
  53. ctx.Response().Header().Set("Content-Length", strconv.Itoa(size))
  54. lr := io.LimitedReader{resp.Body, int64(proxyMaxSize)}
  55. return ctx.Stream(http.StatusOK, mediaType, &lr)
  56. })
  57. alps.RegisterPluginLoader(p.Loader())
  58. }