From 28070f8d749ed22fc94be8f4d2dc58a738fa21dc Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Wed, 13 May 2026 15:03:45 +0200 Subject: [PATCH] feat(main): enhance logging and restrict server binding Add detailed structured logging to `sendHandler` and `sendParamsHandler` to track request details, domain lookups, and SMTP outcomes (success/failure). Update the server to bind to `localhost` instead of `0.0.0.0` for improved security. --- main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 365c5eb..3870d16 100644 --- a/main.go +++ b/main.go @@ -215,10 +215,13 @@ func sendHandler(w http.ResponseWriter, r *http.Request) { var data FormData if err := json.NewDecoder(r.Body).Decode(&data); err != nil { + log.Printf("[send] Invalid JSON from %s", r.RemoteAddr) http.Error(w, "Invalid JSON", http.StatusBadRequest) return } + log.Printf("[send] request from=%s domain=%q subject=%q", data.Email, data.Domain, data.Subject) + // Determine SMTP configuration: prefer query params if provided; otherwise use domain map q := r.URL.Query() qEmail := strings.TrimSpace(q.Get("email")) @@ -237,15 +240,18 @@ func sendHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Invalid port", http.StatusBadRequest) return } + log.Printf("[send] using query params host=%s port=%d email=%s", qServer, port, qEmail) config = EmailConfig{Email: qEmail, Password: qPass, SMTPHost: qServer, SMTPPort: port} } else { data.Domain = strings.ToLower(strings.TrimSpace(data.Domain)) var ok bool config, ok = domainMap[data.Domain] if !ok { + log.Printf("[send] FAILED unknown domain=%q", data.Domain) http.Error(w, "Unknown domain", http.StatusUnauthorized) return } + log.Printf("[send] using domain map host=%s port=%d email=%s", config.SMTPHost, config.SMTPPort, config.Email) } m := gomail.NewMessage() @@ -261,11 +267,12 @@ func sendHandler(w http.ResponseWriter, r *http.Request) { d.SSL = true // Using SSL for secure connection if err := d.DialAndSend(m); err != nil { - log.Println("Email send error:", err) + log.Printf("[send] FAILED to=%s subject=%q via %s:%d error=%v", config.Email, data.Subject, config.SMTPHost, config.SMTPPort, err) http.Error(w, "Failed to send email", http.StatusInternalServerError) return } + log.Printf("[send] SUCCESS to=%s subject=%q via %s:%d", config.Email, data.Subject, config.SMTPHost, config.SMTPPort) w.Write([]byte("Email sent successfully")) } @@ -340,11 +347,12 @@ func sendParamsHandler(w http.ResponseWriter, r *http.Request) { d.SSL = true if err := d.DialAndSend(m); err != nil { - log.Println("Email send error:", err) + log.Printf("[send-params] FAILED to=%s subject=%q via %s:%d error=%v", to, subject, smtpServer, port, err) http.Error(w, "Failed to send email", http.StatusInternalServerError) return } + log.Printf("[send-params] SUCCESS to=%s subject=%q via %s:%d", to, subject, smtpServer, port) w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(map[string]string{"status": "ok", "message": "Email sent successfully"}) } @@ -355,6 +363,6 @@ func main() { http.HandleFunc("/", enableCors(rootHandler)) http.HandleFunc("/send", enableCors(sendHandler)) http.HandleFunc("/send-params", enableCors(sendParamsHandler)) - log.Println("Server running on :660") - log.Fatal(http.ListenAndServe("0.0.0.0:660", nil)) + log.Println("Server running on localhost:660") + log.Fatal(http.ListenAndServe("localhost:660", nil)) }