2024-10-09 22:31:52 +02:00
|
|
|
|
package utils
|
|
|
|
|
|
|
2024-12-06 03:25:38 +01:00
|
|
|
|
/*
|
|
|
|
|
|
MIT License
|
|
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2023 Jonas Kaninda
|
|
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
|
SOFTWARE.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2024-12-06 14:21:55 +01:00
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/go-mail/mail"
|
|
|
|
|
|
"github.com/jkaninda/mysql-bkup/pkg/logger"
|
|
|
|
|
|
"html/template"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2024-10-09 22:31:52 +02:00
|
|
|
|
func parseTemplate[T any](data T, fileName string) (string, error) {
|
|
|
|
|
|
// Open the file
|
|
|
|
|
|
tmpl, err := template.ParseFiles(filepath.Join(templatePath, fileName))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
if err = tmpl.Execute(&buf, data); err != nil {
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 04:14:42 +02:00
|
|
|
|
func SendEmail(subject, body string) error {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Info("Start sending email notification....")
|
2024-10-09 22:31:52 +02:00
|
|
|
|
config := loadMailConfig()
|
|
|
|
|
|
emails := strings.Split(config.MailTo, ",")
|
|
|
|
|
|
m := mail.NewMessage()
|
|
|
|
|
|
m.SetHeader("From", config.MailFrom)
|
|
|
|
|
|
m.SetHeader("To", emails...)
|
|
|
|
|
|
m.SetHeader("Subject", subject)
|
|
|
|
|
|
m.SetBody("text/html", body)
|
|
|
|
|
|
d := mail.NewDialer(config.MailHost, config.MailPort, config.MailUserName, config.MailPassword)
|
|
|
|
|
|
d.TLSConfig = &tls.Config{InsecureSkipVerify: config.SkipTls}
|
|
|
|
|
|
|
|
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Error could not send email : %v", err)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
return err
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Info("Email notification has been sent")
|
2024-10-10 04:14:42 +02:00
|
|
|
|
return nil
|
2024-10-09 22:31:52 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-10 04:14:42 +02:00
|
|
|
|
func sendMessage(msg string) error {
|
2024-10-09 22:31:52 +02:00
|
|
|
|
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Info("Sending Telegram notification... ")
|
2024-10-09 22:31:52 +02:00
|
|
|
|
chatId := os.Getenv("TG_CHAT_ID")
|
|
|
|
|
|
body, _ := json.Marshal(map[string]string{
|
|
|
|
|
|
"chat_id": chatId,
|
|
|
|
|
|
"text": msg,
|
|
|
|
|
|
})
|
|
|
|
|
|
url := fmt.Sprintf("%s/sendMessage", getTgUrl())
|
|
|
|
|
|
// Create an HTTP post request
|
|
|
|
|
|
request, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
request.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
|
|
response, err := client.Do(request)
|
|
|
|
|
|
if err != nil {
|
2024-10-10 04:14:42 +02:00
|
|
|
|
return err
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
code := response.StatusCode
|
|
|
|
|
|
if code == 200 {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Info("Telegram notification has been sent")
|
2024-10-10 04:14:42 +02:00
|
|
|
|
return nil
|
2024-10-09 22:31:52 +02:00
|
|
|
|
} else {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
body, _ := io.ReadAll(response.Body)
|
|
|
|
|
|
logger.Error("Error could not send message, error: %s", string(body))
|
2024-10-10 04:14:42 +02:00
|
|
|
|
return fmt.Errorf("error could not send message %s", string(body))
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
func NotifySuccess(notificationData *NotificationData) {
|
2024-10-10 04:14:42 +02:00
|
|
|
|
notificationData.BackupReference = backupReference()
|
2024-10-09 22:31:52 +02:00
|
|
|
|
var vars = []string{
|
|
|
|
|
|
"TG_TOKEN",
|
|
|
|
|
|
"TG_CHAT_ID",
|
|
|
|
|
|
}
|
|
|
|
|
|
var mailVars = []string{
|
|
|
|
|
|
"MAIL_HOST",
|
|
|
|
|
|
"MAIL_PORT",
|
|
|
|
|
|
"MAIL_USERNAME",
|
|
|
|
|
|
"MAIL_PASSWORD",
|
|
|
|
|
|
"MAIL_FROM",
|
|
|
|
|
|
"MAIL_TO",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 14:21:55 +01:00
|
|
|
|
// Email notification
|
2024-10-09 22:31:52 +02:00
|
|
|
|
err := CheckEnvVars(mailVars)
|
|
|
|
|
|
if err == nil {
|
2024-10-22 17:22:45 +02:00
|
|
|
|
body, err := parseTemplate(*notificationData, "email.tmpl")
|
2024-10-09 22:31:52 +02:00
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not parse email template: %v", err)
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
2024-10-10 04:14:42 +02:00
|
|
|
|
err = SendEmail(fmt.Sprintf("✅ Database Backup Notification – %s", notificationData.Database), body)
|
|
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not send email: %v", err)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
}
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
2024-12-06 14:21:55 +01:00
|
|
|
|
// Telegram notification
|
2024-10-09 22:31:52 +02:00
|
|
|
|
err = CheckEnvVars(vars)
|
|
|
|
|
|
if err == nil {
|
2024-10-22 17:22:45 +02:00
|
|
|
|
message, err := parseTemplate(*notificationData, "telegram.tmpl")
|
2024-10-09 22:31:52 +02:00
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not parse telegram template: %v", err)
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 04:14:42 +02:00
|
|
|
|
err = sendMessage(message)
|
|
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not send Telegram message: %v", err)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
}
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
func NotifyError(error string) {
|
|
|
|
|
|
var vars = []string{
|
|
|
|
|
|
"TG_TOKEN",
|
|
|
|
|
|
"TG_CHAT_ID",
|
|
|
|
|
|
}
|
|
|
|
|
|
var mailVars = []string{
|
|
|
|
|
|
"MAIL_HOST",
|
|
|
|
|
|
"MAIL_PORT",
|
|
|
|
|
|
"MAIL_USERNAME",
|
|
|
|
|
|
"MAIL_PASSWORD",
|
|
|
|
|
|
"MAIL_FROM",
|
|
|
|
|
|
"MAIL_TO",
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-06 14:21:55 +01:00
|
|
|
|
// Email notification
|
2024-10-09 22:31:52 +02:00
|
|
|
|
err := CheckEnvVars(mailVars)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
body, err := parseTemplate(ErrorMessage{
|
2024-10-10 04:14:42 +02:00
|
|
|
|
Error: error,
|
|
|
|
|
|
EndTime: time.Now().Format(TimeFormat()),
|
|
|
|
|
|
BackupReference: os.Getenv("BACKUP_REFERENCE"),
|
2024-10-22 17:22:45 +02:00
|
|
|
|
}, "email-error.tmpl")
|
2024-10-09 22:31:52 +02:00
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not parse error template: %v", err)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
}
|
2024-12-06 14:21:55 +01:00
|
|
|
|
err = SendEmail("🔴 Urgent: Database Backup Failure Notification", body)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not send email: %v", err)
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-06 14:21:55 +01:00
|
|
|
|
// Telegram notification
|
2024-10-09 22:31:52 +02:00
|
|
|
|
err = CheckEnvVars(vars)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
message, err := parseTemplate(ErrorMessage{
|
2024-10-10 04:14:42 +02:00
|
|
|
|
Error: error,
|
|
|
|
|
|
EndTime: time.Now().Format(TimeFormat()),
|
|
|
|
|
|
BackupReference: os.Getenv("BACKUP_REFERENCE"),
|
2024-10-22 17:22:45 +02:00
|
|
|
|
}, "telegram-error.tmpl")
|
2024-10-09 22:31:52 +02:00
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not parse error template: %v", err)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-10 04:14:42 +02:00
|
|
|
|
err = sendMessage(message)
|
|
|
|
|
|
if err != nil {
|
2024-12-06 14:21:55 +01:00
|
|
|
|
logger.Error("Could not send telegram message: %v", err)
|
2024-10-10 04:14:42 +02:00
|
|
|
|
}
|
2024-10-09 22:31:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getTgUrl() string {
|
|
|
|
|
|
return fmt.Sprintf("https://api.telegram.org/bot%s", os.Getenv("TG_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
}
|