From e9690a5b5074c1cf8a5d4f115594d1b00e59f556 Mon Sep 17 00:00:00 2001 From: naiba Date: Sun, 3 Jan 2021 21:11:00 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(notification):=20close=20#47?= =?UTF-8?q?=20query=20escape=20&&=20json=20encode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/notification.go | 30 +++++++++++++++++++++------- service/alertmanager/alertmanager.go | 2 -- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/model/notification.go b/model/notification.go index 907cdd3..ac9318a 100644 --- a/model/notification.go +++ b/model/notification.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "log" "net/http" "net/url" "strings" @@ -34,7 +35,9 @@ type Notification struct { } func (n *Notification) reqURL(message string) string { - return replaceParamsInString(n.URL, message) + return replaceParamsInString(n.URL, message, func(msg string) string { + return url.QueryEscape(msg) + }) } func (n *Notification) reqBody(message string) (string, error) { @@ -43,7 +46,10 @@ func (n *Notification) reqBody(message string) (string, error) { } switch n.RequestType { case NotificationRequestTypeJSON: - return replaceParamsInString(n.RequestBody, message), nil + return replaceParamsInString(n.RequestBody, message, func(msg string) string { + msgBytes, _ := json.Marshal(msg) + return string(msgBytes)[1 : len(msgBytes)-1] + }), nil case NotificationRequestTypeForm: var data map[string]string if err := json.Unmarshal([]byte(n.RequestBody), &data); err != nil { @@ -51,7 +57,9 @@ func (n *Notification) reqBody(message string) (string, error) { } params := url.Values{} for k, v := range data { - params.Add(k, replaceParamsInString(v, message)) + params.Add(k, replaceParamsInString(v, message, func(msg string) string { + return url.QueryEscape(msg) + })) } return params.Encode(), nil } @@ -64,9 +72,8 @@ func (n *Notification) reqContentType() string { } if n.RequestType == NotificationRequestTypeForm { return "application/x-www-form-urlencoded" - } else { - return "application/json" } + return "application/json" } func (n *Notification) Send(message string) error { @@ -97,10 +104,19 @@ func (n *Notification) Send(message string) error { err = fmt.Errorf("%d %s", resp.StatusCode, resp.Status) } + // defer resp.Body.Close() + // body, _ := ioutil.ReadAll(resp.Body) + + log.Printf("%s 通知:%s %s %+v\n", n.Name, message, reqBody, err) + return err } -func replaceParamsInString(str string, message string) string { - str = strings.ReplaceAll(str, "#NEZHA#", message) +func replaceParamsInString(str string, message string, mod func(string) string) string { + if mod != nil { + str = strings.ReplaceAll(str, "#NEZHA#", mod(message)) + } else { + str = strings.ReplaceAll(str, "#NEZHA#", message) + } return str } diff --git a/service/alertmanager/alertmanager.go b/service/alertmanager/alertmanager.go index 0765de2..27d68c8 100644 --- a/service/alertmanager/alertmanager.go +++ b/service/alertmanager/alertmanager.go @@ -4,7 +4,6 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "log" "sync" "time" @@ -149,7 +148,6 @@ func checkStatus() { } if flag { message := fmt.Sprintf("逮到咯,快去看看!服务器:%s(%s),报警规则:%s,%s", server.Name, server.Host.IP, alerts[j].Name, desc) - log.Printf("通知:%s\n", message) go sendNotification(message) } }