log.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package server
  2. import (
  3. "fmt"
  4. "github.com/emersion/go-smtp"
  5. "github.com/gorilla/websocket"
  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(httpContext(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(httpContext(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 httpContext(r *http.Request) log.Context {
  42. requestURI := r.RequestURI
  43. if requestURI == "" {
  44. requestURI = r.URL.Path
  45. }
  46. return log.Context{
  47. "http_method": r.Method,
  48. "http_path": requestURI,
  49. }
  50. }
  51. func websocketErrorContext(err error) log.Context {
  52. if c, ok := err.(*websocket.CloseError); ok {
  53. return log.Context{
  54. "error": c.Error(),
  55. "error_code": c.Code,
  56. "error_type": "websocket.CloseError",
  57. }
  58. }
  59. return log.Context{
  60. "error": err.Error(),
  61. }
  62. }
  63. func renderHTTPRequest(r *http.Request) string {
  64. peekLimit := 4096
  65. lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
  66. for key, values := range r.Header {
  67. for _, value := range values {
  68. lines += fmt.Sprintf("%s: %s\n", key, value)
  69. }
  70. }
  71. lines += "\n"
  72. body, err := util.Peek(r.Body, peekLimit)
  73. if err != nil {
  74. lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
  75. } else if utf8.Valid(body.PeekedBytes) {
  76. lines += string(body.PeekedBytes)
  77. if body.LimitReached {
  78. lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
  79. }
  80. lines += "\n"
  81. } else {
  82. if body.LimitReached {
  83. lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
  84. } else {
  85. lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
  86. }
  87. }
  88. r.Body = body // Important: Reset body, so it can be re-read
  89. return strings.TrimSpace(lines)
  90. }