fix(sdk): 离线 license 默认自动校验机器指纹
- 新增 sdk.MachineFingerprint:Linux machine-id+DMI、macOS IOPlatformUUID,容器内 fail-closed - 文件模式在 bind.fp 存在时自动校验,无需显式 WithFingerprint;采集失败拒绝绑定 license - 新增 WithFingerprintFunc 支持容器读取挂载宿主 machine-id;在线激活同步使用自动指纹 - license 包新增 NormalizeFingerprint(sha256:64hex 校验与小写归一化) - 新增指纹绑定/采集失败/无绑定跳过等测试
This commit is contained in:
10 files changed
+433
-21
No files matched your search
@@ -0,0 +1,104 @@
|
||||
//go:build linux
|
||||
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// machineFields Linux 采集:machine-id + DMI product_uuid;
|
||||
// 两者都不可用时退回首个物理网卡 MAC,最后退回 hostname。
|
||||
func machineFields() ([]string, error) {
|
||||
if inContainer() {
|
||||
return nil, errors.New("sdk: 检测到容器环境,容器自身 ID 不稳定;请显式 WithFingerprint,或用 WithFingerprintFunc 读取挂载的宿主 /etc/machine-id(若已挂载宿主 ID,可设置 EF_FINGERPRINT_ALLOW_CONTAINER=1 放行)")
|
||||
}
|
||||
|
||||
var fields []string
|
||||
if v := readTrimmed("/etc/machine-id"); v != "" {
|
||||
fields = append(fields, "machine-id:"+v)
|
||||
} else if v := readTrimmed("/var/lib/dbus/machine-id"); v != "" {
|
||||
fields = append(fields, "machine-id:"+v)
|
||||
}
|
||||
if v := readTrimmed("/sys/class/dmi/id/product_uuid"); v != "" && !junkUUID(v) {
|
||||
fields = append(fields, "product-uuid:"+strings.ToLower(v))
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
if mac := firstPhysicalMAC(); mac != "" {
|
||||
fields = append(fields, "mac:"+mac)
|
||||
}
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
if h := hostname(); h != "" && h != "unknown" {
|
||||
fields = append(fields, "hostname:"+h)
|
||||
}
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
return nil, errors.New("sdk: Linux 上未找到 machine-id/DMI/MAC/hostname 任一稳定标识")
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
// inContainer 判断常见容器运行时;显式放行环境变量供"宿主 machine-id 已挂载"场景使用。
|
||||
func inContainer() bool {
|
||||
if os.Getenv("EF_FINGERPRINT_ALLOW_CONTAINER") == "1" {
|
||||
return false
|
||||
}
|
||||
if _, err := os.Stat("/.dockerenv"); err == nil {
|
||||
return true
|
||||
}
|
||||
if _, err := os.Stat("/run/.containerenv"); err == nil {
|
||||
return true
|
||||
}
|
||||
if data, err := os.ReadFile("/proc/1/cgroup"); err == nil {
|
||||
text := string(data)
|
||||
for _, marker := range []string{"docker", "kubepods", "containerd", "libpod"} {
|
||||
if strings.Contains(text, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// junkUUID 过滤全零/全 F 一类无效 DMI UUID。
|
||||
func junkUUID(v string) bool {
|
||||
s := strings.ReplaceAll(strings.ToLower(v), "-", "")
|
||||
if len(s) < 16 {
|
||||
return true
|
||||
}
|
||||
return strings.Trim(s, "0") == "" || strings.Trim(s, "f") == ""
|
||||
}
|
||||
|
||||
// firstPhysicalMAC 返回首个非回环、非全零的 MAC(小写,带冒号)。
|
||||
func firstPhysicalMAC() string {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, iface := range ifaces {
|
||||
if iface.Flags&net.FlagLoopback != 0 {
|
||||
continue
|
||||
}
|
||||
mac := iface.HardwareAddr.String()
|
||||
if mac == "" {
|
||||
continue
|
||||
}
|
||||
compact := strings.ReplaceAll(mac, ":", "")
|
||||
if strings.Trim(compact, "0") == "" {
|
||||
continue
|
||||
}
|
||||
return strings.ToLower(mac)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func readTrimmed(path string) string {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
Reference in new issue
Block a user