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
+32
View File
@@ -0,0 +1,32 @@
package watchdog
import (
"fmt"
"strings"
"super-frpc/global"
"super-frpc/webhook"
)
func exceptionHandle(exceptionType string, serviceName string, errorMsg string) {
body := make(map[string]string)
for k, v := range global.CurrentConfig.Webhook.Body {
replaced := v
for strings.Contains(replaced, "{{ exceptionType }}") ||
strings.Contains(replaced, "{{ serviceName }}") ||
strings.Contains(replaced, "{{ exceptionMsg }}") {
replaced = strings.ReplaceAll(replaced, "{{ exceptionType }}", exceptionType)
replaced = strings.ReplaceAll(replaced, "{{ serviceName }}", serviceName)
replaced = strings.ReplaceAll(replaced, "{{ exceptionMsg }}", errorMsg)
}
body[k] = replaced
}
bodyJSON := ""
for k, v := range body {
bodyJSON = fmt.Sprintf(`{"%s": "%s"}`, k, v)
break
}
webhook.SendHook(global.CurrentConfig.Webhook.URL, global.CurrentConfig.Webhook.Method, global.CurrentConfig.Webhook.Headers, bodyJSON)
}
+12 -3
View File
@@ -6,6 +6,7 @@ import (
"net"
"strings"
"super-frpc/postLog"
"super-frpc/utils"
"sync"
"time"
)
@@ -108,15 +109,23 @@ func recvMsg() {
if len(data) > 0 {
line := strings.TrimSpace(string(data))
if len(line) > 0 {
select {
case recvChan <- line:
case recvChan <- line:
default:
// drop the message
}
// Here add logic to handle the message
if !isResponseMessage(line) {
postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
cmdType := utils.GetCmdType(line)
if cmdType == "Exception" {
exceptionType := utils.GetCmdParams(line, "exceptionType")
serviceName := utils.GetCmdParams(line, "serviceName")
errorMsg := utils.GetCmdParams(line, "errorMsg")
postLog.Error(fmt.Sprintf("[Watchdog] Exception[%s]: %s, %s", serviceName, exceptionType, errorMsg))
exceptionHandle(exceptionType, serviceName, errorMsg)
}
}
}
}