mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-18 02:48:40 +00:00
control/controlclient: use API to get Windows version number
Fixes #1478 Signed-off-by: Aleksandar Pesic <peske.nis@gmail.com>
This commit is contained in:
parent
2bc518dcb2
commit
4224b3f731
@ -5,9 +5,9 @@
|
|||||||
package controlclient
|
package controlclient
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os/exec"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -15,16 +15,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func osVersionWindows() string {
|
func osVersionWindows() string {
|
||||||
cmd := exec.Command("cmd", "/c", "ver")
|
major, minor, build := windows.RtlGetNtVersionNumbers()
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
|
return fmt.Sprintf("%d.%d.%d", major, minor, build)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
28
control/controlclient/hostinfo_windows_test.go
Normal file
28
control/controlclient/hostinfo_windows_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// 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])
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user