util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package server
  2. import (
  3. "fmt"
  4. "github.com/emersion/go-smtp"
  5. "heckel.io/ntfy/log"
  6. "heckel.io/ntfy/util"
  7. "net/http"
  8. "net/netip"
  9. "strings"
  10. "unicode/utf8"
  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 logMessagePrefix(v *visitor, m *message) string {
  45. return fmt.Sprintf("%s/%s/%s", v.ip, m.Topic, m.ID)
  46. }
  47. func logHTTPPrefix(v *visitor, r *http.Request) string {
  48. requestURI := r.RequestURI
  49. if requestURI == "" {
  50. requestURI = r.URL.Path
  51. }
  52. return fmt.Sprintf("%s HTTP %s %s", v.ip, r.Method, requestURI)
  53. }
  54. func logSMTPPrefix(state *smtp.ConnectionState) string {
  55. return fmt.Sprintf("%s/%s SMTP", state.Hostname, state.RemoteAddr.String())
  56. }
  57. func renderHTTPRequest(r *http.Request) string {
  58. peekLimit := 4096
  59. lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
  60. for key, values := range r.Header {
  61. for _, value := range values {
  62. lines += fmt.Sprintf("%s: %s\n", key, value)
  63. }
  64. }
  65. lines += "\n"
  66. body, err := util.Peek(r.Body, peekLimit)
  67. if err != nil {
  68. lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
  69. } else if utf8.Valid(body.PeekedBytes) {
  70. lines += string(body.PeekedBytes)
  71. if body.LimitReached {
  72. lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
  73. }
  74. lines += "\n"
  75. } else {
  76. if body.LimitReached {
  77. lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
  78. } else {
  79. lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
  80. }
  81. }
  82. r.Body = body // Important: Reset body, so it can be re-read
  83. return strings.TrimSpace(lines)
  84. }
  85. func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr {
  86. remoteAddr := r.RemoteAddr
  87. addrPort, err := netip.ParseAddrPort(remoteAddr)
  88. ip := addrPort.Addr()
  89. if err != nil {
  90. // This should not happen in real life; only in tests. So, using falling back to 0.0.0.0 if address unspecified
  91. ip, err = netip.ParseAddr(remoteAddr)
  92. if err != nil {
  93. ip = netip.IPv4Unspecified()
  94. if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used
  95. log.Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created %s", remoteAddr, err)
  96. }
  97. }
  98. }
  99. if behindProxy && strings.TrimSpace(r.Header.Get("X-Forwarded-For")) != "" {
  100. // X-Forwarded-For can contain multiple addresses (see #328). If we are behind a proxy,
  101. // only the right-most address can be trusted (as this is the one added by our proxy server).
  102. // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For for details.
  103. ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",")
  104. realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr)))
  105. if err != nil {
  106. log.Error("invalid IP address %s received in X-Forwarded-For header: %s", ip, err.Error())
  107. // Fall back to regular remote address if X-Forwarded-For is damaged
  108. } else {
  109. ip = realIP
  110. }
  111. }
  112. return ip
  113. }