mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
♻️ 通知支持自定义 request header
This commit is contained in:
parent
d20fd7a9fe
commit
6636d77cfd
@ -4,7 +4,7 @@
|
||||
<br>
|
||||
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
|
||||
<br><br>
|
||||
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.10.8&logo=github&style=for-the-badge"> <img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github"> <img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge"> <img src="https://img.shields.io/badge/Installer-v0.7.1-brightgreen?style=for-the-badge&logo=linux">
|
||||
<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Dashboard%20image?label=Dash%20v0.11.0&logo=github&style=for-the-badge"> <img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github"> <img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge"> <img src="https://img.shields.io/badge/Installer-v0.7.1-brightgreen?style=for-the-badge&logo=linux">
|
||||
<br>
|
||||
<br>
|
||||
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。</p>
|
||||
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
2
resource/template/common/footer.html
vendored
2
resource/template/common/footer.html
vendored
@ -9,7 +9,7 @@
|
||||
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.js"></script>
|
||||
<script src="/static/semantic-ui-alerts.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
|
||||
<script src="/static/main.js?v20210902"></script>
|
||||
<script src="/static/main.js?v20211105"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
@ -26,9 +26,13 @@
|
||||
<option value="2">FORM</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="secret field">
|
||||
<label>Header</label>
|
||||
<textarea name="RequestHeader" placeholder='{"User-Agent":"Nezha-Agent"}'></textarea>
|
||||
</div>
|
||||
<div class="secret field">
|
||||
<label>Body</label>
|
||||
<textarea name="RequestBody"></textarea>
|
||||
<textarea name="RequestBody" placeholder='{"content":"#NEZHA#"}'></textarea>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="ui nf-ssl checkbox">
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user