v0.0.4 - 更新适配MAC系统和类BSD系统

This commit is contained in:
spiritlhl
2024-07-01 15:58:47 +00:00
parent ee06b25ec7
commit d2e8e4c1fa
8 changed files with 101 additions and 24 deletions

View File

@@ -24,7 +24,7 @@ jobs:
run: | run: |
git config --global user.name 'github-actions' git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com' 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 tag $TAG
git push origin $TAG git push origin $TAG
env: env:

View File

@@ -10,6 +10,7 @@ Include: https://github.com/oneclickvirt/gostun
- [x] 以```-l```指定输出的语言类型,可指定```zh```或```en```,默认不指定时使用中文输出 - [x] 以```-l```指定输出的语言类型,可指定```zh```或```en```,默认不指定时使用中文输出
- [x] 使用```sysctl```获取CPU信息-特化适配freebsd、openbsd系统 - [x] 使用```sysctl```获取CPU信息-特化适配freebsd、openbsd系统
- [x] 适配```MacOS```与```Windows```系统的信息查询
## TODO ## TODO

View File

@@ -1,9 +1,14 @@
package model package model
const BasicsVersion = "v0.0.3" const BasicsVersion = "v0.0.4"
var EnableLoger bool var EnableLoger bool
var (
IsMacOS bool
MacOSInfo []string
)
type IpInfo struct { type IpInfo struct {
Ip string Ip string
ASN string ASN string

View File

@@ -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 系统 // 使用 sysctl 获取信息 - 特化适配 freebsd openbsd 系统
if checkSysctlVersion() { if checkSysctlVersion() {
if ret.CpuModel == "" { if ret.CpuModel == "" {
@@ -241,25 +261,21 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error
} }
} }
} }
// 使用 /proc/device-tree 获取信息 - 特化适配嵌入式系统 // MAC需要额外获取信息进行判断
deviceTreeContent, err := os.ReadFile("/proc/device-tree") if model.IsMacOS {
if err == nil { if len(model.MacOSInfo) > 0 {
ret.CpuModel = string(deviceTreeContent) 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])
}
} }
// 获取虚拟化架构
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")
} }
return ret, nil return ret, nil
} }

View File

@@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/libp2p/go-nat" "github.com/libp2p/go-nat"
"github.com/oneclickvirt/basics/model"
"github.com/oneclickvirt/basics/system/utils" "github.com/oneclickvirt/basics/system/utils"
"github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/host"
) )
@@ -115,6 +116,16 @@ func getHostInfo() (string, string, string, string, string, string, string, stri
if VmType == "" && runtime.GOOS == "windows" { if VmType == "" && runtime.GOOS == "windows" {
VmType = utils.CheckVMTypeWithWIMC() 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类型 // 查询NAT类型
NatType = getNatType() NatType = getNatType()
if NatType == "Inconclusive" { if NatType == "Inconclusive" {

24
system/macos.go Normal file
View File

@@ -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")
}
}

View File

@@ -1,11 +1,13 @@
package system package system
import ( import (
"github.com/shirou/gopsutil/mem"
"os" "os"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"github.com/oneclickvirt/basics/model"
"github.com/shirou/gopsutil/mem"
) )
func getMemoryInfo() (string, string, string, string, string, string) { 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" { if runtime.GOOS == "windows" {
// gopsutil 在 Windows 下不能正确取 swap // gopsutil 在 Windows 下不能正确取 swap
ms, err := mem.SwapMemory() ms, err := mem.SwapMemory()

View File

@@ -19,6 +19,10 @@ var (
// GetSystemInfo 获取主机硬件信息 // GetSystemInfo 获取主机硬件信息
func GetSystemInfo() *model.SystemInfo { func GetSystemInfo() *model.SystemInfo {
var ret = &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() cpuType, ret.Uptime, ret.Platform, ret.Kernel, ret.Arch, ret.VmType, ret.NatType, ret.TimeZone, _ = getHostInfo()
// CPU信息查询 // CPU信息查询
@@ -71,7 +75,9 @@ func CheckSystemInfo(language string) string {
} else { } else {
res += "\n" res += "\n"
} }
if ret.BootPath != "" {
res += " Boot Path : " + ret.BootPath + "\n" res += " Boot Path : " + ret.BootPath + "\n"
}
res += " OS Release : " + ret.Platform + " [" + ret.Arch + "] " + "\n" res += " OS Release : " + ret.Platform + " [" + ret.Arch + "] " + "\n"
if ret.Kernel != "" { if ret.Kernel != "" {
res += " Kernel : " + ret.Kernel + "\n" res += " Kernel : " + ret.Kernel + "\n"
@@ -112,7 +118,9 @@ func CheckSystemInfo(language string) string {
} else { } else {
res += "\n" res += "\n"
} }
if ret.BootPath != "" {
res += " 启动盘路径 : " + ret.BootPath + "\n" res += " 启动盘路径 : " + ret.BootPath + "\n"
}
res += " 系统 : " + ret.Platform + " [" + ret.Arch + "] " + "\n" res += " 系统 : " + ret.Platform + " [" + ret.Arch + "] " + "\n"
if ret.Kernel != "" { if ret.Kernel != "" {
res += " 内核 : " + ret.Kernel + "\n" res += " 内核 : " + ret.Kernel + "\n"