♻️ 通知支持自定义 request header

This commit is contained in:
naiba 2021-11-05 12:04:39 +08:00
parent d20fd7a9fe
commit 6636d77cfd
7 changed files with 60 additions and 17 deletions

View File

@ -4,7 +4,7 @@
<br> <br>
<small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small> <small><i>LOGO designed by <a href="https://xio.ng" target="_blank">熊大</a> .</i></small>
<br><br> <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">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<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">&nbsp;<img src="https://img.shields.io/github/v/release/naiba/nezha?color=brightgreen&label=Agent&style=for-the-badge&logo=github">&nbsp;<img src="https://img.shields.io/github/workflow/status/naiba/nezha/Agent%20release?label=Agent%20CI&logo=github&style=for-the-badge">&nbsp;<img src="https://img.shields.io/badge/Installer-v0.7.1-brightgreen?style=for-the-badge&logo=linux">
<br> <br>
<br> <br>
<p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。</p> <p>:trollface: <b>哪吒监控</b> 一站式轻监控轻运维系统。支持系统状态、HTTP(SSL 证书变更、即将到期、到期)、TCP、Ping 监控报警,计划任务和在线终端。</p>

View File

@ -366,6 +366,7 @@ type notificationForm struct {
URL string URL string
RequestMethod int RequestMethod int
RequestType int RequestType int
RequestHeader string
RequestBody string RequestBody string
VerifySSL string VerifySSL string
} }
@ -378,6 +379,7 @@ func (ma *memberAPI) addOrEditNotification(c *gin.Context) {
n.Name = nf.Name n.Name = nf.Name
n.RequestMethod = nf.RequestMethod n.RequestMethod = nf.RequestMethod
n.RequestType = nf.RequestType n.RequestType = nf.RequestType
n.RequestHeader = nf.RequestHeader
n.RequestBody = nf.RequestBody n.RequestBody = nf.RequestBody
n.URL = nf.URL n.URL = nf.URL
verifySSL := nf.VerifySSL == "on" verifySSL := nf.VerifySSL == "on"

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -29,6 +30,7 @@ type Notification struct {
URL string URL string
RequestMethod int RequestMethod int
RequestType int RequestType int
RequestHeader string `gorm:"type:longtext" `
RequestBody string `gorm:"type:longtext" ` RequestBody string `gorm:"type:longtext" `
VerifySSL *bool 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) { func (n *Notification) reqBody(message string) (string, error) {
if n.RequestMethod == NotificationRequestMethodGET { if n.RequestMethod == NotificationRequestMethodGET {
return "", nil return "", nil
@ -73,6 +85,20 @@ func (n *Notification) reqContentType() string {
return "application/json" 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 { func (n *Notification) Send(message string) error {
var verifySSL bool var verifySSL bool
@ -86,24 +112,32 @@ func (n *Notification) Send(message string) error {
} }
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10} client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
reqBody, err := n.reqBody(message) reqBody, err := n.reqBody(message)
if err != nil {
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 && (resp.StatusCode < 200 || resp.StatusCode > 299) {
err = fmt.Errorf("%d %s", resp.StatusCode, resp.Status)
}
return err return err
}
req, err := http.NewRequest(n.reqMethod(), n.reqURL(message), strings.NewReader(reqBody))
if err != nil {
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 { func replaceParamsInString(str string, message string, mod func(string) string) string {

View File

@ -135,6 +135,9 @@ function addOrEditNotification(notification) {
modal.find("input[name=ID]").val(notification ? notification.ID : null); modal.find("input[name=ID]").val(notification ? notification.ID : null);
modal.find("input[name=Name]").val(notification ? notification.Name : null); modal.find("input[name=Name]").val(notification ? notification.Name : null);
modal.find("input[name=URL]").val(notification ? notification.URL : null); modal.find("input[name=URL]").val(notification ? notification.URL : null);
modal
.find("textarea[name=RequestHeader]")
.val(notification ? notification.RequestHeader : null);
modal modal
.find("textarea[name=RequestBody]") .find("textarea[name=RequestBody]")
.val(notification ? notification.RequestBody : null); .val(notification ? notification.RequestBody : null);

View File

@ -9,7 +9,7 @@
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.1/dist/semantic.min.js"></script> <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="/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="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> </body>
</html> </html>

View File

@ -26,9 +26,13 @@
<option value="2">FORM</option> <option value="2">FORM</option>
</select> </select>
</div> </div>
<div class="secret field">
<label>Header</label>
<textarea name="RequestHeader" placeholder='{"User-Agent":"Nezha-Agent"}'></textarea>
</div>
<div class="secret field"> <div class="secret field">
<label>Body</label> <label>Body</label>
<textarea name="RequestBody"></textarea> <textarea name="RequestBody" placeholder='{"content":"#NEZHA#"}'></textarea>
</div> </div>
<div class="field"> <div class="field">
<div class="ui nf-ssl checkbox"> <div class="ui nf-ssl checkbox">

View File

@ -13,7 +13,7 @@ import (
pb "github.com/naiba/nezha/proto" pb "github.com/naiba/nezha/proto"
) )
var Version = "v0.10.8" // !!记得修改 README 中的 badge 版本!! var Version = "v0.11.0" // !!记得修改 README 中的 badge 版本!!
var ( var (
Conf *model.Config Conf *model.Config