From 76d46ec64666f2cd52eee84a8e29b943ea13cc76 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Wed, 15 Feb 2023 10:55:01 -0500 Subject: [PATCH] Minor tweaks --- server/config.go | 2 +- server/log.go | 27 +++++++++++++++++++++++---- server/server.go | 28 +++++----------------------- server/server_firebase.go | 10 ++++------ server/smtp_sender.go | 14 +++++++------- server/util.go | 5 ++--- util/util.go | 2 +- web/public/config.js | 4 ++-- 8 files changed, 45 insertions(+), 47 deletions(-) diff --git a/server/config.go b/server/config.go index 811bc95a..04ac00b0 100644 --- a/server/config.go +++ b/server/config.go @@ -61,7 +61,7 @@ var ( // DefaultDisallowedTopics defines the topics that are forbidden, because they are used elsewhere. This array can be // extended using the server.yml config. If updated, also update in Android and web app. - DefaultDisallowedTopics = []string{"docs", "static", "file", "app", "account", "settings", "signup", "login"} + DefaultDisallowedTopics = []string{"docs", "static", "file", "app", "account", "settings", "signup", "login", "v1"} ) // Config is the main config struct for the application. Use New to instantiate a default config struct. diff --git a/server/log.go b/server/log.go index ef2672f9..d385821d 100644 --- a/server/log.go +++ b/server/log.go @@ -11,19 +11,38 @@ import ( "unicode/utf8" ) +// Log tags +const ( + tagStartup = "startup" + tagHTTP = "http" + tagPublish = "publish" + tagSubscribe = "subscribe" + tagFirebase = "firebase" + tagSMTP = "smtp" // Receive email + tagEmail = "email" // Send email + tagFileCache = "file_cache" + tagMessageCache = "message_cache" + tagStripe = "stripe" + tagAccount = "account" + tagManager = "manager" + tagResetter = "resetter" + tagWebsocket = "websocket" + tagMatrix = "matrix" +) + // logr creates a new log event with HTTP request fields func logr(r *http.Request) *log.Event { - return log.Fields(httpContext(r)) + return log.Tag(tagHTTP).Fields(httpContext(r)) // Tag may be overwritten } -// logr creates a new log event with visitor fields +// logv creates a new log event with visitor fields func logv(v *visitor) *log.Event { return log.With(v) } -// logr creates a new log event with HTTP request and visitor fields +// logvr creates a new log event with HTTP request and visitor fields func logvr(v *visitor, r *http.Request) *log.Event { - return logv(v).Fields(httpContext(r)) + return logr(r).With(v) } // logvrm creates a new log event with HTTP request, visitor fields and message fields diff --git a/server/server.go b/server/server.go index 4334800e..11e38cd4 100644 --- a/server/server.go +++ b/server/server.go @@ -121,24 +121,6 @@ const ( wsPongWait = 15 * time.Second ) -// Log tags -const ( - tagStartup = "startup" - tagPublish = "publish" - tagSubscribe = "subscribe" - tagFirebase = "firebase" - tagSMTP = "smtp" // Receive email - tagEmail = "email" // Send email - tagFileCache = "file_cache" - tagMessageCache = "message_cache" - tagStripe = "stripe" - tagAccount = "account" - tagManager = "manager" - tagResetter = "resetter" - tagWebsocket = "websocket" - tagMatrix = "matrix" -) - // New instantiates a new Server. It creates the cache and adds a Firebase // subscriber (if configured). func New(conf *Config) (*Server, error) { @@ -314,11 +296,11 @@ func (s *Server) handle(w http.ResponseWriter, r *http.Request) { s.handleError(w, r, v, err) return } - - if logvr(v, r).IsTrace() { - logvr(v, r).Field("http_request", renderHTTPRequest(r)).Trace("HTTP request started") - } else if log.IsDebug() { - logvr(v, r).Debug("HTTP request started") + ev := logvr(v, r) + if ev.IsTrace() { + ev.Field("http_request", renderHTTPRequest(r)).Trace("HTTP request started") + } else if logvr(v, r).IsDebug() { + ev.Debug("HTTP request started") } logvr(v, r). Timing(func() { diff --git a/server/server_firebase.go b/server/server_firebase.go index 749901e5..6318b98e 100644 --- a/server/server_firebase.go +++ b/server/server_firebase.go @@ -8,7 +8,6 @@ import ( "firebase.google.com/go/v4/messaging" "fmt" "google.golang.org/api/option" - "heckel.io/ntfy/log" "heckel.io/ntfy/user" "heckel.io/ntfy/util" "strings" @@ -46,16 +45,15 @@ func (c *firebaseClient) Send(v *visitor, m *message) error { if err != nil { return err } - if log.Tag(tagFirebase).IsTrace() { - logvm(v, m). - Tag(tagFirebase). - Field("firebase_message", util.MaybeMarshalJSON(fbm)). - Trace("Firebase message") + ev := logvm(v, m).Tag(tagFirebase) + if ev.IsTrace() { + ev.Field("firebase_message", util.MaybeMarshalJSON(fbm)).Trace("Firebase message") } err = c.sender.Send(fbm) if err == errFirebaseQuotaExceeded { logvm(v, m). Tag(tagFirebase). + Err(err). Warn("Firebase quota exceeded (likely for topic), temporarily denying Firebase access to visitor") v.FirebaseTemporarilyDeny() } diff --git a/server/smtp_sender.go b/server/smtp_sender.go index d8114ad0..ee263658 100644 --- a/server/smtp_sender.go +++ b/server/smtp_sender.go @@ -37,18 +37,18 @@ func (s *smtpSender) Send(v *visitor, m *message, to string) error { return err } auth := smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host) - logvm(v, m). + ev := logvm(v, m). Tag(tagEmail). Fields(log.Context{ "email_via": s.config.SMTPSenderAddr, "email_user": s.config.SMTPSenderUser, "email_to": to, - }). - Debug("Sending email") - logvm(v, m). - Tag(tagEmail). - Field("email_body", message). - Trace("Email body") + }) + if ev.IsTrace() { + ev.Field("email_body", message).Trace("Sending email") + } else if ev.IsDebug() { + ev.Debug("Sending email") + } return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message)) }) } diff --git a/server/util.go b/server/util.go index 8fbfaefa..1141e5d5 100644 --- a/server/util.go +++ b/server/util.go @@ -1,7 +1,6 @@ package server import ( - "heckel.io/ntfy/log" "heckel.io/ntfy/util" "io" "net/http" @@ -55,7 +54,7 @@ func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr { if err != nil { ip = netip.IPv4Unspecified() if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used - log.Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created %s", remoteAddr, err) + logr(r).Err(err).Warn("unable to parse IP (%s), new visitor with unspecified IP (0.0.0.0) created", remoteAddr) } } } @@ -66,7 +65,7 @@ func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr { ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",") realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr))) if err != nil { - log.Error("invalid IP address %s received in X-Forwarded-For header: %s", ip, err.Error()) + logr(r).Err(err).Error("invalid IP address %s received in X-Forwarded-For header", ip) // Fall back to regular remote address if X-Forwarded-For is damaged } else { ip = realIP diff --git a/util/util.go b/util/util.go index bc093b06..33fa34ee 100644 --- a/util/util.go +++ b/util/util.go @@ -379,7 +379,7 @@ func String(v string) *string { return &v } -// Int turns a string into a pointer of an int +// Int turns an int into a pointer of an int func Int(v int) *int { return &v } diff --git a/web/public/config.js b/web/public/config.js index 8d1ed4cd..e714a4fe 100644 --- a/web/public/config.js +++ b/web/public/config.js @@ -6,11 +6,11 @@ // During web development, you may change values here for rapid testing. var config = { - base_url: "https://127.0.0.1", // window.location.origin FIXME update before merging + base_url: window.location.origin, // Set this to "https://127.0.0.1" to test against a different server app_root: "/app", enable_login: true, enable_signup: true, enable_payments: true, enable_reservations: true, - disallowed_topics: ["docs", "static", "file", "app", "account", "settings", "signup", "login"] + disallowed_topics: ["docs", "static", "file", "app", "account", "settings", "signup", "login", "v1"] };