From 6636d77cfd023275a7f6046e3a1c488e6d3f64cd Mon Sep 17 00:00:00 2001 From: naiba Date: Fri, 5 Nov 2021 12:04:39 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89=20request=20heade?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- cmd/dashboard/controller/member_api.go | 2 + model/notification.go | 60 +++++++++++++++---- resource/static/main.js | 3 + resource/template/common/footer.html | 2 +- resource/template/component/notification.html | 6 +- service/dao/dao.go | 2 +- 7 files changed, 60 insertions(+), 17 deletions(-) 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 @@ - + diff --git a/resource/template/component/notification.html b/resource/template/component/notification.html index 97403c9..9ba5a09 100644 --- a/resource/template/component/notification.html +++ b/resource/template/component/notification.html @@ -26,9 +26,13 @@ +
+ + +
- +
diff --git a/service/dao/dao.go b/service/dao/dao.go index 9cd9af2..5bd167f 100644 --- a/service/dao/dao.go +++ b/service/dao/dao.go @@ -13,7 +13,7 @@ import ( pb "github.com/naiba/nezha/proto" ) -var Version = "v0.10.8" // !!记得修改 README 中的 badge 版本!! +var Version = "v0.11.0" // !!记得修改 README 中的 badge 版本!! var ( Conf *model.Config