2020-12-19 09:14:36 -05:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
transCfg := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: verifySSL},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: transCfg, Timeout: time.Minute * 10}
|
|
|
|
var reqURL *url.URL
|
|
|
|
reqURL, err = url.Parse(n.URL)
|
|
|
|
var data map[string]string
|
|
|
|
if err == nil && (n.RequestMethod == NotificationRequestMethodGET || n.RequestType == NotificationRequestTypeForm) {
|
|
|
|
err = json.Unmarshal([]byte(n.RequestBody), &data)
|
|
|
|
}
|
|
|
|
|
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-19 23:18:27 -05:00
|
|
|
var queryValue = reqURL.Query()
|
2020-12-19 09:14:36 -05:00
|
|
|
for k, v := range data {
|
2020-12-19 23:18:27 -05:00
|
|
|
queryValue.Set(k, replaceParamsInString(v, message))
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
reqURL.RawQuery = queryValue.Encode()
|
|
|
|
resp, err = client.Get(reqURL.String())
|
2020-12-19 09:14:36 -05:00
|
|
|
} else {
|
|
|
|
if n.RequestType == NotificationRequestTypeForm {
|
|
|
|
params := url.Values{}
|
|
|
|
for k, v := range data {
|
2020-12-19 23:18:27 -05:00
|
|
|
params.Add(k, replaceParamsInString(v, message))
|
2020-12-19 09:14:36 -05:00
|
|
|
}
|
2020-12-19 23:18:27 -05:00
|
|
|
resp, err = client.PostForm(reqURL.String(), params)
|
2020-12-19 09:14:36 -05:00
|
|
|
} else {
|
2020-12-19 23:18:27 -05:00
|
|
|
jsonValue := replaceParamsInJSON(n.RequestBody, message)
|
|
|
|
resp, err = client.Post(reqURL.String(), "application/json", strings.NewReader(jsonValue))
|
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
|
|
|
}
|
|
|
|
|
2020-12-19 23:18:27 -05:00
|
|
|
func replaceParamsInString(str string, message string) string {
|
|
|
|
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
2020-12-19 09:14:36 -05:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2020-12-19 23:18:27 -05:00
|
|
|
func replaceParamsInJSON(str string, message string) string {
|
|
|
|
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
2020-12-19 09:14:36 -05:00
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonEscape(raw interface{}) string {
|
|
|
|
b, _ := json.Marshal(raw)
|
|
|
|
strb := string(b)
|
|
|
|
if strings.HasPrefix(strb, "\"") {
|
|
|
|
return strb[1 : len(strb)-1]
|
|
|
|
}
|
|
|
|
return strb
|
|
|
|
}
|