From cfde14234176b750da47890866f3d1023aa2e3a5 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sat, 21 Nov 2020 23:13:19 +0100 Subject: version: unify architecture string handling Always report native architecture and use "x86" instead of "386" for all identification strings, except when explicitly stating the Go verison. Signed-off-by: Jason A. Donenfeld --- version/useragent.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'version') diff --git a/version/useragent.go b/version/useragent.go index 77633765..e0781e29 100644 --- a/version/useragent.go +++ b/version/useragent.go @@ -6,10 +6,64 @@ package version import ( + "debug/pe" + "errors" "fmt" "runtime" + + "golang.org/x/sys/windows" ) +var arch string + +func NativeArch() string { + if len(arch) > 0 { + return arch + } + var processMachine, nativeMachine uint16 + err := windows.IsWow64Process2(windows.CurrentProcess(), &processMachine, &nativeMachine) + if err != nil && errors.Is(err, windows.ERROR_PROC_NOT_FOUND) { + var b bool + err = windows.IsWow64Process(windows.CurrentProcess(), &b) + if err != nil { + panic(err) + } + if b && runtime.GOARCH == "x86" { + nativeMachine = pe.IMAGE_FILE_MACHINE_AMD64 + } else if b && runtime.GOARCH == "arm" { + nativeMachine = pe.IMAGE_FILE_MACHINE_ARM64 + } else { + switch runtime.GOARCH { + case "arm": + nativeMachine = pe.IMAGE_FILE_MACHINE_ARMNT + case "arm64": + nativeMachine = pe.IMAGE_FILE_MACHINE_ARM64 + case "amd64": + nativeMachine = pe.IMAGE_FILE_MACHINE_AMD64 + case "386": + nativeMachine = pe.IMAGE_FILE_MACHINE_I386 + default: + panic("Unrecognized GOARCH") + } + } + } else if err != nil { + panic(err) + } + switch nativeMachine { + case pe.IMAGE_FILE_MACHINE_ARMNT: + arch = "arm" + case pe.IMAGE_FILE_MACHINE_ARM64: + arch = "arm64" + case pe.IMAGE_FILE_MACHINE_AMD64: + arch = "amd64" + case pe.IMAGE_FILE_MACHINE_I386: + arch = "x86" + default: + panic("Unrecognized machine type") + } + return arch +} + func UserAgent() string { - return fmt.Sprintf("WireGuard/%s (%s; %s)", Number, OsName(), runtime.GOARCH) + return fmt.Sprintf("WireGuard/%s (%s; %s)", Number, OsName(), NativeArch()) } -- cgit v1.2.3-59-g8ed1b