mirror of
https://github.com/crocofied/CoreControl.git
synced 2025-12-17 15:36:50 +00:00
Added SMTP & Ntfy Notification providers
This commit is contained in:
parent
480a0578a4
commit
c4da6d45f5
@ -3,3 +3,8 @@ module github.com/crocofied/CoreControl
|
||||
go 1.24.3
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
@ -1,2 +1,6 @@
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
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=
|
||||
|
||||
@ -7,9 +7,11 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"gopkg.in/gomail.v2"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
@ -82,9 +84,9 @@ func SendNotification(providerType NotificationType, config json.RawMessage, tes
|
||||
case Telegram:
|
||||
return sendTelegramNotification(config, test, message)
|
||||
case Ntfy:
|
||||
return sendNtfyNotification(config, test)
|
||||
return sendNtfyNotification(config, test, message)
|
||||
case SMTP:
|
||||
return sendSMTPNotification(config, test)
|
||||
return sendSMTPNotification(config, test, message)
|
||||
default:
|
||||
return fmt.Errorf("unknown provider type: %s", providerType)
|
||||
}
|
||||
@ -130,33 +132,74 @@ func sendTelegramNotification(config json.RawMessage, test NotificationTest, mes
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendNtfyNotification(config json.RawMessage, test NotificationTest) error {
|
||||
func sendNtfyNotification(config json.RawMessage, test NotificationTest, message string) error {
|
||||
var cfg struct {
|
||||
Topic string `json:"topic"`
|
||||
Server string `json:"server"`
|
||||
URL string `json:"url"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(config, &cfg); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func sendSMTPNotification(config json.RawMessage, test NotificationTest) error {
|
||||
func sendSMTPNotification(config json.RawMessage, test NotificationTest, message string) error {
|
||||
var cfg struct {
|
||||
Server string `json:"server"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Secure bool `json:"secure"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(config, &cfg); err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user