From 5a289ccf750a8d911c7f39a193f24e339db78db1 Mon Sep 17 00:00:00 2001 From: ish Date: Wed, 4 Dec 2024 17:40:39 +0900 Subject: [PATCH] server: lib: ssh: Fix benchmark data parsing issue --- server/lib/ssh/ssh.go | 52 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/server/lib/ssh/ssh.go b/server/lib/ssh/ssh.go index c453f70..1e32dc0 100644 --- a/server/lib/ssh/ssh.go +++ b/server/lib/ssh/ssh.go @@ -125,14 +125,54 @@ func (o *SSH) runBenchmarkCommand(cmd string, BenchmarkList *[]model.Benchmark, } logger.Println(logger.DEBUG, true, "SSH: Benchmark Output: "+output) - var response Response - if err := json.Unmarshal([]byte(output), &response); err != nil { - logger.Println(logger.DEBUG, true, "Failed to unmarshal output", err) - return err + // Find the first complete JSON object + var firstJSON string + braceCount := 0 + inQuotes := false + escapeNext := false + + for _, char := range output { + if escapeNext { + escapeNext = false + firstJSON += string(char) + continue + } + + switch char { + case '\\': + escapeNext = true + firstJSON += string(char) + case '"': + inQuotes = !inQuotes + firstJSON += string(char) + case '{': + if !inQuotes { + braceCount++ + } + firstJSON += string(char) + case '}': + if !inQuotes { + braceCount-- + if braceCount == 0 { + firstJSON += string(char) + // Found complete JSON object + var response Response + if err := json.Unmarshal([]byte(firstJSON), &response); err != nil { + logger.Println(logger.DEBUG, true, "Failed to unmarshal output", err) + return err + } + + *BenchmarkList = append(*BenchmarkList, model.Benchmark{Type: t, Data: response.Data}) + return nil + } + } + firstJSON += string(char) + default: + firstJSON += string(char) + } } - *BenchmarkList = append(*BenchmarkList, model.Benchmark{Type: t, Data: response.Data}) - return nil + return fmt.Errorf("no valid JSON object found in output") } func (o *SSH) StopBenchmark(connectionInfo model.ConnectionInfo) error {