From 1d7f9d5b4a59b3e0a6a883780d4b2e5b4e99a857 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 15 Mar 2021 21:28:48 -0700 Subject: [PATCH] Revert "control/controlclient: use API to get Windows version number" This reverts commit 4224b3f73110f6f0c6268eb1ae943bc24cf89baf. From https://github.com/tailscale/tailscale/pull/1494#discussion_r594852889 ... > Actually, I want all four numbers back. I spent the evening > debugging an issue for a user running an old version of Windows > and then going to to > https://en.wikipedia.org/wiki/Windows_10_version_history_(version_1809) > and reading all the revision notes in the footnotes of that wikipedia > page. > > I'm going to revert this for now for Tailscale 1.6. We can land it > again later when we figure out how to get the fourth numbers. Updates #1478 --- control/controlclient/hostinfo_windows.go | 20 +++++++++---- .../controlclient/hostinfo_windows_test.go | 28 ------------------- 2 files changed, 15 insertions(+), 33 deletions(-) delete mode 100644 control/controlclient/hostinfo_windows_test.go diff --git a/control/controlclient/hostinfo_windows.go b/control/controlclient/hostinfo_windows.go index 81c438327..07cb4ce4e 100644 --- a/control/controlclient/hostinfo_windows.go +++ b/control/controlclient/hostinfo_windows.go @@ -5,9 +5,9 @@ package controlclient import ( - "fmt" - - "golang.org/x/sys/windows" + "os/exec" + "strings" + "syscall" ) func init() { @@ -15,6 +15,16 @@ func init() { } func osVersionWindows() string { - major, minor, build := windows.RtlGetNtVersionNumbers() - return fmt.Sprintf("%d.%d.%d", major, minor, build) + cmd := exec.Command("cmd", "/c", "ver") + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + out, _ := cmd.Output() // "\nMicrosoft Windows [Version 10.0.19041.388]\n\n" + s := strings.TrimSpace(string(out)) + s = strings.TrimPrefix(s, "Microsoft Windows [") + s = strings.TrimSuffix(s, "]") + + // "Version 10.x.y.z", with "Version" localized. Keep only stuff after the space. + if sp := strings.Index(s, " "); sp != -1 { + s = s[sp+1:] + } + return s // "10.0.19041.388", ideally } diff --git a/control/controlclient/hostinfo_windows_test.go b/control/controlclient/hostinfo_windows_test.go deleted file mode 100644 index edfb9b0c1..000000000 --- a/control/controlclient/hostinfo_windows_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package controlclient - -import ( - "os/exec" - "regexp" - "testing" -) - -func TestOsVersionWindows(t *testing.T) { - out, err := exec.Command("cmd", "/c", "ver").Output() - if err != nil { - t.Fatalf("`ver` error: %v", err) - } - // Extract the version number from the output, and isolate the first three parts (major.minor.build) - rx := regexp.MustCompile(`(\d+\.\d+\.\d+)(\.\d+)?`) - m := rx.FindStringSubmatch(string(out)) - if m == nil { - t.Fatalf("no version number in `ver` output: %q", out) - } - got := osVersionWindows() - if m[1] != got { - t.Errorf("osVersionWindows got %q want %q", got, m[1]) - } -}