Add smtp support

Signed-off-by: adrien <adrien@iglou.eu>
pull/3/head
adrien 2 years ago
parent 71a31b5d45
commit 22c34f952c
  1. 1
      config/struct_error.go
  2. 11
      examples/etc/xavier-srv/errors.toml
  3. 20
      main.go
  4. 49
      tools/reporting.go
  5. 6
      tools/various.go

@ -5,6 +5,7 @@ type ErrorList struct {
Team map[string]struct {
Report []struct {
Process string `toml:"process"`
Encrypt string `toml:"encrypt"`
Host string `toml:"host,omitempty"`
From string `toml:"from,omitempty"`
User string `toml:"user,omitempty"`

@ -1,14 +1,15 @@
[team.IglouEu]
[[team.IglouEu.report]]
process = 'smtp'
host = 'mail.gandi.net:465'
encrypt = 'tls'
host = 'mail.gandi.net:587'
from = 'Professeur Xavier <pxavier@iglou.eu>'
user = ''
passwd = ''
recipients = ['a@a.a', 'b@b.b']
subject = '🤯 [WRNING][[%TEAM]] Error on webapp'
recipients = ['garbage@yopmail.com', 'garbage2@yopmail.com']
subject = '🤯 [WARNING][[%TEAM]] Error on webapp'
body = '''
Some WebAPP from '[%TEAM]' have failed.
Some WebAPP from team '[%TEAM]' have failed.
[%ERRORS]
'''
@ -24,7 +25,7 @@ Some WebAPP from '[%TEAM]' have failed.
methods = 'GET'
url = 'https://smsapi.free-mobile.fr/sendmsg?user=&pass=&msg='
body = '''
Some WebAPP from '[%TEAM]' have failed.
Some WebAPP from team '[%TEAM]' have failed.
[%ERRORS]
'''

@ -78,9 +78,17 @@ func xTeamReport(team string, failures [][]string) (bool, error) {
case "smtp":
subject := strings.ReplaceAll(reportProcess.Subject, "[%TEAM]", team)
message := strings.ReplaceAll(reportProcess.Body, "[%TEAM]", team)
message = strings.ReplaceAll(reportProcess.Body, "[%ERRORS]", team)
_ = subject + message
//report.byMail(host, from, user, passwd, subject, message string, recip []string)
message = strings.ReplaceAll(message, "[%ERRORS]", reportMessage)
tools.SmtpReport(
reportProcess.Host,
reportProcess.Encrypt,
reportProcess.From,
reportProcess.User,
reportProcess.Passwd,
reportProcess.Recipients,
subject, message,
)
case "http":
message := strings.ReplaceAll(reportProcess.Body, "[%TEAM]", team)
@ -137,9 +145,9 @@ func xTeamReport(team string, failures [][]string) (bool, error) {
}
func cerebro(url string, need int) (bool, error) {
xPsyAgatha, errAgatha := tools.HttpStatus(url, need)
xPsyArthur, errArthur := tools.HttpStatus(url, need)
xPsyDash, errDash := tools.HttpStatus(url, need)
xPsyAgatha, errAgatha := tools.HttpStatus(url, need, config.Global.HTTP.MaxWait)
xPsyArthur, errArthur := tools.HttpStatus(url, need, config.Global.HTTP.MaxWait)
xPsyDash, errDash := tools.HttpStatus(url, need, config.Global.HTTP.MaxWait)
if (xPsyAgatha && xPsyArthur) ||
(xPsyAgatha && xPsyDash) ||

@ -5,6 +5,11 @@ import (
"errors"
"net/http"
"net/url"
"strconv"
"strings"
"time"
mail "github.com/xhit/go-simple-mail"
)
func HttpReport(m string, u string, d string) {
@ -25,3 +30,47 @@ func HttpReport(m string, u string, d string) {
PushToLog(3, errors.New("Request to `"+u+url.QueryEscape(d)+"` has failed: "+res.Status))
}
}
func SmtpReport(host string, encrypt string, from string, user string, passwd string, recip []string, subject string, message string) {
ht := strings.Split(host, ":")
pt, _ := strconv.Atoi(ht[1])
server := mail.NewSMTPClient()
//SMTP Server
server.Host = ht[0]
server.Port = pt
server.Username = user
server.Password = passwd
server.Authentication = mail.AuthPlain
server.KeepAlive = true
server.ConnectTimeout = 10 * time.Second
server.SendTimeout = 10 * time.Second
smtpClient, err := server.Connect()
switch encrypt {
case "tls":
server.Encryption = mail.EncryptionTLS
case "ssl":
server.Encryption = mail.EncryptionSSL
default:
server.Encryption = mail.EncryptionNone
}
if err != nil {
PushToLog(3, err)
}
for _, to := range recip {
email := mail.NewMSG()
email.SetFrom(from).
AddTo(to).
SetSubject(subject)
email.SetBody(mail.TextPlain, message)
if err = email.Send(smtpClient); err != nil {
PushToLog(3, err)
}
}
}

@ -7,8 +7,6 @@ import (
"strconv"
"strings"
"time"
"git.iglou.eu/xavierSrv/config"
)
func JsonEscapeString(d string) string {
@ -25,8 +23,8 @@ func JsonEscapeString(d string) string {
return r.Replace(d)
}
func HttpStatus(url string, need int) (bool, error) {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(config.Global.HTTP.MaxWait)))
func HttpStatus(url string, need int, mwait int) (bool, error) {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(mwait)))
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {

Loading…
Cancel
Save