初始化项目

This commit is contained in:
BBIT-Kai
2026-05-26 13:53:23 +08:00
commit 7e803e2cdb
27 changed files with 1820 additions and 0 deletions
+40
View File
@@ -0,0 +1,40 @@
package device
import (
"net"
"os"
"strings"
)
// GetDeviceID 返回本机原始唯一IDLinux /etc/machine-id 或 hostname+MAC
func GetDeviceID() string {
// 尝试读取 Linux /etc/machine-id
if data, err := os.ReadFile("/etc/machine-id"); err == nil {
s := strings.TrimSpace(string(data))
if s != "" {
return s
}
}
// fallback: hostname + first non零MAC
hn, _ := os.Hostname()
mac := getFirstMac()
return hn + "|" + mac
}
func getFirstMac() string {
ifaces, err := net.Interfaces()
if err != nil {
return ""
}
for _, it := range ifaces {
if len(it.HardwareAddr) == 0 {
continue
}
mac := it.HardwareAddr.String()
if mac != "" && mac != "00:00:00:00:00:00" {
return mac
}
}
return ""
}