From 6f0b1556bb337657da99a31a51b3503af09f6a58 Mon Sep 17 00:00:00 2001 From: spiritysdx Date: Sun, 30 Jun 2024 20:15:55 +0800 Subject: [PATCH] update --- goecs.go | 83 ++++++++++++++++++++++++++++++++++---------------- utils/utils.go | 58 +++++++++++++++++++++++++++++------ 2 files changed, 105 insertions(+), 36 deletions(-) diff --git a/goecs.go b/goecs.go index 7656db6..172e361 100644 --- a/goecs.go +++ b/goecs.go @@ -131,48 +131,77 @@ func main() { utils.PrintCenteredTitle("", width) }, tempOutput, output) } else if language == "en" { - utils.PrintHead(language, width, ecsVersion) - utils.PrintCenteredTitle("Basic Information", width) - basicInfo, securityInfo, nt3CheckType = utils.SecurityCheck(language, nt3CheckType) - fmt.Printf(basicInfo) - utils.PrintCenteredTitle(fmt.Sprintf("CPU Test - %s Method", cpuTestMethod), width) - cputest.CpuTest(language, cpuTestMethod, cpuTestThreadMode) - utils.PrintCenteredTitle(fmt.Sprintf("Memory Test - %s Method", memoryTestMethod), width) - memorytest.MemoryTest(language, memoryTestMethod) - utils.PrintCenteredTitle(fmt.Sprintf("Disk Test - %s Method", diskTestMethod), width) - disktest.DiskTest(language, diskTestMethod, diskTestPath, diskMultiCheck) + output = utils.PrintAndCapture(func() { + utils.PrintHead(language, width, ecsVersion) + utils.PrintCenteredTitle("Basic Information", width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + basicInfo, securityInfo, nt3CheckType = utils.SecurityCheck(language, nt3CheckType) + fmt.Printf(basicInfo) + utils.PrintCenteredTitle(fmt.Sprintf("CPU Test - %s Method", cpuTestMethod), width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + cputest.CpuTest(language, cpuTestMethod, cpuTestThreadMode) + utils.PrintCenteredTitle(fmt.Sprintf("Memory Test - %s Method", memoryTestMethod), width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + memorytest.MemoryTest(language, memoryTestMethod) + utils.PrintCenteredTitle(fmt.Sprintf("Disk Test - %s Method", diskTestMethod), width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + memorytest.MemoryTest(language, memoryTestMethod) + utils.PrintCenteredTitle(fmt.Sprintf("Disk Test - %s Method", diskTestMethod), width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + disktest.DiskTest(language, diskTestMethod, diskTestPath, diskMultiCheck) + }, tempOutput, output) wg.Add(1) go func() { defer wg.Done() emailInfo = email.EmailCheck() }() - utils.PrintCenteredTitle("The Three Families Streaming Media Unlock", width) - commediatest.ComMediaTest(language) - utils.PrintCenteredTitle("Cross-Border Streaming Media Unlock", width) - unlocktest.MediaTest(language) - utils.PrintCenteredTitle("IP Quality Check", width) - fmt.Printf(securityInfo) - utils.PrintCenteredTitle("Email Port Check", width) + output = utils.PrintAndCapture(func() { + utils.PrintCenteredTitle("The Three Families Streaming Media Unlock", width) + commediatest.ComMediaTest(language) + utils.PrintCenteredTitle("Cross-Border Streaming Media Unlock", width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + unlocktest.MediaTest(language) + utils.PrintCenteredTitle("IP Quality Check", width) + fmt.Printf(securityInfo) + utils.PrintCenteredTitle("Email Port Check", width) + }, tempOutput, output) wg.Wait() - fmt.Println(emailInfo) - //utils.PrintCenteredTitle("Return Path Routing", width) - utils.PrintCenteredTitle("Nearby Node Speed Test", width) - speedtest.ShowHead(language) - speedtest.NearbySP() - speedtest.CustomSP("net", "global", -1) - utils.PrintCenteredTitle("", width) + output = utils.PrintAndCapture(func() { + fmt.Println(emailInfo) + //utils.PrintCenteredTitle("Return Path Routing", width) + utils.PrintCenteredTitle("Nearby Node Speed Test", width) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + speedtest.ShowHead(language) + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + speedtest.NearbySP() + }, tempOutput, output) + output = utils.PrintAndCapture(func() { + speedtest.CustomSP("net", "global", -1) + utils.PrintCenteredTitle("", width) + }, tempOutput, output) endTime := time.Now() duration := endTime.Sub(startTime) minutes := int(duration.Minutes()) seconds := int(duration.Seconds()) % 60 - fmt.Printf("Cost Time : %d 分 %d 秒\n", minutes, seconds) currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006") - fmt.Printf("Current Time : %s\n", currentTime) - utils.PrintCenteredTitle("", width) + output = utils.PrintAndCapture(func() { + fmt.Printf("Cost Time : %d 分 %d 秒\n", minutes, seconds) + fmt.Printf("Current Time : %s\n", currentTime) + utils.PrintCenteredTitle("", width) + }, tempOutput, output) } shorturl, err := utils.UploadText(output) if err != nil { fmt.Println("Upload failed, can not generate short URL.") + fmt.Println(err.Error()) } fmt.Println("Upload successful, short URL:", shorturl) } diff --git a/utils/utils.go b/utils/utils.go index f02f2f6..b2322e9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -7,6 +7,7 @@ import ( "github.com/oneclickvirt/UnlockTests/uts" "github.com/oneclickvirt/basics/system" "github.com/oneclickvirt/security/network" + "io" "os" "strings" "sync" @@ -87,17 +88,56 @@ func SecurityCheck(language, nt3CheckType string) (string, string, string) { return basicInfo, securityInfo, nt3CheckType } -// CaptureOutput 捕获函数输出并返回字符串 +// CaptureOutput 捕获函数输出和错误输出并返回字符串 func CaptureOutput(f func()) string { - old := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + // 保存旧的 stdout 和 stderr + oldStdout := os.Stdout + oldStderr := os.Stderr + // 创建管道 + stdoutPipeR, stdoutPipeW, err := os.Pipe() + if err != nil { + return "Error creating stdout pipe" + } + stderrPipeR, stderrPipeW, err := os.Pipe() + if err != nil { + stdoutPipeW.Close() + stdoutPipeR.Close() + return "Error creating stderr pipe" + } + // 替换标准输出和标准错误输出为管道写入端 + os.Stdout = stdoutPipeW + os.Stderr = stderrPipeW + // 恢复标准输出和标准错误输出 + defer func() { + os.Stdout = oldStdout + os.Stderr = oldStderr + stdoutPipeW.Close() + stderrPipeW.Close() + stdoutPipeR.Close() + stderrPipeR.Close() + }() + // 缓冲区 + var stdoutBuf, stderrBuf bytes.Buffer + // 并发读取 stdout 和 stderr + done := make(chan struct{}) + go func() { + io.Copy(&stdoutBuf, stdoutPipeR) + done <- struct{}{} + }() + go func() { + io.Copy(&stderrBuf, stderrPipeR) + done <- struct{}{} + }() + // 执行函数 f() - w.Close() - os.Stdout = old - var buf bytes.Buffer - buf.ReadFrom(r) - return buf.String() + // 关闭管道写入端,让管道读取端可以读取所有数据 + stdoutPipeW.Close() + stderrPipeW.Close() + // 等待两个 goroutine 完成 + <-done + <-done + // 返回捕获的输出字符串 + return stdoutBuf.String() + stderrBuf.String() } // PrintAndCapture 捕获函数输出的同时打印内容