fix(watchdog): fix watchdog unable to send webhook and simplify the parsing of json config

- 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
This commit is contained in:
2026-04-28 20:48:54 +08:00
parent 8108d4d01f
commit 1432651a14
6 changed files with 93 additions and 61 deletions
+14
View File
@@ -3,6 +3,7 @@ package watchdog
import (
"fmt"
"super-frpc/postLog"
"super-frpc/utils"
)
func AddInstance(serviceName string) bool {
@@ -70,4 +71,17 @@ func Close() bool {
}
return false
}
func parseCommand(cmd string) (result string, err error) {
cmdType := utils.GetCmdType(cmd)
if cmdType == "Exception" {
// Watchdog msg: [Exception] <exceptionType>...</exceptionType> <serviceName>...</serviceName> <errorMsg>...</errorMsg>
exceptionType := utils.GetCmdParams(cmd, "exceptionType")
serviceName := utils.GetCmdParams(cmd, "serviceName")
errorMsg := utils.GetCmdParams(cmd, "errorMsg")
postLog.Error(fmt.Sprintf("[Watchdog] Exception[%s]: %s, %s", serviceName, exceptionType, errorMsg))
exceptionHandle(exceptionType, serviceName, errorMsg)
}
return "", nil
}
+34 -10
View File
@@ -1,18 +1,26 @@
package watchdog
import (
"encoding/json"
"fmt"
"strings"
"super-frpc/global"
"super-frpc/postLog"
"super-frpc/webhook"
)
func exceptionHandle(exceptionType string, serviceName string, errorMsg string) {
if global.CurrentConfig.Notification.Enabled {
body := make(map[string]string)
if global.CurrentConfig.Notification.Method == "Webhook" {
for k, v := range global.CurrentConfig.Webhook.Body {
if global.CurrentConfig.Notification.Method == "webhook" {
var bodyMap map[string]string
err := json.Unmarshal([]byte(global.CurrentConfig.Webhook.Body), &bodyMap)
if err != nil {
postLog.Error(fmt.Sprintf("[exceptionHandler] Failed to parse webhook body: %v", err))
return
}
for k, v := range bodyMap {
replaced := v
for strings.Contains(replaced, "{{ exceptionType }}") ||
strings.Contains(replaced, "{{ serviceName }}") ||
@@ -21,17 +29,33 @@ func exceptionHandle(exceptionType string, serviceName string, errorMsg string)
replaced = strings.ReplaceAll(replaced, "{{ serviceName }}", serviceName)
replaced = strings.ReplaceAll(replaced, "{{ exceptionMsg }}", errorMsg)
}
body[k] = replaced
bodyMap[k] = replaced
}
bodyJSON := ""
for k, v := range body {
bodyJSON = fmt.Sprintf(`{"%s": "%s"}`, k, v)
break
bodyJSON, err := json.Marshal(bodyMap)
if err != nil {
postLog.Error(fmt.Sprintf("[exceptionHandler] Failed to marshal webhook body: %v", err))
return
}
webhook.SendHook(global.CurrentConfig.Webhook.URL, global.CurrentConfig.Webhook.Method, global.CurrentConfig.Webhook.Headers, bodyJSON)
// Parse headers as map[string]string format
headersMap := make(map[string]string)
headerPairs := strings.Split(global.CurrentConfig.Webhook.Headers, ";")
for _, pair := range headerPairs {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
parts := strings.SplitN(pair, ":", 2)
if len(parts) == 2 {
headersMap[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
webhook.SendHook(global.CurrentConfig.Webhook.URL, global.CurrentConfig.Webhook.Method, headersMap, string(bodyJSON))
}
} else {
postLog.Warning("[exceptionHandler] exception occoured, but notification is not enabled!")
}
}
+2 -11
View File
@@ -5,8 +5,6 @@ import (
"fmt"
"net"
"strings"
"super-frpc/postLog"
"super-frpc/utils"
"sync"
"time"
)
@@ -117,15 +115,8 @@ func recvMsg() {
}
// 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)
}
// postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
parseCommand(line)
}
}
}