Minor tweaks

This commit is contained in:
binwiederhier 2023-02-15 10:55:01 -05:00
parent e90f52f375
commit 76d46ec646
8 changed files with 45 additions and 47 deletions

View file

@ -61,7 +61,7 @@ var (
// DefaultDisallowedTopics defines the topics that are forbidden, because they are used elsewhere. This array can be // 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. // 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. // Config is the main config struct for the application. Use New to instantiate a default config struct.

View file

@ -11,19 +11,38 @@ import (
"unicode/utf8" "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 // logr creates a new log event with HTTP request fields
func logr(r *http.Request) *log.Event { 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 { func logv(v *visitor) *log.Event {
return log.With(v) 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 { 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 // logvrm creates a new log event with HTTP request, visitor fields and message fields

View file

@ -121,24 +121,6 @@ const (
wsPongWait = 15 * time.Second 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 // New instantiates a new Server. It creates the cache and adds a Firebase
// subscriber (if configured). // subscriber (if configured).
func New(conf *Config) (*Server, error) { 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) s.handleError(w, r, v, err)
return return
} }
ev := logvr(v, r)
if logvr(v, r).IsTrace() { if ev.IsTrace() {
logvr(v, r).Field("http_request", renderHTTPRequest(r)).Trace("HTTP request started") ev.Field("http_request", renderHTTPRequest(r)).Trace("HTTP request started")
} else if log.IsDebug() { } else if logvr(v, r).IsDebug() {
logvr(v, r).Debug("HTTP request started") ev.Debug("HTTP request started")
} }
logvr(v, r). logvr(v, r).
Timing(func() { Timing(func() {

View file

@ -8,7 +8,6 @@ import (
"firebase.google.com/go/v4/messaging" "firebase.google.com/go/v4/messaging"
"fmt" "fmt"
"google.golang.org/api/option" "google.golang.org/api/option"
"heckel.io/ntfy/log"
"heckel.io/ntfy/user" "heckel.io/ntfy/user"
"heckel.io/ntfy/util" "heckel.io/ntfy/util"
"strings" "strings"
@ -46,16 +45,15 @@ func (c *firebaseClient) Send(v *visitor, m *message) error {
if err != nil { if err != nil {
return err return err
} }
if log.Tag(tagFirebase).IsTrace() { ev := logvm(v, m).Tag(tagFirebase)
logvm(v, m). if ev.IsTrace() {
Tag(tagFirebase). ev.Field("firebase_message", util.MaybeMarshalJSON(fbm)).Trace("Firebase message")
Field("firebase_message", util.MaybeMarshalJSON(fbm)).
Trace("Firebase message")
} }
err = c.sender.Send(fbm) err = c.sender.Send(fbm)
if err == errFirebaseQuotaExceeded { if err == errFirebaseQuotaExceeded {
logvm(v, m). logvm(v, m).
Tag(tagFirebase). Tag(tagFirebase).
Err(err).
Warn("Firebase quota exceeded (likely for topic), temporarily denying Firebase access to visitor") Warn("Firebase quota exceeded (likely for topic), temporarily denying Firebase access to visitor")
v.FirebaseTemporarilyDeny() v.FirebaseTemporarilyDeny()
} }

View file

@ -37,18 +37,18 @@ func (s *smtpSender) Send(v *visitor, m *message, to string) error {
return err return err
} }
auth := smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host) auth := smtp.PlainAuth("", s.config.SMTPSenderUser, s.config.SMTPSenderPass, host)
logvm(v, m). ev := logvm(v, m).
Tag(tagEmail). Tag(tagEmail).
Fields(log.Context{ Fields(log.Context{
"email_via": s.config.SMTPSenderAddr, "email_via": s.config.SMTPSenderAddr,
"email_user": s.config.SMTPSenderUser, "email_user": s.config.SMTPSenderUser,
"email_to": to, "email_to": to,
}). })
Debug("Sending email") if ev.IsTrace() {
logvm(v, m). ev.Field("email_body", message).Trace("Sending email")
Tag(tagEmail). } else if ev.IsDebug() {
Field("email_body", message). ev.Debug("Sending email")
Trace("Email body") }
return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message)) return smtp.SendMail(s.config.SMTPSenderAddr, auth, s.config.SMTPSenderFrom, []string{to}, []byte(message))
}) })
} }

View file

@ -1,7 +1,6 @@
package server package server
import ( import (
"heckel.io/ntfy/log"
"heckel.io/ntfy/util" "heckel.io/ntfy/util"
"io" "io"
"net/http" "net/http"
@ -55,7 +54,7 @@ func extractIPAddress(r *http.Request, behindProxy bool) netip.Addr {
if err != nil { if err != nil {
ip = netip.IPv4Unspecified() ip = netip.IPv4Unspecified()
if remoteAddr != "@" || !behindProxy { // RemoteAddr is @ when unix socket is used 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"), ",") ips := util.SplitNoEmpty(r.Header.Get("X-Forwarded-For"), ",")
realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr))) realIP, err := netip.ParseAddr(strings.TrimSpace(util.LastString(ips, remoteAddr)))
if err != nil { 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 // Fall back to regular remote address if X-Forwarded-For is damaged
} else { } else {
ip = realIP ip = realIP

View file

@ -379,7 +379,7 @@ func String(v string) *string {
return &v 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 { func Int(v int) *int {
return &v return &v
} }

View file

@ -6,11 +6,11 @@
// During web development, you may change values here for rapid testing. // During web development, you may change values here for rapid testing.
var config = { 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", app_root: "/app",
enable_login: true, enable_login: true,
enable_signup: true, enable_signup: true,
enable_payments: true, enable_payments: true,
enable_reservations: 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"]
}; };