log.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package server
  2. import (
  3. "fmt"
  4. "github.com/emersion/go-smtp"
  5. "golang.org/x/time/rate"
  6. "heckel.io/ntfy/log"
  7. "heckel.io/ntfy/util"
  8. "net/http"
  9. "strings"
  10. "unicode/utf8"
  11. )
  12. // logr creates a new log event with HTTP request fields
  13. func logr(r *http.Request) *log.Event {
  14. return log.Fields(httpFields(r))
  15. }
  16. // logr creates a new log event with visitor fields
  17. func logv(v *visitor) *log.Event {
  18. return log.With(v)
  19. }
  20. // logr creates a new log event with HTTP request and visitor fields
  21. func logvr(v *visitor, r *http.Request) *log.Event {
  22. return logv(v).Fields(httpFields(r))
  23. }
  24. // logvrm creates a new log event with HTTP request, visitor fields and message fields
  25. func logvrm(v *visitor, r *http.Request, m *message) *log.Event {
  26. return logvr(v, r).With(m)
  27. }
  28. // logvrm creates a new log event with visitor fields and message fields
  29. func logvm(v *visitor, m *message) *log.Event {
  30. return logv(v).With(m)
  31. }
  32. // logem creates a new log event with email fields
  33. func logem(state *smtp.ConnectionState) *log.Event {
  34. return log.
  35. Tag(tagSMTP).
  36. Fields(log.Context{
  37. "smtp_hostname": state.Hostname,
  38. "smtp_remote_addr": state.RemoteAddr.String(),
  39. })
  40. }
  41. func httpFields(r *http.Request) map[string]any {
  42. requestURI := r.RequestURI
  43. if requestURI == "" {
  44. requestURI = r.URL.Path
  45. }
  46. return map[string]any{
  47. "http_method": r.Method,
  48. "http_path": requestURI,
  49. }
  50. }
  51. func requestLimiterFields(limiter *rate.Limiter) map[string]any {
  52. return map[string]any{
  53. "visitor_request_limiter_limit": limiter.Limit(),
  54. "visitor_request_limiter_tokens": limiter.Tokens(),
  55. }
  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. }