diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 29d4770..ac8233a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81be927 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea +/.vscode +.DS_Store \ No newline at end of file diff --git a/go.mod b/go.mod index 3d61b50..490102b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/rs/xid -go 1.12 +go 1.16 diff --git a/hostid_darwin.go b/hostid_darwin.go index 08351ff..1735156 100644 --- a/hostid_darwin.go +++ b/hostid_darwin.go @@ -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") } diff --git a/hostid_windows.go b/hostid_windows.go index ec2593e..a4d98ab 100644 --- a/hostid_windows.go +++ b/hostid_windows.go @@ -11,11 +11,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 @@ -23,9 +29,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[:])