mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
[client] Move some sys info to static place (#4446)
This PR refactors the system information collection code by moving static system information gathering to a dedicated location and separating platform-specific implementations. The primary goal is to improve code organization and maintainability by centralizing static info collection logic. Key changes: - Centralized static info collection into dedicated files with platform-specific implementations - Moved `StaticInfo` struct definition to the main static_info.go file - Added async initialization function `UpdateStaticInfoAsync()` across all platforms
This commit is contained in:
@@ -227,7 +227,7 @@ func doForegroundLogin(ctx context.Context, cmd *cobra.Command, setupKey string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update host's static platform and system information
|
// update host's static platform and system information
|
||||||
system.UpdateStaticInfo()
|
system.UpdateStaticInfoAsync()
|
||||||
|
|
||||||
configFilePath, err := activeProf.FilePath()
|
configFilePath, err := activeProf.FilePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func (p *program) Start(svc service.Service) error {
|
|||||||
log.Info("starting NetBird service") //nolint
|
log.Info("starting NetBird service") //nolint
|
||||||
|
|
||||||
// Collect static system and platform information
|
// Collect static system and platform information
|
||||||
system.UpdateStaticInfo()
|
system.UpdateStaticInfoAsync()
|
||||||
|
|
||||||
// in any case, even if configuration does not exists we run daemon to serve CLI gRPC API.
|
// in any case, even if configuration does not exists we run daemon to serve CLI gRPC API.
|
||||||
p.serv = grpc.NewServer()
|
p.serv = grpc.NewServer()
|
||||||
|
|||||||
@@ -95,14 +95,6 @@ func (i *Info) SetFlags(
|
|||||||
i.LazyConnectionEnabled = lazyConnectionEnabled
|
i.LazyConnectionEnabled = lazyConnectionEnabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// StaticInfo is an object that contains machine information that does not change
|
|
||||||
type StaticInfo struct {
|
|
||||||
SystemSerialNumber string
|
|
||||||
SystemProductName string
|
|
||||||
SystemManufacturer string
|
|
||||||
Environment Environment
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractUserAgent extracts Netbird's agent (client) name and version from the outgoing context
|
// extractUserAgent extracts Netbird's agent (client) name and version from the outgoing context
|
||||||
func extractUserAgent(ctx context.Context) string {
|
func extractUserAgent(ctx context.Context) string {
|
||||||
md, hasMeta := metadata.FromOutgoingContext(ctx)
|
md, hasMeta := metadata.FromOutgoingContext(ctx)
|
||||||
@@ -195,10 +187,3 @@ func GetInfoWithChecks(ctx context.Context, checks []*proto.Checks) (*Info, erro
|
|||||||
|
|
||||||
return info, nil
|
return info, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStaticInfo asynchronously updates static system and platform information
|
|
||||||
func UpdateStaticInfo() {
|
|
||||||
go func() {
|
|
||||||
_ = updateStaticInfo()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,6 +15,11 @@ import (
|
|||||||
"github.com/netbirdio/netbird/version"
|
"github.com/netbirdio/netbird/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UpdateStaticInfoAsync is a no-op on Android as there is no static info to update
|
||||||
|
func UpdateStaticInfoAsync() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
// GetInfo retrieves and parses the system information
|
// GetInfo retrieves and parses the system information
|
||||||
func GetInfo(ctx context.Context) *Info {
|
func GetInfo(ctx context.Context) *Info {
|
||||||
kernel := "android"
|
kernel := "android"
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ import (
|
|||||||
"github.com/netbirdio/netbird/version"
|
"github.com/netbirdio/netbird/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func UpdateStaticInfoAsync() {
|
||||||
|
go updateStaticInfo()
|
||||||
|
}
|
||||||
|
|
||||||
// GetInfo retrieves and parses the system information
|
// GetInfo retrieves and parses the system information
|
||||||
func GetInfo(ctx context.Context) *Info {
|
func GetInfo(ctx context.Context) *Info {
|
||||||
utsname := unix.Utsname{}
|
utsname := unix.Utsname{}
|
||||||
@@ -41,7 +45,7 @@ func GetInfo(ctx context.Context) *Info {
|
|||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
si := updateStaticInfo()
|
si := getStaticInfo()
|
||||||
if time.Since(start) > 1*time.Second {
|
if time.Since(start) > 1*time.Second {
|
||||||
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ import (
|
|||||||
"github.com/netbirdio/netbird/version"
|
"github.com/netbirdio/netbird/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UpdateStaticInfoAsync is a no-op on Android as there is no static info to update
|
||||||
|
func UpdateStaticInfoAsync() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
// GetInfo retrieves and parses the system information
|
// GetInfo retrieves and parses the system information
|
||||||
func GetInfo(ctx context.Context) *Info {
|
func GetInfo(ctx context.Context) *Info {
|
||||||
out := _getInfo()
|
out := _getInfo()
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ import (
|
|||||||
"github.com/netbirdio/netbird/version"
|
"github.com/netbirdio/netbird/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UpdateStaticInfoAsync is a no-op on Android as there is no static info to update
|
||||||
|
func UpdateStaticInfoAsync() {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
|
||||||
// GetInfo retrieves and parses the system information
|
// GetInfo retrieves and parses the system information
|
||||||
func GetInfo(ctx context.Context) *Info {
|
func GetInfo(ctx context.Context) *Info {
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ var (
|
|||||||
getSystemInfo = defaultSysInfoImplementation
|
getSystemInfo = defaultSysInfoImplementation
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func UpdateStaticInfoAsync() {
|
||||||
|
go updateStaticInfo()
|
||||||
|
}
|
||||||
|
|
||||||
// GetInfo retrieves and parses the system information
|
// GetInfo retrieves and parses the system information
|
||||||
func GetInfo(ctx context.Context) *Info {
|
func GetInfo(ctx context.Context) *Info {
|
||||||
info := _getInfo()
|
info := _getInfo()
|
||||||
@@ -48,7 +52,7 @@ func GetInfo(ctx context.Context) *Info {
|
|||||||
}
|
}
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
si := updateStaticInfo()
|
si := getStaticInfo()
|
||||||
if time.Since(start) > 1*time.Second {
|
if time.Since(start) > 1*time.Second {
|
||||||
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,66 +2,48 @@ package system
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/yusufpapurcu/wmi"
|
|
||||||
"golang.org/x/sys/windows/registry"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/version"
|
"github.com/netbirdio/netbird/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Win32_OperatingSystem struct {
|
func UpdateStaticInfoAsync() {
|
||||||
Caption string
|
go updateStaticInfo()
|
||||||
}
|
|
||||||
|
|
||||||
type Win32_ComputerSystem struct {
|
|
||||||
Manufacturer string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Win32_ComputerSystemProduct struct {
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Win32_BIOS struct {
|
|
||||||
SerialNumber string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetInfo retrieves and parses the system information
|
// GetInfo retrieves and parses the system information
|
||||||
func GetInfo(ctx context.Context) *Info {
|
func GetInfo(ctx context.Context) *Info {
|
||||||
osName, osVersion := getOSNameAndVersion()
|
|
||||||
buildVersion := getBuildVersion()
|
|
||||||
|
|
||||||
addrs, err := networkAddresses()
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to discover network addresses: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
si := updateStaticInfo()
|
si := getStaticInfo()
|
||||||
if time.Since(start) > 1*time.Second {
|
if time.Since(start) > 1*time.Second {
|
||||||
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
log.Warnf("updateStaticInfo took %s", time.Since(start))
|
||||||
}
|
}
|
||||||
|
|
||||||
gio := &Info{
|
gio := &Info{
|
||||||
Kernel: "windows",
|
Kernel: "windows",
|
||||||
OSVersion: osVersion,
|
OSVersion: si.OSVersion,
|
||||||
Platform: "unknown",
|
Platform: "unknown",
|
||||||
OS: osName,
|
OS: si.OSName,
|
||||||
GoOS: runtime.GOOS,
|
GoOS: runtime.GOOS,
|
||||||
CPUs: runtime.NumCPU(),
|
CPUs: runtime.NumCPU(),
|
||||||
KernelVersion: buildVersion,
|
KernelVersion: si.BuildVersion,
|
||||||
NetworkAddresses: addrs,
|
|
||||||
SystemSerialNumber: si.SystemSerialNumber,
|
SystemSerialNumber: si.SystemSerialNumber,
|
||||||
SystemProductName: si.SystemProductName,
|
SystemProductName: si.SystemProductName,
|
||||||
SystemManufacturer: si.SystemManufacturer,
|
SystemManufacturer: si.SystemManufacturer,
|
||||||
Environment: si.Environment,
|
Environment: si.Environment,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addrs, err := networkAddresses()
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("failed to discover network addresses: %s", err)
|
||||||
|
} else {
|
||||||
|
gio.NetworkAddresses = addrs
|
||||||
|
}
|
||||||
|
|
||||||
systemHostname, _ := os.Hostname()
|
systemHostname, _ := os.Hostname()
|
||||||
gio.Hostname = extractDeviceName(ctx, systemHostname)
|
gio.Hostname = extractDeviceName(ctx, systemHostname)
|
||||||
gio.NetbirdVersion = version.NetbirdVersion()
|
gio.NetbirdVersion = version.NetbirdVersion()
|
||||||
@@ -69,120 +51,3 @@ func GetInfo(ctx context.Context) *Info {
|
|||||||
|
|
||||||
return gio
|
return gio
|
||||||
}
|
}
|
||||||
|
|
||||||
func sysInfo() (serialNumber string, productName string, manufacturer string) {
|
|
||||||
var err error
|
|
||||||
serialNumber, err = sysNumber()
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to get system serial number: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
productName, err = sysProductName()
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to get system product name: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
manufacturer, err = sysManufacturer()
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to get system manufacturer: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return serialNumber, productName, manufacturer
|
|
||||||
}
|
|
||||||
|
|
||||||
func getOSNameAndVersion() (string, string) {
|
|
||||||
var dst []Win32_OperatingSystem
|
|
||||||
query := wmi.CreateQuery(&dst, "")
|
|
||||||
err := wmi.Query(query, &dst)
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err)
|
|
||||||
return "Windows", getBuildVersion()
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func sysNumber() (string, error) {
|
|
||||||
var dst []Win32_BIOS
|
|
||||||
query := wmi.CreateQuery(&dst, "")
|
|
||||||
err := wmi.Query(query, &dst)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return dst[0].SerialNumber, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sysProductName() (string, error) {
|
|
||||||
var dst []Win32_ComputerSystemProduct
|
|
||||||
query := wmi.CreateQuery(&dst, "")
|
|
||||||
err := wmi.Query(query, &dst)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// `ComputerSystemProduct` could be empty on some virtualized systems
|
|
||||||
if len(dst) < 1 {
|
|
||||||
return "unknown", nil
|
|
||||||
}
|
|
||||||
return dst[0].Name, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sysManufacturer() (string, error) {
|
|
||||||
var dst []Win32_ComputerSystem
|
|
||||||
query := wmi.CreateQuery(&dst, "")
|
|
||||||
err := wmi.Query(query, &dst)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return dst[0].Manufacturer, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,12 +3,7 @@
|
|||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/system/detect_cloud"
|
|
||||||
"github.com/netbirdio/netbird/client/system/detect_platform"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -16,25 +11,26 @@ var (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func updateStaticInfo() StaticInfo {
|
// StaticInfo is an object that contains machine information that does not change
|
||||||
|
type StaticInfo struct {
|
||||||
|
SystemSerialNumber string
|
||||||
|
SystemProductName string
|
||||||
|
SystemManufacturer string
|
||||||
|
Environment Environment
|
||||||
|
|
||||||
|
// Windows specific fields
|
||||||
|
OSName string
|
||||||
|
OSVersion string
|
||||||
|
BuildVersion string
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateStaticInfo() {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
staticInfo = newStaticInfo()
|
||||||
defer cancel()
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
wg.Add(3)
|
|
||||||
go func() {
|
|
||||||
staticInfo.SystemSerialNumber, staticInfo.SystemProductName, staticInfo.SystemManufacturer = sysInfo()
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
staticInfo.Environment.Cloud = detect_cloud.Detect(ctx)
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
go func() {
|
|
||||||
staticInfo.Environment.Platform = detect_platform.Detect(ctx)
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
wg.Wait()
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStaticInfo() StaticInfo {
|
||||||
|
updateStaticInfo()
|
||||||
return staticInfo
|
return staticInfo
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
//go:build android || freebsd || ios
|
|
||||||
|
|
||||||
package system
|
|
||||||
|
|
||||||
// updateStaticInfo returns an empty implementation for unsupported platforms
|
|
||||||
func updateStaticInfo() StaticInfo {
|
|
||||||
return StaticInfo{}
|
|
||||||
}
|
|
||||||
35
client/system/static_info_update.go
Normal file
35
client/system/static_info_update.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
//go:build (linux && !android) || (darwin && !ios)
|
||||||
|
|
||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/system/detect_cloud"
|
||||||
|
"github.com/netbirdio/netbird/client/system/detect_platform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newStaticInfo() StaticInfo {
|
||||||
|
si := StaticInfo{}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(3)
|
||||||
|
go func() {
|
||||||
|
si.SystemSerialNumber, si.SystemProductName, si.SystemManufacturer = sysInfo()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
si.Environment.Cloud = detect_cloud.Detect(ctx)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
si.Environment.Platform = detect_platform.Detect(ctx)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
return si
|
||||||
|
}
|
||||||
184
client/system/static_info_update_windows.go
Normal file
184
client/system/static_info_update_windows.go
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/yusufpapurcu/wmi"
|
||||||
|
"golang.org/x/sys/windows/registry"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/system/detect_cloud"
|
||||||
|
"github.com/netbirdio/netbird/client/system/detect_platform"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Win32_OperatingSystem struct {
|
||||||
|
Caption string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Win32_ComputerSystem struct {
|
||||||
|
Manufacturer string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Win32_ComputerSystemProduct struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Win32_BIOS struct {
|
||||||
|
SerialNumber string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newStaticInfo() StaticInfo {
|
||||||
|
si := StaticInfo{}
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
si.SystemSerialNumber, si.SystemProductName, si.SystemManufacturer = sysInfo()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
si.Environment.Cloud = detect_cloud.Detect(ctx)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
si.Environment.Platform = detect_platform.Detect(ctx)
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
si.OSName, si.OSVersion = getOSNameAndVersion()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
si.BuildVersion = getBuildVersion()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
|
return si
|
||||||
|
}
|
||||||
|
|
||||||
|
func sysInfo() (serialNumber string, productName string, manufacturer string) {
|
||||||
|
var err error
|
||||||
|
serialNumber, err = sysNumber()
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("failed to get system serial number: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
productName, err = sysProductName()
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("failed to get system product name: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
manufacturer, err = sysManufacturer()
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("failed to get system manufacturer: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return serialNumber, productName, manufacturer
|
||||||
|
}
|
||||||
|
|
||||||
|
func sysNumber() (string, error) {
|
||||||
|
var dst []Win32_BIOS
|
||||||
|
query := wmi.CreateQuery(&dst, "")
|
||||||
|
err := wmi.Query(query, &dst)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return dst[0].SerialNumber, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sysProductName() (string, error) {
|
||||||
|
var dst []Win32_ComputerSystemProduct
|
||||||
|
query := wmi.CreateQuery(&dst, "")
|
||||||
|
err := wmi.Query(query, &dst)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
// `ComputerSystemProduct` could be empty on some virtualized systems
|
||||||
|
if len(dst) < 1 {
|
||||||
|
return "unknown", nil
|
||||||
|
}
|
||||||
|
return dst[0].Name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sysManufacturer() (string, error) {
|
||||||
|
var dst []Win32_ComputerSystem
|
||||||
|
query := wmi.CreateQuery(&dst, "")
|
||||||
|
err := wmi.Query(query, &dst)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return dst[0].Manufacturer, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getOSNameAndVersion() (string, string) {
|
||||||
|
var dst []Win32_OperatingSystem
|
||||||
|
query := wmi.CreateQuery(&dst, "")
|
||||||
|
err := wmi.Query(query, &dst)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return "Windows", getBuildVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user