Added SMTP & Ntfy Notification providers

This commit is contained in:
headlesdev 2025-05-28 12:26:59 +02:00
parent 480a0578a4
commit c4da6d45f5
3 changed files with 61 additions and 9 deletions

View File

@ -3,3 +3,8 @@ module github.com/crocofied/CoreControl
go 1.24.3 go 1.24.3
require github.com/lib/pq v1.10.9 require github.com/lib/pq v1.10.9
require (
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
)

View File

@ -1,2 +1,6 @@
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=

View File

@ -7,9 +7,11 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"gopkg.in/gomail.v2"
) )
var db *sql.DB var db *sql.DB
@ -82,9 +84,9 @@ func SendNotification(providerType NotificationType, config json.RawMessage, tes
case Telegram: case Telegram:
return sendTelegramNotification(config, test, message) return sendTelegramNotification(config, test, message)
case Ntfy: case Ntfy:
return sendNtfyNotification(config, test) return sendNtfyNotification(config, test, message)
case SMTP: case SMTP:
return sendSMTPNotification(config, test) return sendSMTPNotification(config, test, message)
default: default:
return fmt.Errorf("unknown provider type: %s", providerType) return fmt.Errorf("unknown provider type: %s", providerType)
} }
@ -130,33 +132,74 @@ func sendTelegramNotification(config json.RawMessage, test NotificationTest, mes
return nil return nil
} }
func sendNtfyNotification(config json.RawMessage, test NotificationTest) error { func sendNtfyNotification(config json.RawMessage, test NotificationTest, message string) error {
var cfg struct { var cfg struct {
Topic string `json:"topic"` URL string `json:"url"`
Server string `json:"server"` Token string `json:"token"`
} }
if err := json.Unmarshal(config, &cfg); err != nil { if err := json.Unmarshal(config, &cfg); err != nil {
return fmt.Errorf("invalid NTFY config: %w", err) return fmt.Errorf("invalid NTFY config: %w", err)
} }
log.Printf("Sending NTFY test to topic %s on %s", cfg.Topic, cfg.Server) baseURL := strings.TrimSuffix(cfg.URL, "/")
req, err := http.NewRequest("POST", baseURL, strings.NewReader(message))
if err != nil {
return fmt.Errorf("failed to create NTFY request: %w", err)
}
if cfg.Token != "" {
req.Header.Set("Authorization", "Bearer "+cfg.Token)
}
req.Header.Set("Content-Type", "text/plain")
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to send NTFY request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("NTFY API returned error status: %d", resp.StatusCode)
}
log.Printf("Successfully sent NTFY notification to %s", baseURL)
return nil return nil
} }
func sendSMTPNotification(config json.RawMessage, test NotificationTest) error { func sendSMTPNotification(config json.RawMessage, test NotificationTest, message string) error {
var cfg struct { var cfg struct {
Server string `json:"server"` Host string `json:"host"`
Port int `json:"port"` Port int `json:"port"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"`
From string `json:"from"`
To string `json:"to"` To string `json:"to"`
Secure bool `json:"secure"`
} }
if err := json.Unmarshal(config, &cfg); err != nil { if err := json.Unmarshal(config, &cfg); err != nil {
return fmt.Errorf("invalid SMTP config: %w", err) return fmt.Errorf("invalid SMTP config: %w", err)
} }
log.Printf("Sending SMTP test to %s via %s:%d", cfg.To, cfg.Server, cfg.Port) d := gomail.NewDialer(cfg.Host, cfg.Port, cfg.Username, cfg.Password)
if cfg.Secure {
d.SSL = true
}
m := gomail.NewMessage()
m.SetHeader("From", cfg.From)
m.SetHeader("To", cfg.To)
m.SetHeader("Subject", "Test Notification from CoreControl")
m.SetBody("text/plain", message)
if err := d.DialAndSend(m); err != nil {
return fmt.Errorf("failed to send email: %w", err)
}
log.Printf("Successfully sent SMTP notification to %s", cfg.To)
return nil return nil
} }