2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-08-23 05:12:27 +01:00
|
|
|
|
|
|
|
package hostinfo
|
|
|
|
|
|
|
|
import (
|
2021-11-22 18:45:19 +00:00
|
|
|
"fmt"
|
2022-02-22 18:01:36 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-01-25 21:58:23 +00:00
|
|
|
"strings"
|
2021-11-22 18:45:19 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/sys/windows/registry"
|
2022-12-01 01:33:29 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2022-02-22 17:39:09 +00:00
|
|
|
"tailscale.com/util/winutil"
|
2024-04-18 17:14:27 +01:00
|
|
|
"tailscale.com/util/winutil/winenv"
|
2021-08-23 05:12:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2024-04-18 17:14:27 +01:00
|
|
|
distroName = lazyDistroName.Get
|
2022-09-11 18:57:18 +01:00
|
|
|
osVersion = lazyOSVersion.Get
|
|
|
|
packageType = lazyPackageType.Get
|
2021-08-23 05:12:27 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 18:57:18 +01:00
|
|
|
var (
|
2024-04-18 17:14:27 +01:00
|
|
|
lazyDistroName = &lazyAtomicValue[string]{f: ptr.To(distroNameWindows)}
|
2022-12-01 01:33:29 +00:00
|
|
|
lazyOSVersion = &lazyAtomicValue[string]{f: ptr.To(osVersionWindows)}
|
|
|
|
lazyPackageType = &lazyAtomicValue[string]{f: ptr.To(packageTypeWindows)}
|
2022-09-11 18:57:18 +01:00
|
|
|
)
|
2021-08-23 05:12:27 +01:00
|
|
|
|
2024-04-18 17:14:27 +01:00
|
|
|
func distroNameWindows() string {
|
|
|
|
if winenv.IsWindowsServer() {
|
|
|
|
return "Server"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-08-23 05:12:27 +01:00
|
|
|
func osVersionWindows() string {
|
2021-11-22 18:45:19 +00:00
|
|
|
major, minor, build := windows.RtlGetNtVersionNumbers()
|
|
|
|
s := fmt.Sprintf("%d.%d.%d", major, minor, build)
|
|
|
|
// Windows 11 still uses 10 as its major number internally
|
|
|
|
if major == 10 {
|
|
|
|
if ubr, err := getUBR(); err == nil {
|
|
|
|
s += fmt.Sprintf(".%d", ubr)
|
|
|
|
}
|
2021-08-23 05:12:27 +01:00
|
|
|
}
|
|
|
|
return s // "10.0.19041.388", ideally
|
|
|
|
}
|
2021-11-22 18:45:19 +00:00
|
|
|
|
|
|
|
// getUBR obtains a fourth version field, the "Update Build Revision",
|
|
|
|
// from the registry. This field is only available beginning with Windows 10.
|
|
|
|
func getUBR() (uint32, error) {
|
|
|
|
key, err := registry.OpenKey(registry.LOCAL_MACHINE,
|
|
|
|
`SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE|registry.WOW64_64KEY)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer key.Close()
|
|
|
|
|
|
|
|
val, valType, err := key.GetIntegerValue("UBR")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if valType != registry.DWORD {
|
|
|
|
return 0, registry.ErrUnexpectedType
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint32(val), nil
|
|
|
|
}
|
2022-02-22 18:01:36 +00:00
|
|
|
|
|
|
|
func packageTypeWindows() string {
|
|
|
|
if _, err := os.Stat(`C:\ProgramData\chocolatey\lib\tailscale`); err == nil {
|
|
|
|
return "choco"
|
|
|
|
}
|
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-01-25 21:58:23 +00:00
|
|
|
home, _ := os.UserHomeDir()
|
|
|
|
if strings.HasPrefix(exe, filepath.Join(home, "scoop", "apps", "tailscale")) {
|
|
|
|
return "scoop"
|
|
|
|
}
|
2024-04-30 19:23:40 +01:00
|
|
|
msiSentinel, _ := winutil.GetRegInteger("MSI")
|
|
|
|
if msiSentinel != 1 {
|
|
|
|
// Atypical. Not worth trying to detect. Likely open
|
|
|
|
// source tailscaled or a developer running by hand.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
result := "msi"
|
|
|
|
if env, _ := winutil.GetRegString("MSIDist"); env != "" {
|
|
|
|
result += "/" + env
|
2022-02-22 18:01:36 +00:00
|
|
|
}
|
2024-04-30 19:23:40 +01:00
|
|
|
return result
|
2022-02-22 18:01:36 +00:00
|
|
|
}
|