From b3673238016ea817eafb65f82ec2058df1ca2203 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Sat, 4 Apr 2026 13:44:48 +0800 Subject: [PATCH] feat(command): add command parsing --- commandParse.go | 41 +++++++++++++++++++++++++++++++++++++++++ main.go | 7 ++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 commandParse.go diff --git a/commandParse.go b/commandParse.go new file mode 100644 index 0000000..d49037c --- /dev/null +++ b/commandParse.go @@ -0,0 +1,41 @@ +package main + +import ( + "strings" +) + +func getTextMiddle(text, left, right string) string { + start := 0 + if left != "" { + idx := strings.Index(text, left) + if idx == -1 { + return "" + } + start = idx + len(left) + } + + if right == "" { + if start > len(text) { + return "" + } + return text[start:] + } + + endIdx := strings.Index(text[start:], right) + if endIdx == -1 { + return "" + } + return text[start : start+endIdx] +} + +func getCommand(content string) string { + return getTextMiddle(content, "[", "]") +} + +func parseCommand(content, cmdType string) string { + return getTextMiddle(content, "<" + cmdType + ">", "") +} + +func executeCommand(input string) error { + return nil +} \ No newline at end of file diff --git a/main.go b/main.go index 4db9fe2..49078c3 100644 --- a/main.go +++ b/main.go @@ -70,7 +70,12 @@ func handleRequest(conn net.Conn) { if recvMsg == "watchdogAgentConnectionTest" { responseMsg = "success" } else { - responseMsg = "error: unknown message" + err := executeCommand(recvMsg) + if err != nil { + responseMsg = fmt.Sprintf("error: %v", err) + } else { + responseMsg = "success" + } } if _, err := conn.Write([]byte(responseMsg + "\n")); err != nil { postLog.Error(fmt.Sprintf("Failed to write: %v, err: %v", conn, err))