util.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package server
  2. import (
  3. "bufio"
  4. "heckel.io/ntfy/util"
  5. "io"
  6. "net"
  7. "net/http"
  8. "net/netip"
  9. "strings"
  10. "sync"
  11. )
  12. func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
  13. value := strings.ToLower(readParam(r, names...))
  14. if value == "" {
  15. return defaultValue
  16. }
  17. return value == "1" || value == "yes" || value == "true"
  18. }
  19. func readParam(r *http.Request, names ...string) string {
  20. value := readHeaderParam(r, names...)
  21. if value != "" {
  22. return value
  23. }
  24. return readQueryParam(r, names...)
  25. }
  26. func readHeaderParam(r *http.Request, names ...string) string {
  27. for _, name := range names {
  28. value := r.Header.Get(name)
  29. if value != "" {
  30. return strings.TrimSpace(value)
  31. }
  32. }
  33. return ""
  34. }
  35. func readQueryParam(r *http.Request, names ...string) string {
  36. for _, name := range names {
  37. value := r.URL.Query().Get(strings.ToLower(name))
  38. if value != "" {
  39. return strings.TrimSpace(value)
  40. }
  41. }
  42. return ""
  43. }
  44. func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr {
  45. remoteAddr := r.RemoteAddr
  46. addrPort, err := netip.ParseAddrPort(remoteAddr)
  47. ip := addrPort.Addr()
  48. if err != nil {
  49. // This should not happen in real life; only in tests. So, using falling back to 0.0.0.0 if address unspecified
  50. ip, err = netip.ParseAddr(remoteAddr)
  51. if err != nil {
  52. ip = netip.IPv4Unspecified()
  53. if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used
  54. logr(r).Err(err).Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created", remoteAddr)
  55. }
  56. }
  57. }
  58. if behindProxy && strings.TrimSpace(r.Header.Get("X-Forwarded-For")) != "" {
  59. // X-Forwarded-For can contain multiple addresses (see #328). If we are behind a proxy,
  60. // only the right-most address can be trusted (as this is the one added by our proxy server).
  61. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For for details.
  62. ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",")
  63. realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr)))
  64. if err != nil {
  65. logr(r).Err(err).Error("invalid IP address %s received in X-Forwarded-For header", ip)
  66. // Fall back to regular remote address if X-Forwarded-For is damaged
  67. } else {
  68. ip = realIP
  69. }
  70. }
  71. return ip
  72. }
  73. func readJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
  74. obj, err := util.UnmarshalJSONWithLimit[T](r, limit, allowEmpty)
  75. if err == util.ErrUnmarshalJSON {
  76. return nil, errHTTPBadRequestJSONInvalid
  77. } else if err == util.ErrTooLargeJSON {
  78. return nil, errHTTPEntityTooLargeJSONBody
  79. } else if err != nil {
  80. return nil, err
  81. }
  82. return obj, nil
  83. }
  84. type httpResponseWriter struct {
  85. w http.ResponseWriter
  86. headerWritten bool
  87. mu sync.Mutex
  88. }
  89. type httpResponseWriterWithHijacker struct {
  90. httpResponseWriter
  91. }
  92. var _ http.ResponseWriter = (*httpResponseWriter)(nil)
  93. var _ http.Flusher = (*httpResponseWriter)(nil)
  94. var _ http.Hijacker = (*httpResponseWriterWithHijacker)(nil)
  95. func newHTTPResponseWriter(w http.ResponseWriter) http.ResponseWriter {
  96. if _, ok := w.(http.Hijacker); ok {
  97. return &httpResponseWriterWithHijacker{httpResponseWriter: httpResponseWriter{w: w}}
  98. }
  99. return &httpResponseWriter{w: w}
  100. }
  101. func (w *httpResponseWriter) Header() http.Header {
  102. return w.w.Header()
  103. }
  104. func (w *httpResponseWriter) Write(bytes []byte) (int, error) {
  105. w.mu.Lock()
  106. w.headerWritten = true
  107. w.mu.Unlock()
  108. return w.w.Write(bytes)
  109. }
  110. func (w *httpResponseWriter) WriteHeader(statusCode int) {
  111. w.mu.Lock()
  112. if w.headerWritten {
  113. w.mu.Unlock()
  114. return
  115. }
  116. w.headerWritten = true
  117. w.mu.Unlock()
  118. w.w.WriteHeader(statusCode)
  119. }
  120. func (w *httpResponseWriter) Flush() {
  121. if f, ok := w.w.(http.Flusher); ok {
  122. f.Flush()
  123. }
  124. }
  125. func (w *httpResponseWriterWithHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
  126. h, _ := w.w.(http.Hijacker)
  127. return h.Hijack()
  128. }