diff --git a/api/api.go b/api/api.go new file mode 100644 index 00000000..97b562e0 --- /dev/null +++ b/api/api.go @@ -0,0 +1,56 @@ +package api + +const ( + // Version API版本号 + Version = "v1.0.0" + + // DefaultVersion 默认的ECS版本号 + DefaultVersion = "v0.1.114" +) + +// 测试方法常量 +const ( + // CPU测试方法 + CpuMethodSysbench = "sysbench" + CpuMethodGeekbench = "geekbench" + CpuMethodWinsat = "winsat" + + // 内存测试方法 + MemoryMethodStream = "stream" + MemoryMethodSysbench = "sysbench" + MemoryMethodDD = "dd" + MemoryMethodWinsat = "winsat" + + // 硬盘测试方法 + DiskMethodFio = "fio" + DiskMethodDD = "dd" + DiskMethodWinsat = "winsat" + + // 线程模式 + ThreadModeSingle = "single" + ThreadModeMulti = "multi" + + // 语言选项 + LanguageZH = "zh" + LanguageEN = "en" + + // IP检测类型 + CheckTypeIPv4 = "ipv4" + CheckTypeIPv6 = "ipv6" + CheckTypeAuto = "auto" + + // 测速平台 + PlatformCN = "cn" + PlatformNet = "net" + + // 运营商类型 + OperatorCMCC = "cmcc" // 中国移动 + OperatorCU = "cu" // 中国联通 + OperatorCT = "ct" // 中国电信 + OperatorGlobal = "global" // 全球节点 + OperatorOther = "other" // 其他 + OperatorHK = "hk" // 香港 + OperatorTW = "tw" // 台湾 + OperatorJP = "jp" // 日本 + OperatorSG = "sg" // 新加坡 +) diff --git a/api/config.go b/api/config.go new file mode 100644 index 00000000..6369b039 --- /dev/null +++ b/api/config.go @@ -0,0 +1,259 @@ +package api + +import ( + "github.com/oneclickvirt/ecs/internal/params" +) + +// Config 配置接口,导出用于外部调用 +type Config = params.Config + +// NewConfig 创建默认配置 +// version: 版本号字符串 +func NewConfig(version string) *Config { + return params.NewConfig(version) +} + +// NewDefaultConfig 创建默认配置(使用默认版本号) +func NewDefaultConfig() *Config { + return params.NewConfig("v0.1.114") +} + +// ConfigOption 配置选项函数类型 +type ConfigOption func(*Config) + +// WithLanguage 设置语言 +func WithLanguage(lang string) ConfigOption { + return func(c *Config) { + c.Language = lang + } +} + +// WithCpuTestMethod 设置CPU测试方法 +// method: "sysbench" 或 "geekbench" +func WithCpuTestMethod(method string) ConfigOption { + return func(c *Config) { + c.CpuTestMethod = method + } +} + +// WithCpuTestThreadMode 设置CPU测试线程模式 +// mode: "single" 或 "multi" +func WithCpuTestThreadMode(mode string) ConfigOption { + return func(c *Config) { + c.CpuTestThreadMode = mode + } +} + +// WithMemoryTestMethod 设置内存测试方法 +// method: "stream", "sysbench", "dd" +func WithMemoryTestMethod(method string) ConfigOption { + return func(c *Config) { + c.MemoryTestMethod = method + } +} + +// WithDiskTestMethod 设置硬盘测试方法 +// method: "fio" 或 "dd" +func WithDiskTestMethod(method string) ConfigOption { + return func(c *Config) { + c.DiskTestMethod = method + } +} + +// WithDiskTestPath 设置硬盘测试路径 +func WithDiskTestPath(path string) ConfigOption { + return func(c *Config) { + c.DiskTestPath = path + } +} + +// WithDiskMultiCheck 设置是否进行硬盘多路径检测 +func WithDiskMultiCheck(enable bool) ConfigOption { + return func(c *Config) { + c.DiskMultiCheck = enable + } +} + +// WithSpeedTestNum 设置测速节点数量 +func WithSpeedTestNum(num int) ConfigOption { + return func(c *Config) { + c.SpNum = num + } +} + +// WithWidth 设置输出宽度 +func WithWidth(width int) ConfigOption { + return func(c *Config) { + c.Width = width + } +} + +// WithFilePath 设置输出文件路径 +func WithFilePath(path string) ConfigOption { + return func(c *Config) { + c.FilePath = path + } +} + +// WithEnableUpload 设置是否启用上传 +func WithEnableUpload(enable bool) ConfigOption { + return func(c *Config) { + c.EnableUpload = enable + } +} + +// WithEnableLogger 设置是否启用日志 +func WithEnableLogger(enable bool) ConfigOption { + return func(c *Config) { + c.EnableLogger = enable + } +} + +// WithBasicTest 设置是否执行基础信息测试 +func WithBasicTest(enable bool) ConfigOption { + return func(c *Config) { + c.BasicStatus = enable + } +} + +// WithCpuTest 设置是否执行CPU测试 +func WithCpuTest(enable bool) ConfigOption { + return func(c *Config) { + c.CpuTestStatus = enable + } +} + +// WithMemoryTest 设置是否执行内存测试 +func WithMemoryTest(enable bool) ConfigOption { + return func(c *Config) { + c.MemoryTestStatus = enable + } +} + +// WithDiskTest 设置是否执行硬盘测试 +func WithDiskTest(enable bool) ConfigOption { + return func(c *Config) { + c.DiskTestStatus = enable + } +} + +// WithUnlockTest 设置是否执行流媒体解锁测试 +func WithUnlockTest(enable bool) ConfigOption { + return func(c *Config) { + c.UtTestStatus = enable + } +} + +// WithSecurityTest 设置是否执行IP质量测试 +func WithSecurityTest(enable bool) ConfigOption { + return func(c *Config) { + c.SecurityTestStatus = enable + } +} + +// WithEmailTest 设置是否执行邮件端口测试 +func WithEmailTest(enable bool) ConfigOption { + return func(c *Config) { + c.EmailTestStatus = enable + } +} + +// WithBacktraceTest 设置是否执行回程路由测试 +func WithBacktraceTest(enable bool) ConfigOption { + return func(c *Config) { + c.BacktraceStatus = enable + } +} + +// WithNt3Test 设置是否执行三网路由测试 +func WithNt3Test(enable bool) ConfigOption { + return func(c *Config) { + c.Nt3Status = enable + } +} + +// WithSpeedTest 设置是否执行测速测试 +func WithSpeedTest(enable bool) ConfigOption { + return func(c *Config) { + c.SpeedTestStatus = enable + } +} + +// WithPingTest 设置是否执行PING测试 +func WithPingTest(enable bool) ConfigOption { + return func(c *Config) { + c.PingTestStatus = enable + } +} + +// WithTgdcTest 设置是否执行Telegram DC测试 +func WithTgdcTest(enable bool) ConfigOption { + return func(c *Config) { + c.TgdcTestStatus = enable + } +} + +// WithWebTest 设置是否执行网站测试 +func WithWebTest(enable bool) ConfigOption { + return func(c *Config) { + c.WebTestStatus = enable + } +} + +// WithNt3CheckType 设置三网路由检测类型 +// checkType: "ipv4", "ipv6" 或 "auto" +func WithNt3CheckType(checkType string) ConfigOption { + return func(c *Config) { + c.Nt3CheckType = checkType + } +} + +// WithNt3Location 设置三网路由检测位置 +func WithNt3Location(location string) ConfigOption { + return func(c *Config) { + c.Nt3Location = location + } +} + +// WithAutoChangeDiskMethod 设置是否自动切换硬盘测试方法 +func WithAutoChangeDiskMethod(enable bool) ConfigOption { + return func(c *Config) { + c.AutoChangeDiskMethod = enable + } +} + +// WithOnlyChinaTest 设置是否只进行国内测试 +func WithOnlyChinaTest(enable bool) ConfigOption { + return func(c *Config) { + c.OnlyChinaTest = enable + } +} + +// WithMenuMode 设置是否启用菜单模式 +func WithMenuMode(enable bool) ConfigOption { + return func(c *Config) { + c.MenuMode = enable + } +} + +// WithOnlyIpInfoCheck 设置是否只进行IP信息检测 +func WithOnlyIpInfoCheck(enable bool) ConfigOption { + return func(c *Config) { + c.OnlyIpInfoCheck = enable + } +} + +// WithChoice 设置菜单选择 +func WithChoice(choice string) ConfigOption { + return func(c *Config) { + c.Choice = choice + } +} + +// ApplyOptions 应用配置选项 +func ApplyOptions(config *Config, options ...ConfigOption) *Config { + for _, opt := range options { + opt(config) + } + return config +} diff --git a/api/menu.go b/api/menu.go new file mode 100644 index 00000000..27160e88 --- /dev/null +++ b/api/menu.go @@ -0,0 +1,27 @@ +package api + +import ( + "github.com/oneclickvirt/ecs/internal/menu" + "github.com/oneclickvirt/ecs/utils" +) + +// GetMenuChoice 获取用户菜单选择 +// language: 语言 ("zh" 或 "en") +// 返回: 用户选择的选项 +func GetMenuChoice(language string) string { + return menu.GetMenuChoice(language) +} + +// PrintMenuOptions 打印菜单选项 +// preCheck: 网络检查结果 +// config: 配置对象 +func PrintMenuOptions(preCheck utils.NetCheckResult, config *Config) { + menu.PrintMenuOptions(preCheck, config) +} + +// HandleMenuMode 处理菜单模式 +// preCheck: 网络检查结果 +// config: 配置对象 +func HandleMenuMode(preCheck utils.NetCheckResult, config *Config) { + menu.HandleMenuMode(preCheck, config) +} diff --git a/api/runner.go b/api/runner.go new file mode 100644 index 00000000..fd8f4f8d --- /dev/null +++ b/api/runner.go @@ -0,0 +1,187 @@ +package api + +import ( + "sync" + "time" + + "github.com/oneclickvirt/ecs/internal/runner" + "github.com/oneclickvirt/ecs/utils" +) + +// RunResult 运行结果 +type RunResult struct { + Output string // 完整输出 + Duration time.Duration // 运行时长 + StartTime time.Time // 开始时间 + EndTime time.Time // 结束时间 +} + +// RunAllTests 执行所有测试(高级接口) +// preCheck: 网络检查结果 +// config: 配置对象 +// 返回: 运行结果 +func RunAllTests(preCheck utils.NetCheckResult, config *Config) *RunResult { + var ( + wg1, wg2, wg3 sync.WaitGroup + basicInfo, securityInfo, emailInfo, mediaInfo, ptInfo string + output, tempOutput string + outputMutex sync.Mutex + infoMutex sync.Mutex + ) + + startTime := time.Now() + + switch config.Language { + case "zh": + runner.RunChineseTests(preCheck, config, &wg1, &wg2, &wg3, + &basicInfo, &securityInfo, &emailInfo, &mediaInfo, &ptInfo, + &output, tempOutput, startTime, &outputMutex, &infoMutex) + case "en": + runner.RunEnglishTests(preCheck, config, &wg1, &wg2, &wg3, + &basicInfo, &securityInfo, &emailInfo, &mediaInfo, &ptInfo, + &output, tempOutput, startTime, &outputMutex, &infoMutex) + default: + runner.RunChineseTests(preCheck, config, &wg1, &wg2, &wg3, + &basicInfo, &securityInfo, &emailInfo, &mediaInfo, &ptInfo, + &output, tempOutput, startTime, &outputMutex, &infoMutex) + } + + endTime := time.Now() + + return &RunResult{ + Output: output, + Duration: endTime.Sub(startTime), + StartTime: startTime, + EndTime: endTime, + } +} + +// RunBasicTests 运行基础信息测试 +func RunBasicTests(preCheck utils.NetCheckResult, config *Config) string { + var ( + basicInfo, securityInfo string + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunBasicTests(preCheck, config, &basicInfo, &securityInfo, output, tempOutput, &outputMutex) +} + +// RunCPUTest 运行CPU测试 +func RunCPUTest(config *Config) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunCPUTest(config, output, tempOutput, &outputMutex) +} + +// RunMemoryTest 运行内存测试 +func RunMemoryTest(config *Config) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunMemoryTest(config, output, tempOutput, &outputMutex) +} + +// RunDiskTest 运行硬盘测试 +func RunDiskTest(config *Config) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunDiskTest(config, output, tempOutput, &outputMutex) +} + +// RunIpInfoCheck 执行IP信息检测 +func RunIpInfoCheck(config *Config) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunIpInfoCheck(config, output, tempOutput, &outputMutex) +} + +// RunStreamingTests 运行流媒体测试 +func RunStreamingTests(config *Config, mediaInfo string) string { + var ( + wg1 sync.WaitGroup + output, tempOutput string + outputMutex sync.Mutex + infoMutex sync.Mutex + ) + return runner.RunStreamingTests(config, &wg1, &mediaInfo, output, tempOutput, &outputMutex, &infoMutex) +} + +// RunSecurityTests 运行安全测试 +func RunSecurityTests(config *Config, securityInfo string) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunSecurityTests(config, securityInfo, output, tempOutput, &outputMutex) +} + +// RunEmailTests 运行邮件端口测试 +func RunEmailTests(config *Config, emailInfo string) string { + var ( + wg2 sync.WaitGroup + output, tempOutput string + outputMutex sync.Mutex + infoMutex sync.Mutex + ) + return runner.RunEmailTests(config, &wg2, &emailInfo, output, tempOutput, &outputMutex, &infoMutex) +} + +// RunNetworkTests 运行网络测试(中文模式) +func RunNetworkTests(config *Config, ptInfo string) string { + var ( + wg3 sync.WaitGroup + output, tempOutput string + outputMutex sync.Mutex + infoMutex sync.Mutex + ) + return runner.RunNetworkTests(config, &wg3, &ptInfo, output, tempOutput, &outputMutex, &infoMutex) +} + +// RunSpeedTests 运行测速测试(中文模式) +func RunSpeedTests(config *Config) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunSpeedTests(config, output, tempOutput, &outputMutex) +} + +// RunEnglishNetworkTests 运行网络测试(英文模式) +func RunEnglishNetworkTests(config *Config, ptInfo string) string { + var ( + wg3 sync.WaitGroup + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunEnglishNetworkTests(config, &wg3, &ptInfo, output, tempOutput, &outputMutex) +} + +// RunEnglishSpeedTests 运行测速测试(英文模式) +func RunEnglishSpeedTests(config *Config) string { + var ( + output, tempOutput string + outputMutex sync.Mutex + ) + return runner.RunEnglishSpeedTests(config, output, tempOutput, &outputMutex) +} + +// AppendTimeInfo 添加时间信息 +func AppendTimeInfo(config *Config, output string, startTime time.Time) string { + var ( + tempOutput string + outputMutex sync.Mutex + ) + return runner.AppendTimeInfo(config, output, tempOutput, startTime, &outputMutex) +} + +// HandleUploadResults 处理上传结果 +func HandleUploadResults(config *Config, output string) { + runner.HandleUploadResults(config, output) +} diff --git a/api/tests.go b/api/tests.go new file mode 100644 index 00000000..700aac2a --- /dev/null +++ b/api/tests.go @@ -0,0 +1,101 @@ +package api + +import ( + "github.com/oneclickvirt/ecs/internal/tests" +) + +// TestResult 测试结果结构 +type TestResult struct { + TestMethod string // 实际使用的测试方法 + Output string // 测试输出结果 + Success bool // 是否成功 + Error error // 错误信息 +} + +// CpuTest CPU测试公共接口 +// language: 语言 ("zh" 或 "en") +// testMethod: 测试方法 ("sysbench" 或 "geekbench") +// testThread: 线程模式 ("single" 或 "multi") +// 返回: (实际测试方法, 测试结果) +func CpuTest(language, testMethod, testThread string) (string, string) { + return tests.CpuTest(language, testMethod, testThread) +} + +// MemoryTest 内存测试公共接口 +// language: 语言 ("zh" 或 "en") +// testMethod: 测试方法 ("stream", "sysbench", "dd") +// 返回: (实际测试方法, 测试结果) +func MemoryTest(language, testMethod string) (string, string) { + return tests.MemoryTest(language, testMethod) +} + +// DiskTest 硬盘测试公共接口 +// language: 语言 ("zh" 或 "en") +// testMethod: 测试方法 ("fio" 或 "dd") +// testPath: 测试路径 +// isMultiCheck: 是否多路径检测 +// autoChange: 是否自动切换方法 +// 返回: (实际测试方法, 测试结果) +func DiskTest(language, testMethod, testPath string, isMultiCheck, autoChange bool) (string, string) { + return tests.DiskTest(language, testMethod, testPath, isMultiCheck, autoChange) +} + +// MediaTest 流媒体解锁测试公共接口 +// language: 语言 ("zh" 或 "en") +// 返回: 测试结果 +func MediaTest(language string) string { + return tests.MediaTest(language) +} + +// SpeedTestShowHead 显示测速表头 +// language: 语言 ("zh" 或 "en") +func SpeedTestShowHead(language string) { + tests.ShowHead(language) +} + +// SpeedTestNearby 就近节点测速 +func SpeedTestNearby() { + tests.NearbySP() +} + +// SpeedTestCustom 自定义测速 +// platform: 平台 ("cn" 或 "net") +// operator: 运营商 ("cmcc", "cu", "ct", "global", "other" 等) +// num: 测试节点数量 +// language: 语言 ("zh" 或 "en") +func SpeedTestCustom(platform, operator string, num int, language string) { + tests.CustomSP(platform, operator, num, language) +} + +// NextTrace3Check 三网路由追踪测试 +// language: 语言 ("zh" 或 "en") +// location: 位置 +// checkType: 检测类型 ("ipv4", "ipv6") +func NextTrace3Check(language, location, checkType string) { + tests.NextTrace3Check(language, location, checkType) +} + +// UpstreamsCheck 上游及回程线路检测 +func UpstreamsCheck() { + tests.UpstreamsCheck() +} + +// GetIPv4Address 获取当前IPv4地址 +func GetIPv4Address() string { + return tests.IPV4 +} + +// GetIPv6Address 获取当前IPv6地址 +func GetIPv6Address() string { + return tests.IPV6 +} + +// SetIPv4Address 设置IPv4地址(用于测试) +func SetIPv4Address(ipv4 string) { + tests.IPV4 = ipv4 +} + +// SetIPv6Address 设置IPv6地址(用于测试) +func SetIPv6Address(ipv6 string) { + tests.IPV6 = ipv6 +} diff --git a/api/utils.go b/api/utils.go new file mode 100644 index 00000000..946579ff --- /dev/null +++ b/api/utils.go @@ -0,0 +1,91 @@ +package api + +import ( + "time" + + "github.com/oneclickvirt/ecs/utils" +) + +// NetCheckResult 网络检查结果 +type NetCheckResult = utils.NetCheckResult + +// StatsResponse 统计信息响应 +type StatsResponse = utils.StatsResponse + +// GitHubRelease GitHub发布信息 +type GitHubRelease = utils.GitHubRelease + +// CheckPublicAccess 检查公网访问能力 +// timeout: 超时时间 +// 返回: 网络检查结果 +func CheckPublicAccess(timeout time.Duration) NetCheckResult { + return utils.CheckPublicAccess(timeout) +} + +// GetGoescStats 获取goecs统计信息 +// 返回: (统计响应, 错误) +func GetGoescStats() (*StatsResponse, error) { + return utils.GetGoescStats() +} + +// GetLatestEcsRelease 获取最新的ECS版本信息 +// 返回: (GitHub发布信息, 错误) +func GetLatestEcsRelease() (*GitHubRelease, error) { + return utils.GetLatestEcsRelease() +} + +// PrintHead 打印程序头部信息 +// language: 语言 ("zh" 或 "en") +// width: 显示宽度 +// version: 版本号 +func PrintHead(language string, width int, version string) { + utils.PrintHead(language, width, version) +} + +// PrintCenteredTitle 打印居中标题 +// title: 标题文本 +// width: 显示宽度 +func PrintCenteredTitle(title string, width int) { + utils.PrintCenteredTitle(title, width) +} + +// ProcessAndUpload 处理并上传结果 +// output: 输出内容 +// filePath: 文件路径 +// enableUpload: 是否启用上传 +// 返回: (HTTP URL, HTTPS URL) +func ProcessAndUpload(output, filePath string, enableUpload bool) (string, string) { + return utils.ProcessAndUpload(output, filePath, enableUpload) +} + +// BasicsAndSecurityCheck 基础信息和安全检查 +// language: 语言 +// checkType: 检查类型 +// securityTestStatus: 是否执行安全测试 +// 返回: (IPv4地址, IPv6地址, 基础信息, 安全信息, 检查类型) +func BasicsAndSecurityCheck(language, checkType string, securityTestStatus bool) (string, string, string, string, string) { + return utils.BasicsAndSecurityCheck(language, checkType, securityTestStatus) +} + +// OnlyBasicsIpInfo 仅获取基础IP信息 +// language: 语言 +// 返回: (IPv4地址, IPv6地址, IP信息) +func OnlyBasicsIpInfo(language string) (string, string, string) { + return utils.OnlyBasicsIpInfo(language) +} + +// FormatGoecsNumber 格式化数字显示 +// num: 数字 +// 返回: 格式化后的字符串 +func FormatGoecsNumber(num int) string { + return utils.FormatGoecsNumber(num) +} + +// PrintAndCapture 打印并捕获输出 +// fn: 执行的函数 +// tempOutput: 临时输出 +// existingOutput: 现有输出 +// 返回: 捕获的输出 +func PrintAndCapture(fn func(), tempOutput, existingOutput string) string { + return utils.PrintAndCapture(fn, tempOutput, existingOutput) +}