Skip to content

Commit

Permalink
🚀 feat(notice): 增加飞书签名字段和生成签名逻辑 (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Cairry <[email protected]>
  • Loading branch information
jinronga and Cairry authored Jan 16, 2025
1 parent 9e91022 commit f88ff99
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions alert/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions internal/models/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
22 changes: 21 additions & 1 deletion pkg/sender/entry.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -31,6 +35,8 @@ type (
Event interface{}
// 电话号码
PhoneNumber []string
// 签名
Sign string `json:"sign,omitempty"`
}

// SendInter 发送通知的接口
Expand Down Expand Up @@ -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
}
39 changes: 37 additions & 2 deletions pkg/sender/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ package sender

import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strconv"
"time"

"watchAlert/pkg/tools"
)

Expand All @@ -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
}
Expand All @@ -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
}

0 comments on commit f88ff99

Please sign in to comment.