mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
规则链
This commit is contained in:
@@ -1,18 +1,28 @@
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/smtp"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"strings"
|
||||
|
||||
"github.com/jordan-wright/email"
|
||||
)
|
||||
|
||||
type externalSendEmailNode struct {
|
||||
bareNode
|
||||
From string `json:"from" yaml:"from"`
|
||||
To string `json:"to" yaml:"to"`
|
||||
Cc string `json:"cc" yaml:"cc"`
|
||||
Bcc string `json:"bcc" yaml:"bcc"`
|
||||
Subject string `json:"subject" yaml:"subject"`
|
||||
Body string `json:"body" yaml:"body"`
|
||||
Host string `json:"host"` // 服务器地址
|
||||
Port int `json:"port"` // 服务器端口
|
||||
From string `json:"from"` // 邮箱账号
|
||||
Nickname string `json:"nickname"` // 发件人
|
||||
Secret string `json:"secret"` // 邮箱密码
|
||||
IsSSL bool `json:"isSsl"` // 是否开启ssl
|
||||
|
||||
To string `json:"to"` //收件人
|
||||
Subject string `json:"subject"` //主题
|
||||
Body string `json:"body"` //内容
|
||||
}
|
||||
|
||||
type externalSendEmailNodeFactory struct{}
|
||||
@@ -21,10 +31,14 @@ func (f externalSendEmailNodeFactory) Name() string { return "SendEmailNode"
|
||||
func (f externalSendEmailNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
|
||||
func (f externalSendEmailNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
|
||||
func (f externalSendEmailNodeFactory) Create(id string, meta Metadata) (Node, error) {
|
||||
labels := []string{"Success", "Failure"}
|
||||
|
||||
node := &externalSendEmailNode{
|
||||
bareNode: newBareNode(f.Name(), id, meta, labels),
|
||||
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
|
||||
/*Host: email.Host,
|
||||
Port: email.Port,
|
||||
Nickname: email.Nickname,
|
||||
Secret: email.Secret,
|
||||
IsSSL: email.IsSSL,*/
|
||||
}
|
||||
return decodePath(meta, node)
|
||||
}
|
||||
@@ -33,19 +47,48 @@ func (n *externalSendEmailNode) Handle(msg message.Message) error {
|
||||
logrus.Infof("%s handle message '%s'", n.Name(), msg.GetType())
|
||||
|
||||
successLabelNode := n.GetLinkedNode("Success")
|
||||
//failureLabelNode := n.GetLinkedNode("Failure")
|
||||
failureLabelNode := n.GetLinkedNode("Failure")
|
||||
|
||||
/*dialer := runtime.NewDialer(runtime.EMAIL)
|
||||
variables := map[string]string{
|
||||
"from": n.From,
|
||||
"to": n.To,
|
||||
"cc": n.Cc,
|
||||
"bcc": n.Bcc,
|
||||
"subject": n.Subject,
|
||||
"body": n.Body,
|
||||
tos := strings.Split(n.To, ",")
|
||||
if tos[len(tos)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除
|
||||
tos = tos[:len(tos)-1]
|
||||
}
|
||||
if err := dialer.DialAndSend(msg.GetMetadata(), variables); err != nil {
|
||||
return failureLabelNode.Handle(msg)
|
||||
}*/
|
||||
return successLabelNode.Handle(msg)
|
||||
err := n.send(tos, msg)
|
||||
if err != nil {
|
||||
if failureLabelNode != nil {
|
||||
return failureLabelNode.Handle(msg)
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if successLabelNode != nil {
|
||||
return successLabelNode.Handle(msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *externalSendEmailNode) send(to []string, msg message.Message) error {
|
||||
|
||||
auth := smtp.PlainAuth("", m.From, m.Secret, m.Host)
|
||||
e := email.NewEmail()
|
||||
if m.Nickname != "" {
|
||||
e.From = fmt.Sprintf("%s <%s>", m.Nickname, m.From)
|
||||
} else {
|
||||
e.From = m.From
|
||||
}
|
||||
e.To = to
|
||||
e.Subject = m.Subject
|
||||
template, err := ParseTemplate(m.Body, msg.GetMetadata().GetValues())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.HTML = []byte(template)
|
||||
hostAddr := fmt.Sprintf("%s:%d", m.Host, m.Port)
|
||||
if m.IsSSL {
|
||||
err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: m.Host})
|
||||
} else {
|
||||
err = e.Send(hostAddr, auth)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user