diff --git a/README.md b/README.md
index 15d5896..9002678 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
LOGO designed by 熊大 .
-
+
:trollface: 哪吒监控 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。
diff --git a/cmd/dashboard/controller/member_api.go b/cmd/dashboard/controller/member_api.go index f68e684..52c546c 100644 --- a/cmd/dashboard/controller/member_api.go +++ b/cmd/dashboard/controller/member_api.go @@ -366,6 +366,7 @@ type notificationForm struct { URL string RequestMethod int RequestType int + RequestHeader string RequestBody string VerifySSL string } @@ -378,6 +379,7 @@ func (ma *memberAPI) addOrEditNotification(c *gin.Context) { n.Name = nf.Name n.RequestMethod = nf.RequestMethod n.RequestType = nf.RequestType + n.RequestHeader = nf.RequestHeader n.RequestBody = nf.RequestBody n.URL = nf.URL verifySSL := nf.VerifySSL == "on" diff --git a/model/notification.go b/model/notification.go index a1ebb39..11e094f 100644 --- a/model/notification.go +++ b/model/notification.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" "net/http" "net/url" "strings" @@ -29,6 +30,7 @@ type Notification struct { URL string RequestMethod int RequestType int + RequestHeader string `gorm:"type:longtext" ` RequestBody string `gorm:"type:longtext" ` VerifySSL *bool } @@ -39,6 +41,16 @@ func (n *Notification) reqURL(message string) string { }) } +func (n *Notification) reqMethod() string { + switch n.RequestMethod { + case NotificationRequestMethodGET: + return http.MethodGet + case NotificationRequestMethodPOST: + return http.MethodGet + } + return "" +} + func (n *Notification) reqBody(message string) (string, error) { if n.RequestMethod == NotificationRequestMethodGET { return "", nil @@ -73,6 +85,20 @@ func (n *Notification) reqContentType() string { return "application/json" } +func (n *Notification) setRequestHeader(req *http.Request) error { + if n.RequestHeader == "" { + return nil + } + var m map[string]string + if err := json.Unmarshal([]byte(n.RequestHeader), &m); err != nil { + return err + } + for k, v := range m { + req.Header.Set(k, v) + } + return nil +} + func (n *Notification) Send(message string) error { var verifySSL bool @@ -86,24 +112,32 @@ func (n *Notification) Send(message string) error { } client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10} - reqBody, err := n.reqBody(message) - - var resp *http.Response - - if err == nil { - if n.RequestMethod == NotificationRequestMethodGET { - resp, err = client.Get(n.reqURL(message)) - } else { - resp, err = client.Post(n.reqURL(message), n.reqContentType(), strings.NewReader(reqBody)) - } + if err != nil { + return err } - if err == nil && (resp.StatusCode < 200 || resp.StatusCode > 299) { - err = fmt.Errorf("%d %s", resp.StatusCode, resp.Status) + req, err := http.NewRequest(n.reqMethod(), n.reqURL(message), strings.NewReader(reqBody)) + if err != nil { + return err } - return err + if err := n.setRequestHeader(req); err != nil { + return err + } + + resp, err := client.Do(req) + if err != nil { + return err + } + + if resp.StatusCode < 200 || resp.StatusCode > 299 { + defer resp.Body.Close() + body, _ := ioutil.ReadAll(resp.Body) + return fmt.Errorf("%d@%s %s", resp.StatusCode, resp.Status, string(body)) + } + + return nil } func replaceParamsInString(str string, message string, mod func(string) string) string { diff --git a/resource/static/main.js b/resource/static/main.js index 1d231ea..c185373 100644 --- a/resource/static/main.js +++ b/resource/static/main.js @@ -135,6 +135,9 @@ function addOrEditNotification(notification) { modal.find("input[name=ID]").val(notification ? notification.ID : null); modal.find("input[name=Name]").val(notification ? notification.Name : null); modal.find("input[name=URL]").val(notification ? notification.URL : null); + modal + .find("textarea[name=RequestHeader]") + .val(notification ? notification.RequestHeader : null); modal .find("textarea[name=RequestBody]") .val(notification ? notification.RequestBody : null); diff --git a/resource/template/common/footer.html b/resource/template/common/footer.html index 6ca4524..5f0c6b3 100644 --- a/resource/template/common/footer.html +++ b/resource/template/common/footer.html @@ -9,7 +9,7 @@ - +