- Change webhook config headers and body from map to string format - Move command parsing logic to separate function in command.go - Add debug logging for webhook operations - Improve exception handling with proper JSON parsing - Simplify config loading logic with default values
43 lines
1.6 KiB
Go
43 lines
1.6 KiB
Go
package webhook
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"super-frpc/postLog"
|
|
)
|
|
|
|
// SendHook sends a webhook to the specified URL.
|
|
// Returns the error message, status code, and response message.
|
|
// If the status code is not 200, it logs the error and returns the status code and response message.
|
|
// If the status code is 200, it returns an empty error message, status code, and response message.
|
|
func SendHook(url string, method string, headers map[string]string, body string) (err string, code int, msg string) {
|
|
postLog.Debug(fmt.Sprintf("[SendHook] SendHook { %s, %s, %s, %s }", url, method, headers, body))
|
|
req, reqErr := http.NewRequest(method, url, strings.NewReader(body))
|
|
if reqErr != nil {
|
|
return reqErr.Error(), 500, ""
|
|
}
|
|
for k, v := range headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
resp, doErr := http.DefaultClient.Do(req)
|
|
if doErr != nil {
|
|
return doErr.Error(), 500, ""
|
|
}
|
|
defer resp.Body.Close()
|
|
respMsg := ""
|
|
if resp.Body != nil {
|
|
respBody, _ := io.ReadAll(resp.Body)
|
|
respMsg = string(respBody)
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
postLog.Debug(fmt.Sprintf("[SendHook] SendHook { %s, %s, %s, %s } failed, status code: %d, response: [%s]: %s", url, method, headers, body, resp.StatusCode, resp.Status, respMsg))
|
|
return fmt.Sprintf("unexpected status code: %d, response: [%s]: %s", resp.StatusCode, resp.Status, respMsg), resp.StatusCode, respMsg
|
|
} else {
|
|
postLog.Debug(fmt.Sprintf("[SendHook] SendHook { %s, %s, %s, %s } success, status code: %d, response: [%s]: %s", url, method, headers, body, resp.StatusCode, resp.Status, respMsg))
|
|
}
|
|
return "", 200, ""
|
|
}
|