mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
🎨 refactor: notification
This commit is contained in:
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -32,6 +33,42 @@ type Notification struct {
|
||||
VerifySSL *bool
|
||||
}
|
||||
|
||||
func (n *Notification) reqURL(message string) string {
|
||||
return replaceParamsInString(n.URL, message)
|
||||
}
|
||||
|
||||
func (n *Notification) reqBody(message string) (string, error) {
|
||||
if n.RequestMethod == NotificationRequestMethodGET {
|
||||
return "", nil
|
||||
}
|
||||
switch n.RequestType {
|
||||
case NotificationRequestTypeJSON:
|
||||
return replaceParamsInString(n.RequestBody, message), nil
|
||||
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 {
|
||||
params.Add(k, replaceParamsInString(v, message))
|
||||
}
|
||||
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"
|
||||
} else {
|
||||
return "application/json"
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notification) Send(message string) error {
|
||||
var verifySSL bool
|
||||
|
||||
@@ -39,39 +76,20 @@ func (n *Notification) Send(message string) error {
|
||||
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)
|
||||
}
|
||||
|
||||
reqBody, err := n.reqBody(message)
|
||||
|
||||
var resp *http.Response
|
||||
|
||||
if err == nil {
|
||||
if n.RequestMethod == NotificationRequestMethodGET {
|
||||
var queryValue = reqURL.Query()
|
||||
for k, v := range data {
|
||||
queryValue.Set(k, replaceParamsInString(v, message))
|
||||
}
|
||||
reqURL.RawQuery = queryValue.Encode()
|
||||
resp, err = client.Get(reqURL.String())
|
||||
resp, err = client.Get(n.reqURL(message))
|
||||
} else {
|
||||
if n.RequestType == NotificationRequestTypeForm {
|
||||
params := url.Values{}
|
||||
for k, v := range data {
|
||||
params.Add(k, replaceParamsInString(v, message))
|
||||
}
|
||||
resp, err = client.PostForm(reqURL.String(), params)
|
||||
} else {
|
||||
jsonValue := replaceParamsInJSON(n.RequestBody, message)
|
||||
resp, err = client.Post(reqURL.String(), "application/json", strings.NewReader(jsonValue))
|
||||
}
|
||||
resp, err = client.Post(n.reqURL(message), n.reqContentType(), strings.NewReader(reqBody))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,17 +104,3 @@ func replaceParamsInString(str string, message string) string {
|
||||
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
||||
return str
|
||||
}
|
||||
|
||||
func replaceParamsInJSON(str string, message string) string {
|
||||
str = strings.ReplaceAll(str, "#NEZHA#", message)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user