Add smtp support

Signed-off-by: adrien <adrien@iglou.eu>
This commit is contained in:
adrien 2020-05-10 14:50:02 +02:00
parent 71a31b5d45
commit 22c34f952c
5 changed files with 72 additions and 15 deletions

View File

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

View File

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

20
main.go
View File

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

View File

@ -5,6 +5,11 @@ import (
"errors" "errors"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings"
"time"
mail "github.com/xhit/go-simple-mail"
) )
func HttpReport(m string, u string, d string) { 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)) 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)
}
}
}

View File

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