Debian Codegraph @

server/log.go

raw
1 package server
2
3 import (
4 "errors"
5 "fmt"
6 "net/http"
7 "strings"
8 "unicode/utf8"
9
10 "github.com/emersion/go-smtp"
11 "github.com/gorilla/websocket"
12 "heckel.io/ntfy/v2/log"
13 "heckel.io/ntfy/v2/model"
14 "heckel.io/ntfy/v2/util"
15 )
16
17 // Log tags
18 const (
19 tagStartup = "startup"
20 tagHTTP = "http"
21 tagPublish = "publish"
22 tagSubscribe = "subscribe"
23 tagFirebase = "firebase"
24 tagSMTP = "smtp" // Receive email
25 tagEmail = "email" // Send email
26 tagTwilio = "twilio"
27 tagStripe = "stripe"
28 tagAccount = "account"
29 tagManager = "manager"
30 tagResetter = "resetter"
31 tagWebsocket = "websocket"
32 tagMatrix = "matrix"
33 tagWebPush = "webpush"
34 )
35
36 var (
37 normalErrorCodes = []int{http.StatusNotFound, http.StatusBadRequest, http.StatusTooManyRequests, http.StatusUnauthorized, http.StatusForbidden, http.StatusInsufficientStorage, http.StatusRequestEntityTooLarge}
38 rateLimitingErrorCodes = []int{http.StatusTooManyRequests, http.StatusRequestEntityTooLarge}
39 )
40
41 // logr creates a new log event with HTTP request fields
42 func logr(r *http.Request) *log.Event {
43 return log.Tag(tagHTTP).Fields(httpContext(r)) // Tag may be overwritten
44 }
45
46 // logv creates a new log event with visitor fields
47 func logv(v *visitor) *log.Event {
48 return log.With(v)
49 }
50
51 // logvr creates a new log event with HTTP request and visitor fields
52 func logvr(v *visitor, r *http.Request) *log.Event {
53 return logr(r).With(v)
54 }
55
56 // logvrm creates a new log event with HTTP request, visitor fields and message fields
57 func logvrm(v *visitor, r *http.Request, m *model.Message) *log.Event {
58 return logvr(v, r).With(m)
59 }
60
61 // logvrm creates a new log event with visitor fields and message fields
62 func logvm(v *visitor, m *model.Message) *log.Event {
63 return logv(v).With(m)
64 }
65
66 // logem creates a new log event with email fields
67 func logem(smtpConn *smtp.Conn) *log.Event {
68 ev := log.Tag(tagSMTP).Field("smtp_hostname", smtpConn.Hostname())
69 if smtpConn.Conn() != nil {
70 ev.Field("smtp_remote_addr", smtpConn.Conn().RemoteAddr().String())
71 }
72 return ev
73 }
74
75 func httpContext(r *http.Request) log.Context {
76 requestURI := r.RequestURI
77 if requestURI == "" {
78 requestURI = r.URL.Path
79 }
80 return log.Context{
81 "http_method": r.Method,
82 "http_path": requestURI,
83 }
84 }
85
86 func websocketErrorContext(err error) log.Context {
87 var c *websocket.CloseError
88 if errors.As(err, &c) {
89 return log.Context{
90 "error": c.Error(),
91 "error_code": c.Code,
92 "error_type": "websocket.CloseError",
93 }
94 }
95 return log.Context{
96 "error": err.Error(),
97 }
98 }
99
100 func renderHTTPRequest(r *http.Request) string {
101 peekLimit := 4096
102 lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
103 for key, values := range r.Header {
104 for _, value := range values {
105 lines += fmt.Sprintf("%s: %s\n", key, value)
106 }
107 }
108 lines += "\n"
109 body, err := util.Peek(r.Body, peekLimit)
110 if err != nil {
111 lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
112 } else if utf8.Valid(body.PeekedBytes) {
113 lines += string(body.PeekedBytes)
114 if body.LimitReached {
115 lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
116 }
117 lines += "\n"
118 } else {
119 if body.LimitReached {
120 lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
121 } else {
122 lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
123 }
124 }
125 r.Body = body // Important: Reset body, so it can be re-read
126 return strings.TrimSpace(lines)
127 }