|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package machine_id |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/ebitengine/purego" |
| 10 | +) |
| 11 | + |
| 12 | +var _ = fmt.Print |
| 13 | + |
| 14 | +func read_machine_id() (string, error) { |
| 15 | + const kIOMainPortDefault uint32 = 0 |
| 16 | + const kCFStringEncodingUTF8 uint32 = 0x08000100 |
| 17 | + |
| 18 | + // 1. Load System Frameworks |
| 19 | + iokit, err := purego.Dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", purego.RTLD_NOW) |
| 20 | + if err != nil { |
| 21 | + return "", err |
| 22 | + } |
| 23 | + defer purego.Dlclose(iokit) |
| 24 | + corefoundation, err := purego.Dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", purego.RTLD_NOW) |
| 25 | + if err != nil { |
| 26 | + return "", err |
| 27 | + } |
| 28 | + defer purego.Dlclose(corefoundation) |
| 29 | + |
| 30 | + // 2. Register CoreFoundation Functions |
| 31 | + var cfStringCreateWithCString func(uintptr, *byte, uint32) uintptr |
| 32 | + purego.RegisterLibFunc(&cfStringCreateWithCString, corefoundation, "CFStringCreateWithCString") |
| 33 | + |
| 34 | + var cfStringGetCString func(uintptr, *byte, int64, uint32) bool |
| 35 | + purego.RegisterLibFunc(&cfStringGetCString, corefoundation, "CFStringGetCString") |
| 36 | + |
| 37 | + var cfRelease func(uintptr) |
| 38 | + purego.RegisterLibFunc(&cfRelease, corefoundation, "CFRelease") |
| 39 | + |
| 40 | + // 3. Register IOKit Functions |
| 41 | + var ioServiceMatching func(*byte) uintptr |
| 42 | + purego.RegisterLibFunc(&ioServiceMatching, iokit, "IOServiceMatching") |
| 43 | + |
| 44 | + var ioServiceGetMatchingService func(uint32, uintptr) uint32 |
| 45 | + purego.RegisterLibFunc(&ioServiceGetMatchingService, iokit, "IOServiceGetMatchingService") |
| 46 | + |
| 47 | + var ioRegistryEntryCreateCFProperty func(uint32, uintptr, uintptr, uint32) uintptr |
| 48 | + purego.RegisterLibFunc(&ioRegistryEntryCreateCFProperty, iokit, "IORegistryEntryCreateCFProperty") |
| 49 | + |
| 50 | + var ioObjectRelease func(uint32) |
| 51 | + purego.RegisterLibFunc(&ioObjectRelease, iokit, "IOObjectRelease") |
| 52 | + |
| 53 | + // 4. Retrieve the UUID |
| 54 | + // Convert Go string to CFStringRef for the property key |
| 55 | + keyName := "IOPlatformUUID\x00" |
| 56 | + cfKey := cfStringCreateWithCString(0, &[]byte(keyName)[0], kCFStringEncodingUTF8) |
| 57 | + defer cfRelease(cfKey) |
| 58 | + |
| 59 | + // Look up the IOPlatformExpertDevice service |
| 60 | + className := "IOPlatformExpertDevice\x00" |
| 61 | + matchingDict := ioServiceMatching(&[]byte(className)[0]) |
| 62 | + service := ioServiceGetMatchingService(kIOMainPortDefault, matchingDict) |
| 63 | + if service == 0 { |
| 64 | + return "", fmt.Errorf("failed to find IOPlatformExpertDevice service") |
| 65 | + } |
| 66 | + defer ioObjectRelease(service) |
| 67 | + |
| 68 | + // Get the property from the registry |
| 69 | + cfValue := ioRegistryEntryCreateCFProperty(service, cfKey, 0, 0) |
| 70 | + if cfValue == 0 { |
| 71 | + return "", fmt.Errorf("failed to find IOPlatformUUID property") |
| 72 | + } |
| 73 | + defer cfRelease(cfValue) |
| 74 | + |
| 75 | + // Convert the resulting CFString back to a Go string |
| 76 | + buf := make([]byte, 128) |
| 77 | + if cfStringGetCString(cfValue, &buf[0], 128, kCFStringEncodingUTF8) { |
| 78 | + return strings.TrimRight(string(buf), "\x00"), nil |
| 79 | + } |
| 80 | + return "", fmt.Errorf("failed to extract string from CFProperty") |
| 81 | +} |
0 commit comments