1 Star 0 Fork 1

flyiot/flylibs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
info.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
flyrainning 提交于 2023-03-03 16:52 +08:00 . u
package FlyOS
import (
"os"
"runtime"
"strings"
"gitee.com/flyiot/flylibs/FlyUtils"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
)
// 获取计算机名
func HostName() string {
hostname, err := os.Hostname()
if err != nil {
return ""
}
return hostname
}
// 获取计算机唯一ID
func GetComputerId() string {
str := "cpu_"
if info, err := cpu.Info(); err == nil {
for _, cpu := range info {
str += cpu.VendorID + cpu.ModelName + cpu.Model
}
}
if info, err := host.HostID(); err == nil {
str += info
}
if s1, s2, err := host.Virtualization(); err == nil {
str += s1 + s2
}
str += tryGetCpuChipId()
str = FlyUtils.MD5(str)
return str
}
// 获取应用唯一ID,检测包括当前程序的路径和md5
func GetAPPId() string {
str := GetComputerId()
str += HostName()
if self, err := os.Executable(); err == nil {
str += self
if m, err := FlyUtils.MD5File(self); err == nil {
str += m
}
}
str = FlyUtils.MD5(str)
return str
}
// 尝试获取cpuid,一般用于arm
func tryGetCpuChipId() string {
res := ""
if runtime.GOOS == "linux" {
str := FlyUtils.ReadFile2str("/proc/cpuinfo")
str = strings.ToLower(str)
lines := strings.Split(str, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "chipid") {
res += line
// if index := strings.Index(line, ":"); index > 1 {
// res = line[index+1:]
// }
}
if strings.HasPrefix(line, "serial") {
res += line
}
}
}
return res
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/flyiot/flylibs.git
git@gitee.com:flyiot/flylibs.git
flyiot
flylibs
flylibs
b9c81f1c9857

搜索帮助