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"
|
2021-11-05 00:04:39 -04:00
|
|
|
"io/ioutil"
|
2020-12-19 09:14:36 -05:00
|
|
|
"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
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-05 00:04:39 -04:00
|
|
|
func (n *Notification) reqMethod() string {
|
|
|
|
switch n.RequestMethod {
|
|
|
|
case NotificationRequestMethodGET:
|
|
|
|
return http.MethodGet
|
|
|
|
case NotificationRequestMethodPOST:
|
|
|
|
return http.MethodGet
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if err := json.Unmarshal([]byte(n.RequestHeader), &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
2021-11-05 00:04:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-19 09:14:36 -05:00
|
|
|
|
2021-11-05 00:04:39 -04:00
|
|
|
req, err := http.NewRequest(n.reqMethod(), n.reqURL(message), strings.NewReader(reqBody))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
|
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 {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|