From d2e8e4c1fa87fc34f17ae9c6df2abd1c17087dcb Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Mon, 1 Jul 2024 15:58:47 +0000 Subject: [PATCH] =?UTF-8?q?v0.0.4=20-=20=E6=9B=B4=E6=96=B0=E9=80=82?= =?UTF-8?q?=E9=85=8DMAC=E7=B3=BB=E7=BB=9F=E5=92=8C=E7=B1=BBBSD=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yaml | 2 +- README.md | 1 + model/model.go | 7 ++++- system/cpu.go | 54 +++++++++++++++++++++++++-------------- system/host.go | 11 ++++++++ system/macos.go | 24 +++++++++++++++++ system/memory.go | 14 +++++++++- system/system.go | 12 +++++++-- 8 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 system/macos.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7f1ab37..f617fab 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,7 +24,7 @@ jobs: run: | git config --global user.name 'github-actions' git config --global user.email 'github-actions@github.com' - TAG="v0.0.3-$(date +'%Y%m%d%H%M%S')" + TAG="v0.0.4-$(date +'%Y%m%d%H%M%S')" git tag $TAG git push origin $TAG env: diff --git a/README.md b/README.md index dd58c98..024d067 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Include: https://github.com/oneclickvirt/gostun - [x] 以```-l```指定输出的语言类型,可指定```zh```或```en```,默认不指定时使用中文输出 - [x] 使用```sysctl```获取CPU信息-特化适配freebsd、openbsd系统 +- [x] 适配```MacOS```与```Windows```系统的信息查询 ## TODO diff --git a/model/model.go b/model/model.go index bd221dc..f1b90f8 100644 --- a/model/model.go +++ b/model/model.go @@ -1,9 +1,14 @@ package model -const BasicsVersion = "v0.0.3" +const BasicsVersion = "v0.0.4" var EnableLoger bool +var ( + IsMacOS bool + MacOSInfo []string +) + type IpInfo struct { Ip string ASN string diff --git a/system/cpu.go b/system/cpu.go index afb9f47..891fb1e 100644 --- a/system/cpu.go +++ b/system/cpu.go @@ -145,6 +145,26 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error } } } + // 使用 /proc/device-tree 获取信息 - 特化适配嵌入式系统 + deviceTreeContent, err := os.ReadFile("/proc/device-tree") + if err == nil { + ret.CpuModel = string(deviceTreeContent) + } + // 获取虚拟化架构 + if runtime.GOOS == "windows" { + aesFeature = `HARDWARE\DESCRIPTION\System\CentralProcessor\0` + virtFeature = `HARDWARE\DESCRIPTION\System\CentralProcessor\0` + hypervFeature = `SYSTEM\CurrentControlSet\Control\Hypervisor\0` + } else if runtime.GOOS == "linux" { + aesFeature = "/proc/cpuinfo" + virtFeature = "/proc/cpuinfo" + hypervFeature = "/proc/cpuinfo" + } + ret.CpuAesNi, _ = checkCPUFeature(aesFeature, "aes") + ret.CpuVAH, st = checkCPUFeature(virtFeature, "vmx") + if !st { + ret.CpuVAH, _ = checkCPUFeature(hypervFeature, "hypervisor") + } // 使用 sysctl 获取信息 - 特化适配 freebsd openbsd 系统 if checkSysctlVersion() { if ret.CpuModel == "" { @@ -241,25 +261,21 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error } } } - // 使用 /proc/device-tree 获取信息 - 特化适配嵌入式系统 - deviceTreeContent, err := os.ReadFile("/proc/device-tree") - if err == nil { - ret.CpuModel = string(deviceTreeContent) - } - // 获取虚拟化架构 - if runtime.GOOS == "windows" { - aesFeature = `HARDWARE\DESCRIPTION\System\CentralProcessor\0` - virtFeature = `HARDWARE\DESCRIPTION\System\CentralProcessor\0` - hypervFeature = `SYSTEM\CurrentControlSet\Control\Hypervisor\0` - } else if runtime.GOOS == "linux" { - aesFeature = "/proc/cpuinfo" - virtFeature = "/proc/cpuinfo" - hypervFeature = "/proc/cpuinfo" - } - ret.CpuAesNi, _ = checkCPUFeature(aesFeature, "aes") - ret.CpuVAH, st = checkCPUFeature(virtFeature, "vmx") - if !st { - ret.CpuVAH, _ = checkCPUFeature(hypervFeature, "hypervisor") + // MAC需要额外获取信息进行判断 + if model.IsMacOS { + if len(model.MacOSInfo) > 0 { + for _, line := range model.MacOSInfo { + if strings.Contains(line, "Chip") { + ret.CpuModel = strings.TrimSpace(strings.Split(line, ":")[1]) + } + if strings.Contains(line, "Total Number of Cores") { + ret.CpuCores = strings.TrimSpace(strings.Split(line, ":")[1]) + } + if strings.Contains(line, "Memory") { + ret.MemoryTotal = strings.TrimSpace(strings.Split(line, ":")[1]) + } + } + } } return ret, nil } diff --git a/system/host.go b/system/host.go index fdac64f..cd414fc 100644 --- a/system/host.go +++ b/system/host.go @@ -10,6 +10,7 @@ import ( "time" "github.com/libp2p/go-nat" + "github.com/oneclickvirt/basics/model" "github.com/oneclickvirt/basics/system/utils" "github.com/shirou/gopsutil/host" ) @@ -115,6 +116,16 @@ func getHostInfo() (string, string, string, string, string, string, string, stri if VmType == "" && runtime.GOOS == "windows" { VmType = utils.CheckVMTypeWithWIMC() } + // MAC需要额外获取信息进行判断 + if model.IsMacOS { + if len(model.MacOSInfo) > 0 { + for _, line := range model.MacOSInfo { + if strings.Contains(line, "Model Name") { + VmType = strings.TrimSpace(strings.Split(line, ":")[1]) + } + } + } + } // 查询NAT类型 NatType = getNatType() if NatType == "Inconclusive" { diff --git a/system/macos.go b/system/macos.go new file mode 100644 index 0000000..c20c33d --- /dev/null +++ b/system/macos.go @@ -0,0 +1,24 @@ +package system + +import ( + "os/exec" + "strings" + + "github.com/oneclickvirt/basics/model" +) + +func isMacOS() bool { + out, err := exec.Command("uname", "-a").Output() + if err != nil { + return false + } + systemName := strings.ToLower(string(out)) + return strings.Contains(systemName, "darwin") +} + +func getMacOSInfo() { + out, err := exec.Command("system_profiler", "SPHardwareDataType").Output() + if err == nil && !strings.Contains(string(out), "error") { + model.MacOSInfo = strings.Split(string(out), "\n") + } +} diff --git a/system/memory.go b/system/memory.go index cf0440b..d5af93c 100644 --- a/system/memory.go +++ b/system/memory.go @@ -1,11 +1,13 @@ package system import ( - "github.com/shirou/gopsutil/mem" "os" "runtime" "strconv" "strings" + + "github.com/oneclickvirt/basics/model" + "github.com/shirou/gopsutil/mem" ) func getMemoryInfo() (string, string, string, string, string, string) { @@ -43,6 +45,16 @@ func getMemoryInfo() (string, string, string, string, string, string) { } } } + // MAC需要额外获取信息进行判断 + if model.IsMacOS { + if len(model.MacOSInfo) > 0 { + for _, line := range model.MacOSInfo { + if strings.Contains(line, "Memory") { + memoryTotalStr = strings.TrimSpace(strings.Split(line, ":")[1]) + } + } + } + } if runtime.GOOS == "windows" { // gopsutil 在 Windows 下不能正确取 swap ms, err := mem.SwapMemory() diff --git a/system/system.go b/system/system.go index ad80872..31958be 100644 --- a/system/system.go +++ b/system/system.go @@ -19,6 +19,10 @@ var ( // GetSystemInfo 获取主机硬件信息 func GetSystemInfo() *model.SystemInfo { var ret = &model.SystemInfo{} + model.IsMacOS = isMacOS() + if model.IsMacOS { + getMacOSInfo() + } // 系统信息查询 cpuType, ret.Uptime, ret.Platform, ret.Kernel, ret.Arch, ret.VmType, ret.NatType, ret.TimeZone, _ = getHostInfo() // CPU信息查询 @@ -71,7 +75,9 @@ func CheckSystemInfo(language string) string { } else { res += "\n" } - res += " Boot Path : " + ret.BootPath + "\n" + if ret.BootPath != "" { + res += " Boot Path : " + ret.BootPath + "\n" + } res += " OS Release : " + ret.Platform + " [" + ret.Arch + "] " + "\n" if ret.Kernel != "" { res += " Kernel : " + ret.Kernel + "\n" @@ -112,7 +118,9 @@ func CheckSystemInfo(language string) string { } else { res += "\n" } - res += " 启动盘路径 : " + ret.BootPath + "\n" + if ret.BootPath != "" { + res += " 启动盘路径 : " + ret.BootPath + "\n" + } res += " 系统 : " + ret.Platform + " [" + ret.Arch + "] " + "\n" if ret.Kernel != "" { res += " 内核 : " + ret.Kernel + "\n"