This commit is contained in:
spiritysdx
2024-06-30 20:28:53 +08:00
parent 6f0b1556bb
commit 861a8f46e4
2 changed files with 56 additions and 101 deletions

View File

@@ -18,8 +18,7 @@ import (
"time" "time"
) )
func main() { var (
var (
ecsVersion = "2024.06.30" ecsVersion = "2024.06.30"
showVersion bool showVersion bool
language string language string
@@ -30,8 +29,9 @@ func main() {
nt3CheckType, nt3Location string nt3CheckType, nt3Location string
spNum int spNum int
width = 84 width = 84
) )
func main() {
flag.BoolVar(&showVersion, "v", false, "Show version information") flag.BoolVar(&showVersion, "v", false, "Show version information")
flag.StringVar(&language, "l", "zh", "Specify language (supported: en, zh)") flag.StringVar(&language, "l", "zh", "Specify language (supported: en, zh)")
flag.StringVar(&cpuTestMethod, "cpum", "sysbench", "Specify CPU test method (supported: sysbench, geekbench, winsat)") flag.StringVar(&cpuTestMethod, "cpum", "sysbench", "Specify CPU test method (supported: sysbench, geekbench, winsat)")
@@ -54,28 +54,20 @@ func main() {
basicInfo, securityInfo, emailInfo, mediaInfo string basicInfo, securityInfo, emailInfo, mediaInfo string
output, tempOutput string output, tempOutput string
) )
if language == "zh" {
output = utils.PrintAndCapture(func() { output = utils.PrintAndCapture(func() {
switch language {
case "zh":
utils.PrintHead(language, width, ecsVersion) utils.PrintHead(language, width, ecsVersion)
utils.PrintCenteredTitle("基础信息", width) utils.PrintCenteredTitle("基础信息", width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
basicInfo, securityInfo, nt3CheckType = utils.SecurityCheck(language, nt3CheckType) basicInfo, securityInfo, nt3CheckType = utils.SecurityCheck(language, nt3CheckType)
fmt.Printf(basicInfo) fmt.Printf(basicInfo)
utils.PrintCenteredTitle(fmt.Sprintf("CPU测试-通过%s测试", cpuTestMethod), width) utils.PrintCenteredTitle(fmt.Sprintf("CPU测试-通过%s测试", cpuTestMethod), width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
cputest.CpuTest(language, cpuTestMethod, cpuTestThreadMode) cputest.CpuTest(language, cpuTestMethod, cpuTestThreadMode)
utils.PrintCenteredTitle(fmt.Sprintf("内存测试-通过%s测试", cpuTestMethod), width) utils.PrintCenteredTitle(fmt.Sprintf("内存测试-通过%s测试", cpuTestMethod), width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
memorytest.MemoryTest(language, memoryTestMethod) memorytest.MemoryTest(language, memoryTestMethod)
utils.PrintCenteredTitle(fmt.Sprintf("硬盘测试-通过%s测试", diskTestMethod), width) utils.PrintCenteredTitle(fmt.Sprintf("硬盘测试-通过%s测试", diskTestMethod), width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
disktest.DiskTest(language, diskTestMethod, diskTestPath, diskMultiCheck) disktest.DiskTest(language, diskTestMethod, diskTestPath, diskMultiCheck)
utils.PrintCenteredTitle("御三家流媒体解锁", width) utils.PrintCenteredTitle("御三家流媒体解锁", width)
}, tempOutput, output)
wg.Add(2) wg.Add(2)
go func() { go func() {
defer wg.Done() defer wg.Done()
@@ -85,119 +77,80 @@ func main() {
defer wg.Done() defer wg.Done()
mediaInfo = unlocktest.MediaTest(language) mediaInfo = unlocktest.MediaTest(language)
}() }()
output = utils.PrintAndCapture(func() {
commediatest.ComMediaTest(language) commediatest.ComMediaTest(language)
utils.PrintCenteredTitle("跨国流媒体解锁", width) utils.PrintCenteredTitle("跨国流媒体解锁", width)
}, tempOutput, output)
wg.Wait() // 后台任务含流媒体测试和邮件测试 wg.Wait() // 后台任务含流媒体测试和邮件测试
output = utils.PrintAndCapture(func() {
fmt.Printf(mediaInfo) fmt.Printf(mediaInfo)
utils.PrintCenteredTitle("IP质量检测", width) utils.PrintCenteredTitle("IP质量检测", width)
fmt.Printf(securityInfo) fmt.Printf(securityInfo)
utils.PrintCenteredTitle("邮件端口检测", width) utils.PrintCenteredTitle("邮件端口检测", width)
fmt.Println(emailInfo) fmt.Println(emailInfo)
}, tempOutput, output)
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
output = utils.PrintAndCapture(func() {
utils.PrintCenteredTitle("三网回程", width) utils.PrintCenteredTitle("三网回程", width)
backtrace.BackTrace() backtrace.BackTrace()
}, tempOutput, output)
// nexttrace 在win上不支持检测报错 bind: An invalid argument was supplied. // nexttrace 在win上不支持检测报错 bind: An invalid argument was supplied.
output = utils.PrintAndCapture(func() {
utils.PrintCenteredTitle("路由检测", width) utils.PrintCenteredTitle("路由检测", width)
ntrace.TraceRoute3(language, nt3Location, nt3CheckType) ntrace.TraceRoute3(language, nt3Location, nt3CheckType)
}, tempOutput, output)
} }
output = utils.PrintAndCapture(func() {
utils.PrintCenteredTitle("就近节点测速", width) utils.PrintCenteredTitle("就近节点测速", width)
speedtest.ShowHead(language) speedtest.ShowHead(language)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
speedtest.NearbySP() speedtest.NearbySP()
speedtest.CustomSP("net", "global", 4) speedtest.CustomSP("net", "global", 4)
speedtest.CustomSP("net", "cu", spNum) speedtest.CustomSP("net", "cu", spNum)
speedtest.CustomSP("net", "ct", spNum) speedtest.CustomSP("net", "ct", spNum)
speedtest.CustomSP("net", "cmcc", spNum) speedtest.CustomSP("net", "cmcc", spNum)
utils.PrintCenteredTitle("", width) utils.PrintCenteredTitle("", width)
}, tempOutput, output)
endTime := time.Now() endTime := time.Now()
duration := endTime.Sub(startTime) duration := endTime.Sub(startTime)
minutes := int(duration.Minutes()) minutes := int(duration.Minutes())
seconds := int(duration.Seconds()) % 60 seconds := int(duration.Seconds()) % 60
currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006") currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006")
output = utils.PrintAndCapture(func() {
fmt.Printf("花费 : %d 分 %d 秒\n", minutes, seconds) fmt.Printf("花费 : %d 分 %d 秒\n", minutes, seconds)
fmt.Printf("时间 : %s\n", currentTime) fmt.Printf("时间 : %s\n", currentTime)
utils.PrintCenteredTitle("", width) utils.PrintCenteredTitle("", width)
}, tempOutput, output) case "en":
} else if language == "en" {
output = utils.PrintAndCapture(func() {
utils.PrintHead(language, width, ecsVersion) utils.PrintHead(language, width, ecsVersion)
utils.PrintCenteredTitle("Basic Information", width) utils.PrintCenteredTitle("Basic Information", width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
basicInfo, securityInfo, nt3CheckType = utils.SecurityCheck(language, nt3CheckType) basicInfo, securityInfo, nt3CheckType = utils.SecurityCheck(language, nt3CheckType)
fmt.Printf(basicInfo) fmt.Printf(basicInfo)
utils.PrintCenteredTitle(fmt.Sprintf("CPU Test - %s Method", cpuTestMethod), width) utils.PrintCenteredTitle(fmt.Sprintf("CPU Test - %s Method", cpuTestMethod), width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
cputest.CpuTest(language, cpuTestMethod, cpuTestThreadMode) cputest.CpuTest(language, cpuTestMethod, cpuTestThreadMode)
utils.PrintCenteredTitle(fmt.Sprintf("Memory Test - %s Method", memoryTestMethod), width) 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) 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) utils.PrintCenteredTitle(fmt.Sprintf("Disk Test - %s Method", diskTestMethod), width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
disktest.DiskTest(language, diskTestMethod, diskTestPath, diskMultiCheck) disktest.DiskTest(language, diskTestMethod, diskTestPath, diskMultiCheck)
}, tempOutput, output)
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
emailInfo = email.EmailCheck() emailInfo = email.EmailCheck()
}() }()
output = utils.PrintAndCapture(func() {
utils.PrintCenteredTitle("The Three Families Streaming Media Unlock", width) utils.PrintCenteredTitle("The Three Families Streaming Media Unlock", width)
commediatest.ComMediaTest(language) commediatest.ComMediaTest(language)
utils.PrintCenteredTitle("Cross-Border Streaming Media Unlock", width) utils.PrintCenteredTitle("Cross-Border Streaming Media Unlock", width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
unlocktest.MediaTest(language) unlocktest.MediaTest(language)
utils.PrintCenteredTitle("IP Quality Check", width) utils.PrintCenteredTitle("IP Quality Check", width)
fmt.Printf(securityInfo) fmt.Printf(securityInfo)
utils.PrintCenteredTitle("Email Port Check", width) utils.PrintCenteredTitle("Email Port Check", width)
}, tempOutput, output)
wg.Wait() wg.Wait()
output = utils.PrintAndCapture(func() {
fmt.Println(emailInfo) fmt.Println(emailInfo)
//utils.PrintCenteredTitle("Return Path Routing", width) //utils.PrintCenteredTitle("Return Path Routing", width)
utils.PrintCenteredTitle("Nearby Node Speed Test", width) utils.PrintCenteredTitle("Nearby Node Speed Test", width)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
speedtest.ShowHead(language) speedtest.ShowHead(language)
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
speedtest.NearbySP() speedtest.NearbySP()
}, tempOutput, output)
output = utils.PrintAndCapture(func() {
speedtest.CustomSP("net", "global", -1) speedtest.CustomSP("net", "global", -1)
utils.PrintCenteredTitle("", width) utils.PrintCenteredTitle("", width)
}, tempOutput, output)
endTime := time.Now() endTime := time.Now()
duration := endTime.Sub(startTime) duration := endTime.Sub(startTime)
minutes := int(duration.Minutes()) minutes := int(duration.Minutes())
seconds := int(duration.Seconds()) % 60 seconds := int(duration.Seconds()) % 60
currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006") currentTime := time.Now().Format("Mon Jan 2 15:04:05 MST 2006")
output = utils.PrintAndCapture(func() {
fmt.Printf("Cost Time : %d 分 %d 秒\n", minutes, seconds) fmt.Printf("Cost Time : %d 分 %d 秒\n", minutes, seconds)
fmt.Printf("Current Time : %s\n", currentTime) fmt.Printf("Current Time : %s\n", currentTime)
utils.PrintCenteredTitle("", width) utils.PrintCenteredTitle("", width)
}, tempOutput, output) default:
fmt.Println("Unsupported language")
} }
}, tempOutput, output)
shorturl, err := utils.UploadText(output) shorturl, err := utils.UploadText(output)
if err != nil { if err != nil {
fmt.Println("Upload failed, can not generate short URL.") fmt.Println("Upload failed, can not generate short URL.")

View File

@@ -121,11 +121,13 @@ func CaptureOutput(f func()) string {
// 并发读取 stdout 和 stderr // 并发读取 stdout 和 stderr
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
io.Copy(&stdoutBuf, stdoutPipeR) multiWriter := io.MultiWriter(&stdoutBuf, oldStdout)
io.Copy(multiWriter, stdoutPipeR)
done <- struct{}{} done <- struct{}{}
}() }()
go func() { go func() {
io.Copy(&stderrBuf, stderrPipeR) multiWriter := io.MultiWriter(&stderrBuf, oldStderr)
io.Copy(multiWriter, stderrPipeR)
done <- struct{}{} done <- struct{}{}
}() }()
// 执行函数 // 执行函数
@@ -137,13 +139,13 @@ func CaptureOutput(f func()) string {
<-done <-done
<-done <-done
// 返回捕获的输出字符串 // 返回捕获的输出字符串
return stdoutBuf.String() + stderrBuf.String() // stderrBuf.String()
return stdoutBuf.String()
} }
// PrintAndCapture 捕获函数输出的同时打印内容 // PrintAndCapture 捕获函数输出的同时打印内容
func PrintAndCapture(f func(), tempOutput, output string) string { func PrintAndCapture(f func(), tempOutput, output string) string {
tempOutput = CaptureOutput(f) tempOutput = CaptureOutput(f)
fmt.Printf(tempOutput)
output += tempOutput output += tempOutput
return output return output
} }