From 3453aecbec46afe486d7efc3292e7212d525e0dd Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Thu, 28 Aug 2025 12:26:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20https://github.com/komari-monitor/komari?= =?UTF-8?q?/issues/226=20win10=20win11=E8=AF=86=E5=88=AB=E4=BC=BC=E4=B9=8E?= =?UTF-8?q?=E6=9C=89=E7=82=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitoring/unit/os_test.go | 11 +++++++++++ monitoring/unit/os_windows.go | 37 ++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 monitoring/unit/os_test.go diff --git a/monitoring/unit/os_test.go b/monitoring/unit/os_test.go new file mode 100644 index 0000000..bdc360c --- /dev/null +++ b/monitoring/unit/os_test.go @@ -0,0 +1,11 @@ +package monitoring + +import "testing" + +func TestOsName(t *testing.T) { + if got := OSName(); got == "" { + t.Errorf("OSName() = %v, want non-empty string", got) + } else { + t.Logf("OSName() = %v", got) + } +} diff --git a/monitoring/unit/os_windows.go b/monitoring/unit/os_windows.go index a6676cd..e212b3d 100644 --- a/monitoring/unit/os_windows.go +++ b/monitoring/unit/os_windows.go @@ -21,28 +21,33 @@ func OSName() string { if err != nil { return "Microsoft Windows" } - // 如果是 Server 版本,直接返回原始名称 + + // Server 版本保持原样 if strings.Contains(productName, "Server") { return productName } - // Windows 11 - majorVersion, _, err := key.GetIntegerValue("CurrentMajorVersionNumber") - if err == nil && majorVersion >= 10 { - buildNumberStr, _, err := key.GetStringValue("CurrentBuild") - if err == nil { - buildNumber, err := strconv.Atoi(buildNumberStr) - if err == nil && buildNumber >= 22000 { // Windows 11 starts at build 22000 - // Windows 11 Windows 10 Pro for Workstations - edition := strings.Replace(productName, "Windows 10 ", "", 1) + // 如果注册表已经直接提供 Windows 11 名称,直接返回 + if strings.Contains(productName, "Windows 11") { + return productName + } + + // Windows 11 从 build 22000 起。DisplayVersion 在 Win10 21H2 也会是 21H2,不能作为判断依据。 + buildNumberStr, _, err := key.GetStringValue("CurrentBuild") + if err == nil { + if buildNumber, err2 := strconv.Atoi(buildNumberStr); err2 == nil && buildNumber >= 22000 { + // 旧字段可能仍然写着 Windows 10,把前缀替换为 Windows 11 + if strings.HasPrefix(productName, "Windows 10 ") { + edition := strings.TrimPrefix(productName, "Windows 10 ") return "Windows 11 " + edition } - } - // DisplayVersion - displayVersion, _, err := key.GetStringValue("DisplayVersion") - if err == nil && displayVersion >= "21H2" { - edition := strings.Replace(productName, "Windows 10 ", "", 1) - return "Windows 11 " + edition + if productName == "Windows 10" { // 极端精简情况 + return "Windows 11" + } + // 如果不是以 Windows 10 开头,但 build 已经 >= 22000,直接补成 Windows 11 + 原名称尾部 + if !strings.Contains(productName, "Windows 11") { + return strings.Replace(productName, "Windows 10", "Windows 11", 1) + } } }