package handler import ( "crypto/rand" "encoding/hex" "encoding/json" "fmt" "io" "math/big" "net/http" "strings" "time" "github.com/go-admin-team/go-admin-core/logger" // "github.com/tjfoc/gmsm/sm3" sm3 "go-admin/utils/sm" ) type SingleMsg struct { ID string `json:"id"` MsgContent string `json:"msgcontent"` MsgType string `json:"msgtype"` PhoneNum string `json:"phonenum"` } type SendMsg struct { SubmitTime string `json:"submittime"` MqReceipt bool `json:"mqreceipt"` ExtCode string `json:"extcode"` MsgList []SingleMsg `json:"msgList"` } func SM3(data string) string { // 这里应该实现SM3算法,由于Go标准库中没有直接支持,这里只是返回一个模拟的字符串 h := sm3.New() h.Write([]byte(data)) sum := h.Sum(nil) logger.Infof("digest value is: %x\n", sum) logger.Infof("digest value string: %s\n", hex.EncodeToString(sum)) return hex.EncodeToString(sum) } func (e *EventHandler) SendMsg() *EventHandler { logger.Debugf("In sms Send func, %s", e.RP.AppAlias) //判断该应用是否添加了短信通知 _, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias] if !ok { return e } logger.Debug("has key......", e.JR.AlertStatus) //判断是否需要告警 if !(e.JR.AlertStatus == FIRING) { return e } logger.Info("start to send sms...") appkey := e.SmsInfo.Appkey appsecret := e.SmsInfo.Appsecret headers := make(map[string]string) nonce, err := rand.Int(rand.Reader, big.NewInt(1<<62)) if err != nil { e.Errs["rander error"] = err return e } timestamp := time.Now().UnixNano() / int64(time.Millisecond) headers["x-system-key"] = appkey headers["x-system-nonce"] = nonce.String() headers["x-system-timestamp"] = fmt.Sprintf("%d", timestamp) headers["x-system-token"] = SM3(appkey + fmt.Sprintf("%d", timestamp) + appsecret + nonce.String()) // set smsContent // 所属系统:{{app_name}} // 事件名称:{{events_name}} // 事件创建时间:{{date_time}} // 事件描述:{{events}}` e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{app_name}}", e.RP.AppName, -1) e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events_name}}", e.RP.RuleName, -1) e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{date_time}}", e.RP.CreateTime.Format("2006-01-02 15:04:05"), -1) e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events}}", fmt.Sprintf("%s, %d分钟内发生%s%v次, 当前值为: %v", e.RP.RuleName, e.AC.Interval, e.AC.Condition, e.JR.CompareV, e.JR.CompareV), -1) logger.Info("sms build: ", e.SmsInfo.SmsTpl) MsgList := make([]SingleMsg, 0) phoneList, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias] if ok { for id, phone := range phoneList { ssm := SingleMsg{ ID: fmt.Sprintf("%d", id), MsgContent: e.SmsInfo.SmsTpl, MsgType: "", PhoneNum: phone, } MsgList = append(MsgList, ssm) } } sendMsg := SendMsg{ SubmitTime: time.Now().Format("2006-01-02 15:04:05"), MqReceipt: false, ExtCode: "", MsgList: MsgList, } jsonData, err := json.Marshal(sendMsg) if err != nil { e.Errs["sms json marshal error"] = err return e } logger.Debug("sms json content: ", string(jsonData)) client := &http.Client{} req, err := http.NewRequest("POST", e.SmsInfo.Url, strings.NewReader(string(jsonData))) if err != nil { e.Errs["sms post build error"] = err return e } for key, value := range headers { req.Header.Set(key, value) } // 设定json格式 req.Header.Set("Content-Type", "application/json") logger.Debug("sms json header: ", req.Header) resp, err := client.Do(req) if err != nil { e.Errs["sms post do error"] = err return e } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { e.Errs["sms body read error"] = err return e } logger.Infof("sms response body: %v", string(body)) e.JR.AlertStatus = SENDSUCCESS return e } // smsTpl =` // 【江苏省电力公司】 // 所属系统:{{app_alias}} // 事件名称:{{events_name}} // 事件创建时间:{{date_time}} // 事件描述:{{events}}`