2020-12-19 09:14:36 -05:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2020-12-30 08:28:57 -05:00
|
|
|
"errors"
|
2020-12-19 09:14:36 -05:00
|
|
|
"fmt"
|
2022-09-14 10:26:14 -04:00
|
|
|
"io"
|
2020-12-19 09:14:36 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-03-18 11:13:22 -04:00
|
|
|
|
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2020-12-19 09:14:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota
|
|
|
|
NotificationRequestTypeJSON
|
|
|
|
NotificationRequestTypeForm
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota
|
|
|
|
NotificationRequestMethodGET
|
|
|
|
NotificationRequestMethodPOST
|
|
|
|
)
|
|
|
|
|
2022-04-17 05:38:45 -04:00
|
|
|
type NotificationServerBundle struct {
|
|
|
|
Notification *Notification
|
|
|
|
Server *Server
|
2023-09-01 12:06:40 -04:00
|
|
|
Loc *time.Location
|
2022-04-17 05:38:45 -04:00
|
|
|
}
|
|
|
|
|
2020-12-19 09:14:36 -05:00
|
|
|
type Notification struct {
|
|
|
|
Common
|
|
|
|
Name string
|
2022-04-14 09:06:42 -04:00
|
|
|
Tag string // 分组名
|
2020-12-19 09:14:36 -05:00
|
|
|
URL string
|
|
|
|
RequestMethod int
|
|
|
|
RequestType int
|
2021-11-05 00:04:39 -04:00
|
|
|
RequestHeader string `gorm:"type:longtext" `
|
2020-12-19 09:14:36 -05:00
|
|
|
RequestBody string `gorm:"type:longtext" `
|
|
|
|
VerifySSL *bool
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:38:45 -04:00
|
|
|
func (ns *NotificationServerBundle) reqURL(message string) string {
|
|
|
|
n := ns.Notification
|
2023-09-01 12:06:40 -04:00
|
|
|
return ns.replaceParamsInString(n.URL, message, func(msg string) string {
|
2021-01-03 08:11:00 -05:00
|
|
|
return url.QueryEscape(msg)
|
|
|
|
})
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
|
2021-11-05 03:49:01 -04:00
|
|
|
func (n *Notification) reqMethod() (string, error) {
|
|
|
|
switch n.RequestMethod {
|
|
|
|
case NotificationRequestMethodPOST:
|
|
|
|
return http.MethodPost, nil
|
|
|
|
case NotificationRequestMethodGET:
|
|
|
|
return http.MethodGet, nil
|
|
|
|
}
|
|
|
|
return "", errors.New("不支持的请求方式")
|
2021-11-05 00:04:39 -04:00
|
|
|
}
|
|
|
|
|
2022-04-17 05:38:45 -04:00
|
|
|
func (ns *NotificationServerBundle) reqBody(message string) (string, error) {
|
|
|
|
n := ns.Notification
|
2021-11-05 02:53:27 -04:00
|
|
|
if n.RequestMethod == NotificationRequestMethodGET || message == "" {
|
2020-12-30 08:28:57 -05:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
switch n.RequestType {
|
|
|
|
case NotificationRequestTypeJSON:
|
2023-09-01 12:06:40 -04:00
|
|
|
return ns.replaceParamsInString(n.RequestBody, message, func(msg string) string {
|
2022-03-18 11:13:22 -04:00
|
|
|
msgBytes, _ := utils.Json.Marshal(msg)
|
2021-01-03 08:11:00 -05:00
|
|
|
return string(msgBytes)[1 : len(msgBytes)-1]
|
|
|
|
}), nil
|
2020-12-30 08:28:57 -05:00
|
|
|
case NotificationRequestTypeForm:
|
|
|
|
var data map[string]string
|
2022-03-18 11:13:22 -04:00
|
|
|
if err := utils.Json.Unmarshal([]byte(n.RequestBody), &data); err != nil {
|
2020-12-30 08:28:57 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
params := url.Values{}
|
|
|
|
for k, v := range data {
|
2023-09-01 12:06:40 -04:00
|
|
|
params.Add(k, ns.replaceParamsInString(v, message, nil))
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
return params.Encode(), nil
|
|
|
|
}
|
|
|
|
return "", errors.New("不支持的请求类型")
|
|
|
|
}
|
|
|
|
|
2021-11-05 00:16:41 -04:00
|
|
|
func (n *Notification) setContentType(req *http.Request) {
|
2020-12-30 08:28:57 -05:00
|
|
|
if n.RequestMethod == NotificationRequestMethodGET {
|
2021-11-05 00:16:41 -04:00
|
|
|
return
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
if n.RequestType == NotificationRequestTypeForm {
|
2021-11-05 00:16:41 -04:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
} else {
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 00:04:39 -04:00
|
|
|
func (n *Notification) setRequestHeader(req *http.Request) error {
|
|
|
|
if n.RequestHeader == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var m map[string]string
|
2022-03-18 11:13:22 -04:00
|
|
|
if err := utils.Json.Unmarshal([]byte(n.RequestHeader), &m); err != nil {
|
2021-11-05 00:04:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:38:45 -04:00
|
|
|
func (ns *NotificationServerBundle) Send(message string) error {
|
2020-12-19 09:14:36 -05:00
|
|
|
var verifySSL bool
|
2022-04-17 05:38:45 -04:00
|
|
|
n := ns.Notification
|
2020-12-19 09:14:36 -05:00
|
|
|
if n.VerifySSL != nil && *n.VerifySSL {
|
|
|
|
verifySSL = true
|
|
|
|
}
|
|
|
|
|
|
|
|
transCfg := &http.Transport{
|
2023-10-21 10:21:21 -04:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: verifySSL},
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
2021-09-04 00:54:05 -04:00
|
|
|
|
2020-12-19 09:14:36 -05:00
|
|
|
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
|
2022-04-17 05:38:45 -04:00
|
|
|
reqBody, err := ns.reqBody(message)
|
2021-11-05 00:04:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-19 09:14:36 -05:00
|
|
|
|
2021-11-05 03:49:01 -04:00
|
|
|
reqMethod, err := n.reqMethod()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-17 05:38:45 -04:00
|
|
|
req, err := http.NewRequest(reqMethod, ns.reqURL(message), strings.NewReader(reqBody))
|
2021-11-05 00:04:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
|
2021-11-05 00:16:41 -04:00
|
|
|
n.setContentType(req)
|
|
|
|
|
2021-11-05 00:04:39 -04:00
|
|
|
if err := n.setRequestHeader(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
|
2021-11-05 00:04:39 -04:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
2023-02-12 08:10:34 -05:00
|
|
|
defer func() {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
}()
|
2022-09-14 10:26:14 -04:00
|
|
|
body, _ := io.ReadAll(resp.Body)
|
2021-11-05 00:04:39 -04:00
|
|
|
return fmt.Errorf("%d@%s %s", resp.StatusCode, resp.Status, string(body))
|
2020-12-19 23:18:27 -05:00
|
|
|
}
|
|
|
|
|
2021-11-05 00:04:39 -04:00
|
|
|
return nil
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
|
|
|
|
2022-04-17 09:06:30 -04:00
|
|
|
// replaceParamInString 替换字符串中的占位符
|
2023-09-01 12:06:40 -04:00
|
|
|
func (ns *NotificationServerBundle) replaceParamsInString(str string, message string, mod func(string) string) string {
|
|
|
|
if mod == nil {
|
|
|
|
mod = func(s string) string {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
|
|
|
|
str = strings.ReplaceAll(str, "#DATETIME#", mod(time.Now().In(ns.Loc).String()))
|
|
|
|
|
|
|
|
if ns.Server != nil {
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.NAME#", mod(ns.Server.Name))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.CPU#", mod(fmt.Sprintf("%f", ns.Server.State.CPU)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.MEM#", mod(fmt.Sprintf("%d", ns.Server.State.MemUsed)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.SWAP#", mod(fmt.Sprintf("%d", ns.Server.State.SwapUsed)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.DISK#", mod(fmt.Sprintf("%d", ns.Server.State.DiskUsed)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.NETINSPEED#", mod(fmt.Sprintf("%d", ns.Server.State.NetInSpeed)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.NETOUTSPEED#", mod(fmt.Sprintf("%d", ns.Server.State.NetOutSpeed)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.TRANSFERIN#", mod(fmt.Sprintf("%d", ns.Server.State.NetInTransfer)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.TRANSFEROUT#", mod(fmt.Sprintf("%d", ns.Server.State.NetOutTransfer)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.LOAD1#", mod(fmt.Sprintf("%f", ns.Server.State.Load1)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.LOAD5#", mod(fmt.Sprintf("%f", ns.Server.State.Load5)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.LOAD15#", mod(fmt.Sprintf("%f", ns.Server.State.Load15)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.TCPCONNCOUNT#", mod(fmt.Sprintf("%d", ns.Server.State.TcpConnCount)))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.UDPCONNCOUNT#", mod(fmt.Sprintf("%d", ns.Server.State.UdpConnCount)))
|
|
|
|
|
|
|
|
var ipv4, ipv6, validIP string
|
|
|
|
ipList := strings.Split(ns.Server.Host.IP, "/")
|
2022-04-24 09:21:03 -04:00
|
|
|
if len(ipList) > 1 {
|
|
|
|
// 双栈
|
|
|
|
ipv4 = ipList[0]
|
|
|
|
ipv6 = ipList[1]
|
|
|
|
validIP = ipv4
|
|
|
|
} else if len(ipList) == 1 {
|
|
|
|
// 仅ipv4|ipv6
|
|
|
|
if strings.Contains(ipList[0], ":") {
|
|
|
|
ipv6 = ipList[0]
|
|
|
|
validIP = ipv6
|
|
|
|
} else {
|
|
|
|
ipv4 = ipList[0]
|
|
|
|
validIP = ipv4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 12:06:40 -04:00
|
|
|
str = strings.ReplaceAll(str, "#SERVER.IP#", mod(validIP))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.IPV4#", mod(ipv4))
|
|
|
|
str = strings.ReplaceAll(str, "#SERVER.IPV6#", mod(ipv6))
|
2022-04-17 05:38:45 -04:00
|
|
|
}
|
2023-09-01 12:06:40 -04:00
|
|
|
|
2020-12-19 09:14:36 -05:00
|
|
|
return str
|
|
|
|
}
|