2020-12-19 09:14:36 -05:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2020-12-30 08:28:57 -05:00
|
|
|
"errors"
|
2020-12-19 09:14:36 -05:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota
|
|
|
|
NotificationRequestTypeJSON
|
|
|
|
NotificationRequestTypeForm
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ = iota
|
|
|
|
NotificationRequestMethodGET
|
|
|
|
NotificationRequestMethodPOST
|
|
|
|
)
|
|
|
|
|
|
|
|
type Notification struct {
|
|
|
|
Common
|
|
|
|
Name string
|
|
|
|
URL string
|
|
|
|
RequestMethod int
|
|
|
|
RequestType int
|
|
|
|
RequestBody string `gorm:"type:longtext" `
|
|
|
|
VerifySSL *bool
|
|
|
|
}
|
|
|
|
|
2020-12-30 08:28:57 -05:00
|
|
|
func (n *Notification) reqURL(message string) string {
|
2021-01-03 08:11:00 -05:00
|
|
|
return replaceParamsInString(n.URL, message, func(msg string) string {
|
|
|
|
return url.QueryEscape(msg)
|
|
|
|
})
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notification) reqBody(message string) (string, error) {
|
|
|
|
if n.RequestMethod == NotificationRequestMethodGET {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
switch n.RequestType {
|
|
|
|
case NotificationRequestTypeJSON:
|
2021-01-03 08:11:00 -05:00
|
|
|
return replaceParamsInString(n.RequestBody, message, func(msg string) string {
|
|
|
|
msgBytes, _ := json.Marshal(msg)
|
|
|
|
return string(msgBytes)[1 : len(msgBytes)-1]
|
|
|
|
}), nil
|
2020-12-30 08:28:57 -05:00
|
|
|
case NotificationRequestTypeForm:
|
|
|
|
var data map[string]string
|
|
|
|
if err := json.Unmarshal([]byte(n.RequestBody), &data); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
params := url.Values{}
|
|
|
|
for k, v := range data {
|
2021-01-03 08:17:39 -05:00
|
|
|
params.Add(k, replaceParamsInString(v, message, nil))
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
return params.Encode(), nil
|
|
|
|
}
|
|
|
|
return "", errors.New("不支持的请求类型")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notification) reqContentType() string {
|
|
|
|
if n.RequestMethod == NotificationRequestMethodGET {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if n.RequestType == NotificationRequestTypeForm {
|
|
|
|
return "application/x-www-form-urlencoded"
|
|
|
|
}
|
2021-01-03 08:11:00 -05:00
|
|
|
return "application/json"
|
2020-12-30 08:28:57 -05:00
|
|
|
}
|
|
|
|
|
2020-12-19 23:18:27 -05:00
|
|
|
func (n *Notification) Send(message string) error {
|
2020-12-19 09:14:36 -05:00
|
|
|
var verifySSL bool
|
|
|
|
|
|
|
|
if n.VerifySSL != nil && *n.VerifySSL {
|
|
|
|
verifySSL = true
|
|
|
|
}
|
|
|
|
|
2021-09-04 00:54:05 -04:00
|
|
|
/* #nosec */
|
2020-12-19 09:14:36 -05:00
|
|
|
transCfg := &http.Transport{
|
2021-09-04 00:54:05 -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}
|
2020-12-30 08:28:57 -05:00
|
|
|
|
|
|
|
reqBody, err := n.reqBody(message)
|
2020-12-19 09:14:36 -05:00
|
|
|
|
2020-12-19 23:18:27 -05:00
|
|
|
var resp *http.Response
|
|
|
|
|
2020-12-19 09:14:36 -05:00
|
|
|
if err == nil {
|
|
|
|
if n.RequestMethod == NotificationRequestMethodGET {
|
2020-12-30 08:28:57 -05:00
|
|
|
resp, err = client.Get(n.reqURL(message))
|
2020-12-19 09:14:36 -05:00
|
|
|
} else {
|
2020-12-30 08:28:57 -05:00
|
|
|
resp, err = client.Post(n.reqURL(message), n.reqContentType(), strings.NewReader(reqBody))
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
|
|
|
|
if err == nil && (resp.StatusCode < 200 || resp.StatusCode > 299) {
|
|
|
|
err = fmt.Errorf("%d %s", resp.StatusCode, resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
|
|
|
|
2021-01-03 08:11:00 -05:00
|
|
|
func replaceParamsInString(str string, message string, mod func(string) string) string {
|
|
|
|
if mod != nil {
|
|
|
|
str = strings.ReplaceAll(str, "#NEZHA#", mod(message))
|
|
|
|
} else {
|
|
|
|
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
|
|
|
}
|
2020-12-19 09:14:36 -05:00
|
|
|
return str
|
|
|
|
}
|