log.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package server
  2. import (
  3. "fmt"
  4. "github.com/emersion/go-smtp"
  5. "github.com/gorilla/websocket"
  6. "golang.org/x/time/rate"
  7. "heckel.io/ntfy/log"
  8. "heckel.io/ntfy/util"
  9. "net/http"
  10. "strings"
  11. "unicode/utf8"
  12. )
  13. // logr creates a new log event with HTTP request fields
  14. func logr(r *http.Request) *log.Event {
  15. return log.Fields(httpContext(r))
  16. }
  17. // logr creates a new log event with visitor fields
  18. func logv(v *visitor) *log.Event {
  19. return log.With(v)
  20. }
  21. // logr creates a new log event with HTTP request and visitor fields
  22. func logvr(v *visitor, r *http.Request) *log.Event {
  23. return logv(v).
  24. Fields(httpContext(r)).
  25. Fields(requestLimiterFields(v.RequestLimiter()))
  26. }
  27. // logvrm creates a new log event with HTTP request, visitor fields and message fields
  28. func logvrm(v *visitor, r *http.Request, m *message) *log.Event {
  29. return logvr(v, r).With(m)
  30. }
  31. // logvrm creates a new log event with visitor fields and message fields
  32. func logvm(v *visitor, m *message) *log.Event {
  33. return logv(v).With(m)
  34. }
  35. // logem creates a new log event with email fields
  36. func logem(state *smtp.ConnectionState) *log.Event {
  37. return log.
  38. Tag(tagSMTP).
  39. Fields(log.Context{
  40. "smtp_hostname": state.Hostname,
  41. "smtp_remote_addr": state.RemoteAddr.String(),
  42. })
  43. }
  44. func httpContext(r *http.Request) log.Context {
  45. requestURI := r.RequestURI
  46. if requestURI == "" {
  47. requestURI = r.URL.Path
  48. }
  49. return log.Context{
  50. "http_method": r.Method,
  51. "http_path": requestURI,
  52. }
  53. }
  54. func websocketErrorContext(err error) log.Context {
  55. if c, ok := err.(*websocket.CloseError); ok {
  56. return log.Context{
  57. "error": c.Error(),
  58. "error_code": c.Code,
  59. "error_type": "websocket.CloseError",
  60. }
  61. }
  62. return log.Context{
  63. "error": err.Error(),
  64. }
  65. }
  66. func requestLimiterFields(limiter *rate.Limiter) map[string]any {
  67. return map[string]any{
  68. "visitor_request_limiter_limit": limiter.Limit(),
  69. "visitor_request_limiter_tokens": limiter.Tokens(),
  70. }
  71. }
  72. func renderHTTPRequest(r *http.Request) string {
  73. peekLimit := 4096
  74. lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
  75. for key, values := range r.Header {
  76. for _, value := range values {
  77. lines += fmt.Sprintf("%s: %s\n", key, value)
  78. }
  79. }
  80. lines += "\n"
  81. body, err := util.Peek(r.Body, peekLimit)
  82. if err != nil {
  83. lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
  84. } else if utf8.Valid(body.PeekedBytes) {
  85. lines += string(body.PeekedBytes)
  86. if body.LimitReached {
  87. lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
  88. }
  89. lines += "\n"
  90. } else {
  91. if body.LimitReached {
  92. lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
  93. } else {
  94. lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
  95. }
  96. }
  97. r.Body = body // Important: Reset body, so it can be re-read
  98. return strings.TrimSpace(lines)
  99. }