From f88ff9955dc0f1d8c4d28bed575ffea3ffdb0ca1 Mon Sep 17 00:00:00 2001 From: jinronga <52482860+jinronga@users.noreply.github.com> Date: Thu, 16 Jan 2025 10:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20feat(notice):=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=A3=9E=E4=B9=A6=E7=AD=BE=E5=90=8D=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=92=8C=E7=94=9F=E6=88=90=E7=AD=BE=E5=90=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=20(#121)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cairry --- alert/consumer/consumer.go | 1 + internal/models/notice.go | 2 ++ pkg/sender/entry.go | 22 ++++++++++++++++++++- pkg/sender/feishu.go | 39 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/alert/consumer/consumer.go b/alert/consumer/consumer.go index fd12922..ee296f9 100644 --- a/alert/consumer/consumer.go +++ b/alert/consumer/consumer.go @@ -332,6 +332,7 @@ func (ec *Consume) handleAlert(rule models.AlertRule, alerts []models.AlertCurEv Content: content, Event: nil, PhoneNumber: phoneNumber, + Sign: noticeData.Sign, }) if err != nil { logc.Errorf(ec.ctx.Ctx, err.Error()) diff --git a/internal/models/notice.go b/internal/models/notice.go index ed29577..acc344a 100644 --- a/internal/models/notice.go +++ b/internal/models/notice.go @@ -13,6 +13,8 @@ type AlertNotice struct { NoticeTmplId string `json:"noticeTmplId"` Hook string `json:"hook"` Email Email `json:"email" gorm:"email;serializer:json"` + // 签名 + Sign string `json:"sign" gorm:"sign"` PhoneNumber []string `json:"phoneNumber" gorm:"phoneNumber;serializer:json"` } diff --git a/pkg/sender/entry.go b/pkg/sender/entry.go index 9388266..7e1efbc 100644 --- a/pkg/sender/entry.go +++ b/pkg/sender/entry.go @@ -1,11 +1,15 @@ package sender import ( + "encoding/json" "fmt" - "github.com/zeromicro/go-zero/core/logc" "time" + "watchAlert/internal/models" "watchAlert/pkg/ctx" + + log "github.com/sirupsen/logrus" + "github.com/zeromicro/go-zero/core/logc" ) type ( @@ -31,6 +35,8 @@ type ( Event interface{} // 电话号码 PhoneNumber []string + // 签名 + Sign string `json:"sign,omitempty"` } // SendInter 发送通知的接口 @@ -97,3 +103,17 @@ func addRecord(ctx *ctx.Context, sendParams SendParams, status int, msg, errMsg logc.Errorf(ctx.Ctx, fmt.Sprintf("Add notice record failed, err: %s", err.Error())) } } + +// GetSendMsg 发送内容 +func (s *SendParams) GetSendMsg() map[string]any { + msg := make(map[string]any) + if s == nil || s.Content == "" { + return msg + } + err := json.Unmarshal([]byte(s.Content), &msg) + if err != nil { + log.Fatal("解析发送内容失败!", err) + return msg + } + return msg +} diff --git a/pkg/sender/feishu.go b/pkg/sender/feishu.go index dd92ca1..d71526f 100644 --- a/pkg/sender/feishu.go +++ b/pkg/sender/feishu.go @@ -2,8 +2,15 @@ package sender import ( "bytes" + "crypto/hmac" + "crypto/sha256" + "encoding/base64" + "encoding/json" "errors" "fmt" + "strconv" + "time" + "watchAlert/pkg/tools" ) @@ -22,8 +29,22 @@ func NewFeiShuSender() SendInter { } func (f *FeiShuSender) Send(params SendParams) error { - cardContentByte := bytes.NewReader([]byte(params.Content)) - res, err := tools.Post(nil, params.Hook, cardContentByte, 10) + msg := params.GetSendMsg() + if params.Sign != "" { + timestamp := strconv.FormatInt(time.Now().Unix(), 10) + signature, err := generateSignature(params.Sign, timestamp) + if err != nil { + return err + } + msg["sign"] = signature + msg["timestamp"] = timestamp + } + + msgStr, _ := json.Marshal(msg) + + msgByte := bytes.NewReader(msgStr) + + res, err := tools.Post(nil, params.Hook, msgByte, 10) if err != nil { return err } @@ -38,3 +59,17 @@ func (f *FeiShuSender) Send(params SendParams) error { return nil } + +// generateSignature 生成签名 +func generateSignature(secret string, timestamp string) (string, error) { + //timestamp + key 做sha256, 再进行base64 encode + stringToSign := fmt.Sprintf("%v", timestamp) + "\n" + secret + var data []byte + h := hmac.New(sha256.New, []byte(stringToSign)) + _, err := h.Write(data) + if err != nil { + return "", err + } + signature := base64.StdEncoding.EncodeToString(h.Sum(nil)) + return signature, nil +}