🐛 fix(notification): close #47

query escape && json encode
This commit is contained in:
naiba 2021-01-03 21:11:00 +08:00
parent d91819a265
commit e9690a5b50
2 changed files with 23 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -34,7 +35,9 @@ type Notification struct {
} }
func (n *Notification) reqURL(message string) string { 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) { func (n *Notification) reqBody(message string) (string, error) {
@ -43,7 +46,10 @@ func (n *Notification) reqBody(message string) (string, error) {
} }
switch n.RequestType { switch n.RequestType {
case NotificationRequestTypeJSON: 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: case NotificationRequestTypeForm:
var data map[string]string var data map[string]string
if err := json.Unmarshal([]byte(n.RequestBody), &data); err != nil { 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{} params := url.Values{}
for k, v := range data { 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 return params.Encode(), nil
} }
@ -64,9 +72,8 @@ func (n *Notification) reqContentType() string {
} }
if n.RequestType == NotificationRequestTypeForm { if n.RequestType == NotificationRequestTypeForm {
return "application/x-www-form-urlencoded" return "application/x-www-form-urlencoded"
} else {
return "application/json"
} }
return "application/json"
} }
func (n *Notification) Send(message string) error { 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) 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 return err
} }
func replaceParamsInString(str string, message string) string { func replaceParamsInString(str string, message string, mod func(string) string) string {
str = strings.ReplaceAll(str, "#NEZHA#", message) if mod != nil {
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
} else {
str = strings.ReplaceAll(str, "#NEZHA#", message)
}
return str return str
} }

View File

@ -4,7 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"log"
"sync" "sync"
"time" "time"
@ -149,7 +148,6 @@ func checkStatus() {
} }
if flag { if flag {
message := fmt.Sprintf("逮到咯,快去看看!服务器:%s(%s),报警规则:%s%s", server.Name, server.Host.IP, alerts[j].Name, desc) message := fmt.Sprintf("逮到咯,快去看看!服务器:%s(%s),报警规则:%s%s", server.Name, server.Host.IP, alerts[j].Name, desc)
log.Printf("通知:%s\n", message)
go sendNotification(message) go sendNotification(message)
} }
} }