Skip to content

Commit

Permalink
update: sync rs/xid v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Aug 27, 2024
1 parent f699b37 commit bc22788
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
4 changes: 3 additions & 1 deletion xid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ 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
- PostgreSQL port by [Rasmus Holm](https://github.com/crholm): https://github.com/modfin/pg-xid
- 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

Expand Down
29 changes: 27 additions & 2 deletions xid/hostid_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
20 changes: 16 additions & 4 deletions xid/hostid_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,33 @@ 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

var regBuf [syscallRegBufLen]uint16
bufLen := uint32(syscallRegBufLen)
var valType uint32
err = syscall.RegQueryValueEx(h, syscall.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[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(&regBuf[0])), &bufLen)
if err != nil {
return "", fmt.Errorf("error parsing ")
}

hostID := syscall.UTF16ToString(regBuf[:])
Expand Down

0 comments on commit bc22788

Please sign in to comment.