2021-08-27 11:34:38 +02:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2022-05-25 23:25:02 +02:00
|
|
|
"context"
|
2022-10-12 12:25:33 +02:00
|
|
|
"fmt"
|
2021-08-27 11:34:38 +02:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2023-11-22 16:23:19 +01:00
|
|
|
"strings"
|
2023-03-15 07:54:51 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-11-22 16:23:19 +01:00
|
|
|
"github.com/yusufpapurcu/wmi"
|
2023-03-15 07:54:51 +01:00
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/version"
|
2021-08-27 11:34:38 +02:00
|
|
|
)
|
|
|
|
|
2023-11-22 16:23:19 +01:00
|
|
|
type Win32_OperatingSystem struct {
|
|
|
|
Caption string
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
// GetInfo retrieves and parses the system information
|
|
|
|
func GetInfo(ctx context.Context) *Info {
|
2023-11-22 16:23:19 +01:00
|
|
|
osName, osVersion := getOSNameAndVersion()
|
|
|
|
buildVersion := getBuildVersion()
|
2023-11-24 17:49:39 +01:00
|
|
|
gio := &Info{Kernel: "windows", OSVersion: osVersion, Core: buildVersion, Platform: "unknown", OS: osName, GoOS: runtime.GOOS, CPUs: runtime.NumCPU()}
|
2023-04-20 16:00:22 +02:00
|
|
|
systemHostname, _ := os.Hostname()
|
|
|
|
gio.Hostname = extractDeviceName(ctx, systemHostname)
|
2023-03-15 07:54:51 +01:00
|
|
|
gio.WiretrusteeVersion = version.NetbirdVersion()
|
2022-05-25 23:25:02 +02:00
|
|
|
gio.UIVersion = extractUserAgent(ctx)
|
2022-02-03 18:35:54 +01:00
|
|
|
|
2021-08-27 11:34:38 +02:00
|
|
|
return gio
|
|
|
|
}
|
2022-10-12 12:25:33 +02:00
|
|
|
|
2023-11-22 16:23:19 +01:00
|
|
|
func getOSNameAndVersion() (string, string) {
|
|
|
|
var dst []Win32_OperatingSystem
|
|
|
|
query := wmi.CreateQuery(&dst, "")
|
|
|
|
err := wmi.Query(query, &dst)
|
|
|
|
if err != nil {
|
2024-01-01 21:28:42 +01:00
|
|
|
log.Error(err)
|
|
|
|
return "Windows", getBuildVersion()
|
2023-11-22 16:23:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(dst) == 0 {
|
|
|
|
return "Windows", getBuildVersion()
|
|
|
|
}
|
|
|
|
|
|
|
|
split := strings.Split(dst[0].Caption, " ")
|
|
|
|
|
|
|
|
if len(split) < 3 {
|
|
|
|
return "Windows", getBuildVersion()
|
|
|
|
}
|
|
|
|
|
|
|
|
name := split[1]
|
|
|
|
version := split[2]
|
|
|
|
if split[2] == "Server" {
|
|
|
|
name = fmt.Sprintf("%s %s", split[1], split[2])
|
|
|
|
version = split[3]
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, version
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBuildVersion() string {
|
2022-10-12 12:25:33 +02:00
|
|
|
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return "0.0.0.0"
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
deferErr := k.Close()
|
|
|
|
if deferErr != nil {
|
|
|
|
log.Error(deferErr)
|
|
|
|
}
|
|
|
|
}()
|
2023-03-15 07:54:51 +01:00
|
|
|
|
2022-10-12 12:25:33 +02:00
|
|
|
major, _, err := k.GetIntegerValue("CurrentMajorVersionNumber")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
minor, _, err := k.GetIntegerValue("CurrentMinorVersionNumber")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
build, _, err := k.GetStringValue("CurrentBuildNumber")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
// Update Build Revision
|
|
|
|
ubr, _, err := k.GetIntegerValue("UBR")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
ver := fmt.Sprintf("%d.%d.%s.%d", major, minor, build, ubr)
|
|
|
|
return ver
|
|
|
|
}
|