Skip to content

Commit

Permalink
Fix UUID for Darwin hosts (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone authored Aug 23, 2024
1 parent 9d8d29f commit 7aed5a8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
30 changes: 18 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,30 @@ on:

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
platform: [ ubuntu-latest, windows-latest, macos-13, macos-latest ]
name: Test - ${{ matrix.platform }}
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.18"
go-version-file: "go.mod"
check-latest: true
- run: go test -race -failfast ./...
- run: go vet ./... && go test -race -failfast ./...
golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
strategy:
matrix:
platform: [ ubuntu-latest, windows-latest, macos-13, macos-latest ]
name: golangci-lint - ${{ matrix.platform }}
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.18"
go-version-file: "go.mod"
check-latest: true
- uses: golangci/golangci-lint-action@v3
- uses: golangci/golangci-lint-action@v6
with:
version: latest
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/.vscode
.DS_Store
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/rs/xid

go 1.12
go 1.16
29 changes: 27 additions & 2 deletions hostid_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,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 hostid_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,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 7aed5a8

Please sign in to comment.