From bc2278803b3c0fcab26a73c099b32af8f708d93a Mon Sep 17 00:00:00 2001 From: Fufu Date: Tue, 27 Aug 2024 14:32:29 +0800 Subject: [PATCH] update: sync rs/xid v1.6.0 --- xid/README.md | 4 +++- xid/hostid_darwin.go | 29 +++++++++++++++++++++++++++-- xid/hostid_windows.go | 20 ++++++++++++++++---- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/xid/README.md b/xid/README.md index 1161b92..f320473 100644 --- a/xid/README.md +++ b/xid/README.md @@ -96,7 +96,8 @@ References: - https://blog.twitter.com/2010/announcing-snowflake - Python port by [Graham Abbott](https://github.com/graham): https://github.com/graham/python_xid - Scala port by [Egor Kolotaev](https://github.com/kolotaev): https://github.com/kolotaev/ride -- Rust port by [Jérôme Renard](https://github.com/jeromer/): https://github.com/jeromer/libxid +- Rust port by [Kaz Yoshihara](https://github.com/kazk): https://github.com/kazk/xid-rs +- Python wrapper around the Rust port [Aleksandr Shpak](https://github.com/shpaker): https://github.com/shpaker/epyxid - Ruby port by [Valar](https://github.com/valarpirai/): https://github.com/valarpirai/ruby_xid - Java port by [0xShamil](https://github.com/0xShamil/): https://github.com/0xShamil/java-xid - Dart port by [Peter Bwire](https://github.com/pitabwire): https://pub.dev/packages/xid @@ -104,6 +105,7 @@ References: - Swift port by [Uditha Atukorala](https://github.com/uatuko): https://github.com/uatuko/swift-xid - C++ port by [Uditha Atukorala](https://github.com/uatuko): https://github.com/uatuko/libxid - Typescript & Javascript port by [Yiwen AI](https://github.com/yiwen-ai): https://github.com/yiwen-ai/xid-ts +- Gleam port by [Alexandre Del Vecchio](https://github.com/defgenx): https://github.com/defgenx/gxid ## Install diff --git a/xid/hostid_darwin.go b/xid/hostid_darwin.go index b18d52f..25520c3 100644 --- a/xid/hostid_darwin.go +++ b/xid/hostid_darwin.go @@ -3,8 +3,33 @@ package xid -import "syscall" +import ( + "errors" + "os/exec" + "strings" +) func readPlatformMachineID() (string, error) { - return syscall.Sysctl("kern.uuid") + ioreg, err := exec.LookPath("ioreg") + if err != nil { + return "", err + } + + cmd := exec.Command(ioreg, "-rd1", "-c", "IOPlatformExpertDevice") + out, err := cmd.CombinedOutput() + if err != nil { + return "", err + } + + for _, line := range strings.Split(string(out), "\n") { + if strings.Contains(line, "IOPlatformUUID") { + parts := strings.SplitAfter(line, `" = "`) + if len(parts) == 2 { + uuid := strings.TrimRight(parts[1], `"`) + return strings.ToLower(uuid), nil + } + } + } + + return "", errors.New("cannot find host id") } diff --git a/xid/hostid_windows.go b/xid/hostid_windows.go index 2af811d..0c284f5 100644 --- a/xid/hostid_windows.go +++ b/xid/hostid_windows.go @@ -12,11 +12,17 @@ import ( func readPlatformMachineID() (string, error) { // source: https://github.com/shirou/gopsutil/blob/master/host/host_syscall.go var h syscall.Handle - err := syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, syscall.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, syscall.KEY_READ|syscall.KEY_WOW64_64KEY, &h) + + regKeyCryptoPtr, err := syscall.UTF16PtrFromString(`SOFTWARE\Microsoft\Cryptography`) + if err != nil { + return "", fmt.Errorf(`error reading registry key "SOFTWARE\Microsoft\Cryptography": %w`, err) + } + + err = syscall.RegOpenKeyEx(syscall.HKEY_LOCAL_MACHINE, regKeyCryptoPtr, 0, syscall.KEY_READ|syscall.KEY_WOW64_64KEY, &h) if err != nil { return "", err } - defer syscall.RegCloseKey(h) + defer func() { _ = syscall.RegCloseKey(h) }() const syscallRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16 const uuidLen = 36 @@ -24,9 +30,15 @@ func readPlatformMachineID() (string, error) { var regBuf [syscallRegBufLen]uint16 bufLen := uint32(syscallRegBufLen) var valType uint32 - err = syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) + + mGuidPtr, err := syscall.UTF16PtrFromString(`MachineGuid`) if err != nil { - return "", err + return "", fmt.Errorf("error reading machine GUID: %w", err) + } + + err = syscall.RegQueryValueEx(h, mGuidPtr, nil, &valType, (*byte)(unsafe.Pointer(®Buf[0])), &bufLen) + if err != nil { + return "", fmt.Errorf("error parsing ") } hostID := syscall.UTF16ToString(regBuf[:])