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.
This commit is contained in:
Tomas Dvorak
2026-05-13 15:03:45 +02:00
parent 25bd96f2bb
commit 28070f8d74
+12 -4
View File
@@ -215,10 +215,13 @@ func sendHandler(w http.ResponseWriter, r *http.Request) {
var data FormData var data FormData
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { 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) http.Error(w, "Invalid JSON", http.StatusBadRequest)
return 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 // Determine SMTP configuration: prefer query params if provided; otherwise use domain map
q := r.URL.Query() q := r.URL.Query()
qEmail := strings.TrimSpace(q.Get("email")) 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) http.Error(w, "Invalid port", http.StatusBadRequest)
return 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} config = EmailConfig{Email: qEmail, Password: qPass, SMTPHost: qServer, SMTPPort: port}
} else { } else {
data.Domain = strings.ToLower(strings.TrimSpace(data.Domain)) data.Domain = strings.ToLower(strings.TrimSpace(data.Domain))
var ok bool var ok bool
config, ok = domainMap[data.Domain] config, ok = domainMap[data.Domain]
if !ok { if !ok {
log.Printf("[send] FAILED unknown domain=%q", data.Domain)
http.Error(w, "Unknown domain", http.StatusUnauthorized) http.Error(w, "Unknown domain", http.StatusUnauthorized)
return return
} }
log.Printf("[send] using domain map host=%s port=%d email=%s", config.SMTPHost, config.SMTPPort, config.Email)
} }
m := gomail.NewMessage() m := gomail.NewMessage()
@@ -261,11 +267,12 @@ func sendHandler(w http.ResponseWriter, r *http.Request) {
d.SSL = true // Using SSL for secure connection d.SSL = true // Using SSL for secure connection
if err := d.DialAndSend(m); err != nil { 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) http.Error(w, "Failed to send email", http.StatusInternalServerError)
return 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")) w.Write([]byte("Email sent successfully"))
} }
@@ -340,11 +347,12 @@ func sendParamsHandler(w http.ResponseWriter, r *http.Request) {
d.SSL = true d.SSL = true
if err := d.DialAndSend(m); err != nil { 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) http.Error(w, "Failed to send email", http.StatusInternalServerError)
return return
} }
log.Printf("[send-params] SUCCESS to=%s subject=%q via %s:%d", to, subject, smtpServer, port)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok", "message": "Email sent successfully"}) _ = 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("/", enableCors(rootHandler))
http.HandleFunc("/send", enableCors(sendHandler)) http.HandleFunc("/send", enableCors(sendHandler))
http.HandleFunc("/send-params", enableCors(sendParamsHandler)) http.HandleFunc("/send-params", enableCors(sendParamsHandler))
log.Println("Server running on :660") log.Println("Server running on localhost:660")
log.Fatal(http.ListenAndServe("0.0.0.0:660", nil)) log.Fatal(http.ListenAndServe("localhost:660", nil))
} }