ntfy/server/log.go

126 lines
3.5 KiB
Go
Raw Normal View History

2023-02-05 05:26:40 +03:00
package server
import (
"fmt"
"github.com/emersion/go-smtp"
2023-02-07 00:01:32 +03:00
"github.com/gorilla/websocket"
2023-11-17 04:54:58 +03:00
"heckel.io/ntfy/v2/log"
"heckel.io/ntfy/v2/util"
2023-02-05 05:26:40 +03:00
"net/http"
"strings"
"unicode/utf8"
)
2023-02-15 18:55:01 +03:00
// Log tags
const (
tagStartup = "startup"
tagHTTP = "http"
tagPublish = "publish"
tagSubscribe = "subscribe"
tagFirebase = "firebase"
tagSMTP = "smtp" // Receive email
tagEmail = "email" // Send email
2023-05-05 23:22:54 +03:00
tagTwilio = "twilio"
2023-02-15 18:55:01 +03:00
tagFileCache = "file_cache"
tagMessageCache = "message_cache"
tagStripe = "stripe"
tagAccount = "account"
tagManager = "manager"
tagResetter = "resetter"
tagWebsocket = "websocket"
tagMatrix = "matrix"
2023-06-08 19:20:12 +03:00
tagWebPush = "webpush"
2023-02-15 18:55:01 +03:00
)
2023-02-25 23:31:12 +03:00
var (
2023-03-03 04:25:13 +03:00
normalErrorCodes = []int{http.StatusNotFound, http.StatusBadRequest, http.StatusTooManyRequests, http.StatusUnauthorized, http.StatusForbidden, http.StatusInsufficientStorage}
2023-02-28 05:13:15 +03:00
rateLimitingErrorCodes = []int{http.StatusTooManyRequests, http.StatusRequestEntityTooLarge}
2023-02-25 23:31:12 +03:00
)
2023-02-06 07:34:27 +03:00
// logr creates a new log event with HTTP request fields
2023-02-05 05:26:40 +03:00
func logr(r *http.Request) *log.Event {
2023-02-15 18:55:01 +03:00
return log.Tag(tagHTTP).Fields(httpContext(r)) // Tag may be overwritten
2023-02-05 05:26:40 +03:00
}
2023-02-15 18:55:01 +03:00
// logv creates a new log event with visitor fields
2023-02-05 05:26:40 +03:00
func logv(v *visitor) *log.Event {
2023-02-06 07:34:27 +03:00
return log.With(v)
2023-02-05 05:26:40 +03:00
}
2023-02-15 18:55:01 +03:00
// logvr creates a new log event with HTTP request and visitor fields
2023-02-05 05:26:40 +03:00
func logvr(v *visitor, r *http.Request) *log.Event {
2023-02-15 18:55:01 +03:00
return logr(r).With(v)
2023-02-05 05:26:40 +03:00
}
2023-02-06 07:34:27 +03:00
// logvrm creates a new log event with HTTP request, visitor fields and message fields
2023-02-05 05:26:40 +03:00
func logvrm(v *visitor, r *http.Request, m *message) *log.Event {
2023-02-06 07:34:27 +03:00
return logvr(v, r).With(m)
2023-02-05 05:26:40 +03:00
}
2023-02-06 07:34:27 +03:00
// logvrm creates a new log event with visitor fields and message fields
2023-02-05 05:26:40 +03:00
func logvm(v *visitor, m *message) *log.Event {
2023-02-06 07:34:27 +03:00
return logv(v).With(m)
2023-02-05 05:26:40 +03:00
}
2023-02-06 07:34:27 +03:00
// logem creates a new log event with email fields
2023-02-15 05:22:46 +03:00
func logem(smtpConn *smtp.Conn) *log.Event {
ev := log.Tag(tagSMTP).Field("smtp_hostname", smtpConn.Hostname())
if smtpConn.Conn() != nil {
ev.Field("smtp_remote_addr", smtpConn.Conn().RemoteAddr().String())
}
return ev
2023-02-05 05:26:40 +03:00
}
2023-02-07 00:01:32 +03:00
func httpContext(r *http.Request) log.Context {
2023-02-05 05:26:40 +03:00
requestURI := r.RequestURI
if requestURI == "" {
requestURI = r.URL.Path
}
2023-02-07 00:01:32 +03:00
return log.Context{
2023-02-05 05:26:40 +03:00
"http_method": r.Method,
"http_path": requestURI,
}
}
2023-02-07 00:01:32 +03:00
func websocketErrorContext(err error) log.Context {
if c, ok := err.(*websocket.CloseError); ok {
return log.Context{
"error": c.Error(),
"error_code": c.Code,
"error_type": "websocket.CloseError",
}
}
return log.Context{
"error": err.Error(),
}
}
2023-02-05 05:26:40 +03:00
func renderHTTPRequest(r *http.Request) string {
peekLimit := 4096
lines := fmt.Sprintf("%s %s %s\n", r.Method, r.URL.RequestURI(), r.Proto)
for key, values := range r.Header {
for _, value := range values {
lines += fmt.Sprintf("%s: %s\n", key, value)
}
}
lines += "\n"
body, err := util.Peek(r.Body, peekLimit)
if err != nil {
lines = fmt.Sprintf("(could not read body: %s)\n", err.Error())
} else if utf8.Valid(body.PeekedBytes) {
lines += string(body.PeekedBytes)
if body.LimitReached {
lines += fmt.Sprintf(" ... (peeked %d bytes)", peekLimit)
}
lines += "\n"
} else {
if body.LimitReached {
lines += fmt.Sprintf("(peeked bytes not UTF-8, peek limit of %d bytes reached, hex: %x ...)\n", peekLimit, body.PeekedBytes)
} else {
lines += fmt.Sprintf("(peeked bytes not UTF-8, %d bytes, hex: %x)\n", len(body.PeekedBytes), body.PeekedBytes)
}
}
r.Body = body // Important: Reset body, so it can be re-read
return strings.TrimSpace(lines)
}