mirror of
https://github.com/oneclickvirt/basics.git
synced 2025-10-07 17:41:54 +08:00
v0.0.4 - 更新适配MAC系统和类BSD系统
This commit is contained in:
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
@@ -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:
|
||||
|
@@ -10,6 +10,7 @@ Include: https://github.com/oneclickvirt/gostun
|
||||
|
||||
- [x] 以```-l```指定输出的语言类型,可指定```zh```或```en```,默认不指定时使用中文输出
|
||||
- [x] 使用```sysctl```获取CPU信息-特化适配freebsd、openbsd系统
|
||||
- [x] 适配```MacOS```与```Windows```系统的信息查询
|
||||
|
||||
## TODO
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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" {
|
||||
|
24
system/macos.go
Normal file
24
system/macos.go
Normal 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")
|
||||
}
|
||||
}
|
@@ -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()
|
||||
|
@@ -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"
|
||||
|
Reference in New Issue
Block a user