feat(watchdog): add exception handling and webhook integration

- Implement exception handling in TCP client to process error messages
- Add webhook functionality to send notifications for exceptions
- Introduce utility functions for string parsing
- Update config with webhook template
This commit is contained in:
2026-04-28 15:46:31 +08:00
parent ac51641e93
commit 0890c8136f
4 changed files with 116 additions and 3 deletions
+39
View File
@@ -0,0 +1,39 @@
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) {
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
}
return "", 200, ""
}