aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/version/useragent.go
blob: 5488029a625cae2ff6467df3a2075a4f059d0760 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019-2020 WireGuard LLC. All Rights Reserved.
 */

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(), NativeArch())
}