//go:build darwin // machineid_darwin.go — macOS machineID 取得。 // // 來源:`ioreg -rd1 -c IOPlatformExpertDevice` 輸出中的 `IOPlatformUUID`。 // 此 UUID 綁定機器硬體 serial 派生,使用者空間可讀,無需 sudo。 // 參考 ADR-009。 package tunnel import ( "bufio" "fmt" "os/exec" "strings" ) // readMachineID 回傳 macOS 的 IOPlatformUUID。 // 失敗時回 ("", error)——呼叫端應 fallback 到 salt file。 func readMachineID() (string, error) { cmd := exec.Command("ioreg", "-rd1", "-c", "IOPlatformExpertDevice") out, err := cmd.Output() if err != nil { return "", fmt.Errorf("ioreg failed: %w", err) } scanner := bufio.NewScanner(strings.NewReader(string(out))) for scanner.Scan() { line := scanner.Text() // 格式:` "IOPlatformUUID" = "12345678-90AB-CDEF-1234-567890ABCDEF"` if idx := strings.Index(line, "IOPlatformUUID"); idx >= 0 { // 抓兩個雙引號之間的值 rest := line[idx+len("IOPlatformUUID"):] // 找 `"value"` 模式 first := strings.Index(rest, "\"") if first < 0 { continue } rest = rest[first+1:] second := strings.Index(rest, "\"") if second < 0 { continue } uuid := strings.TrimSpace(rest[:second]) if uuid != "" { return uuid, nil } } } return "", fmt.Errorf("IOPlatformUUID not found in ioreg output") }