util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package server
  2. import (
  3. "context"
  4. "fmt"
  5. "heckel.io/ntfy/util"
  6. "io"
  7. "net/http"
  8. "net/netip"
  9. "strings"
  10. )
  11. func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
  12. value := strings.ToLower(readParam(r, names...))
  13. if value == "" {
  14. return defaultValue
  15. }
  16. return value == "1" || value == "yes" || value == "true"
  17. }
  18. func readCommaSeparatedParam(r *http.Request, names ...string) (params []string) {
  19. paramStr := readParam(r, names...)
  20. if paramStr != "" {
  21. params = make([]string, 0)
  22. for _, s := range util.SplitNoEmpty(paramStr, ",") {
  23. params = append(params, strings.TrimSpace(s))
  24. }
  25. }
  26. return params
  27. }
  28. func readParam(r *http.Request, names ...string) string {
  29. value := readHeaderParam(r, names...)
  30. if value != "" {
  31. return value
  32. }
  33. return readQueryParam(r, names...)
  34. }
  35. func readHeaderParam(r *http.Request, names ...string) string {
  36. for _, name := range names {
  37. value := r.Header.Get(name)
  38. if value != "" {
  39. return strings.TrimSpace(value)
  40. }
  41. }
  42. return ""
  43. }
  44. func readQueryParam(r *http.Request, names ...string) string {
  45. for _, name := range names {
  46. value := r.URL.Query().Get(strings.ToLower(name))
  47. if value != "" {
  48. return strings.TrimSpace(value)
  49. }
  50. }
  51. return ""
  52. }
  53. func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr {
  54. remoteAddr := r.RemoteAddr
  55. addrPort, err := netip.ParseAddrPort(remoteAddr)
  56. ip := addrPort.Addr()
  57. if err != nil {
  58. // This should not happen in real life; only in tests. So, using falling back to 0.0.0.0 if address unspecified
  59. ip, err = netip.ParseAddr(remoteAddr)
  60. if err != nil {
  61. ip = netip.IPv4Unspecified()
  62. if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used
  63. logr(r).Err(err).Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created", remoteAddr)
  64. }
  65. }
  66. }
  67. if behindProxy && strings.TrimSpace(r.Header.Get("X-Forwarded-For")) != "" {
  68. // X-Forwarded-For can contain multiple addresses (see #328). If we are behind a proxy,
  69. // only the right-most address can be trusted (as this is the one added by our proxy server).
  70. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For for details.
  71. ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",")
  72. realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr)))
  73. if err != nil {
  74. logr(r).Err(err).Error("invalid IP address %s received in X-Forwarded-For header", ip)
  75. // Fall back to regular remote address if X-Forwarded-For is damaged
  76. } else {
  77. ip = realIP
  78. }
  79. }
  80. return ip
  81. }
  82. func readJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
  83. obj, err := util.UnmarshalJSONWithLimit[T](r, limit, allowEmpty)
  84. if err == util.ErrUnmarshalJSON {
  85. return nil, errHTTPBadRequestJSONInvalid
  86. } else if err == util.ErrTooLargeJSON {
  87. return nil, errHTTPEntityTooLargeJSONBody
  88. } else if err != nil {
  89. return nil, err
  90. }
  91. return obj, nil
  92. }
  93. func withContext(r *http.Request, ctx map[contextKey]any) *http.Request {
  94. c := r.Context()
  95. for k, v := range ctx {
  96. c = context.WithValue(c, k, v)
  97. }
  98. return r.WithContext(c)
  99. }
  100. func fromContext[T any](r *http.Request, key contextKey) T {
  101. t, ok := r.Context().Value(key).(T)
  102. if !ok {
  103. panic(fmt.Sprintf("cannot find key %v in request context", key))
  104. }
  105. return t
  106. }