From c3a1e1ca2c7d0b11c48ed5305eab92c73f54a618 Mon Sep 17 00:00:00 2001 From: Maycon Santos Date: Tue, 12 Mar 2024 23:58:16 +0100 Subject: [PATCH] refactor function --- client/system/info.go | 12 +++++++++++ client/system/info_android.go | 23 +++++++------------- client/system/info_darwin.go | 34 ++++++++++++----------------- client/system/info_freebsd.go | 24 +++++++-------------- client/system/info_ios.go | 23 +++++++------------- client/system/info_linux.go | 40 ++++++++++++++--------------------- client/system/info_windows.go | 33 ++++++++++++----------------- 7 files changed, 78 insertions(+), 111 deletions(-) diff --git a/client/system/info.go b/client/system/info.go index 04aa687fa..372e58d7a 100644 --- a/client/system/info.go +++ b/client/system/info.go @@ -8,6 +8,7 @@ import ( "google.golang.org/grpc/metadata" + "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/version" ) @@ -53,6 +54,17 @@ type Info struct { ServerSSHAllowed bool } +// GetInfo retrieves and parses the system information +func GetInfo(ctx context.Context, config internal.Config) *Info { + info := getInfo(ctx) + info.RosenpassEnabled = config.RosenpassEnabled + info.RosenpassPermissive = config.RosenpassPermissive + if config.ServerSSHAllowed != nil { + info.ServerSSHAllowed = *config.ServerSSHAllowed + } + return info +} + // extractUserAgent extracts Netbird's agent (client) name and version from the outgoing context func extractUserAgent(ctx context.Context) string { md, hasMeta := metadata.FromOutgoingContext(ctx) diff --git a/client/system/info_android.go b/client/system/info_android.go index 16e0d12b7..43e24e957 100644 --- a/client/system/info_android.go +++ b/client/system/info_android.go @@ -12,12 +12,10 @@ import ( log "github.com/sirupsen/logrus" - "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/version" ) -// GetInfo retrieves and parses the system information -func GetInfo(ctx context.Context, config internal.Config) *Info { +func getInfo(ctx context.Context) *Info { kernel := "android" osInfo := uname() if len(osInfo) == 2 { @@ -30,18 +28,13 @@ func GetInfo(ctx context.Context, config internal.Config) *Info { } gio := &Info{ - Kernel: kernel, - Platform: "unknown", - OS: "android", - OSVersion: osVersion(), - GoOS: runtime.GOOS, - CPUs: runtime.NumCPU(), - KernelVersion: kernelVersion, - RosenpassEnabled: config.RosenpassEnabled, - RosenpassPermissive: config.RosenpassPermissive, - } - if config.ServerSSHAllowed != nil { - gio.ServerSSHAllowed = true + Kernel: kernel, + Platform: "unknown", + OS: "android", + OSVersion: osVersion(), + GoOS: runtime.GOOS, + CPUs: runtime.NumCPU(), + KernelVersion: kernelVersion, } gio.Hostname = extractDeviceName(ctx, "android") diff --git a/client/system/info_darwin.go b/client/system/info_darwin.go index 0ccf7e5e9..b5fe22eb1 100644 --- a/client/system/info_darwin.go +++ b/client/system/info_darwin.go @@ -15,14 +15,12 @@ import ( log "github.com/sirupsen/logrus" - "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/client/system/detect_cloud" "github.com/netbirdio/netbird/client/system/detect_platform" "github.com/netbirdio/netbird/version" ) -// GetInfo retrieves and parses the system information -func GetInfo(ctx context.Context, config internal.Config) *Info { +func getInfo(ctx context.Context) *Info { utsname := unix.Utsname{} err := unix.Uname(&utsname) if err != nil { @@ -50,24 +48,18 @@ func GetInfo(ctx context.Context, config internal.Config) *Info { } gio := &Info{ - Kernel: sysName, - OSVersion: strings.TrimSpace(string(swVersion)), - Platform: machine, - OS: sysName, - GoOS: runtime.GOOS, - CPUs: runtime.NumCPU(), - KernelVersion: release, - NetworkAddresses: addrs, - SystemSerialNumber: serialNum, - SystemProductName: prodName, - SystemManufacturer: manufacturer, - Environment: env, - RosenpassEnabled: config.RosenpassEnabled, - RosenpassPermissive: config.RosenpassPermissive, - } - - if config.ServerSSHAllowed != nil { - gio.ServerSSHAllowed = *config.ServerSSHAllowed + Kernel: sysName, + OSVersion: strings.TrimSpace(string(swVersion)), + Platform: machine, + OS: sysName, + GoOS: runtime.GOOS, + CPUs: runtime.NumCPU(), + KernelVersion: release, + NetworkAddresses: addrs, + SystemSerialNumber: serialNum, + SystemProductName: prodName, + SystemManufacturer: manufacturer, + Environment: env, } systemHostname, _ := os.Hostname() diff --git a/client/system/info_freebsd.go b/client/system/info_freebsd.go index c4aa1d858..4051f8d98 100644 --- a/client/system/info_freebsd.go +++ b/client/system/info_freebsd.go @@ -10,14 +10,12 @@ import ( "strings" "time" - "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/client/system/detect_cloud" "github.com/netbirdio/netbird/client/system/detect_platform" "github.com/netbirdio/netbird/version" ) -// GetInfo retrieves and parses the system information -func GetInfo(ctx context.Context, config internal.Config) *Info { +func getInfo(ctx context.Context) *Info { out := _getInfo() for strings.Contains(out, "broken pipe") { out = _getInfo() @@ -33,19 +31,13 @@ func GetInfo(ctx context.Context, config internal.Config) *Info { } gio := &Info{ - Kernel: osInfo[0], - Platform: runtime.GOARCH, - OS: osInfo[2], - GoOS: runtime.GOOS, - CPUs: runtime.NumCPU(), - KernelVersion: osInfo[1], - Environment: env, - RosenpassEnabled: config.RosenpassEnabled, - RosenpassPermissive: config.RosenpassPermissive, - } - - if config.ServerSSHAllowed != nil { - gio.ServerSSHAllowed = true + Kernel: osInfo[0], + Platform: runtime.GOARCH, + OS: osInfo[2], + GoOS: runtime.GOOS, + CPUs: runtime.NumCPU(), + KernelVersion: osInfo[1], + Environment: env, } systemHostname, _ := os.Hostname() diff --git a/client/system/info_ios.go b/client/system/info_ios.go index 9dea699d1..1d91dc57f 100644 --- a/client/system/info_ios.go +++ b/client/system/info_ios.go @@ -7,30 +7,23 @@ import ( "context" "runtime" - "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/version" ) -// GetInfo retrieves and parses the system information -func GetInfo(ctx context.Context, config internal.Config) *Info { +func getInfo(ctx context.Context) *Info { // Convert fixed-size byte arrays to Go strings sysName := extractOsName(ctx, "sysName") swVersion := extractOsVersion(ctx, "swVersion") gio := &Info{ - Kernel: sysName, - OSVersion: swVersion, - Platform: "unknown", - OS: sysName, - GoOS: runtime.GOOS, - CPUs: runtime.NumCPU(), - KernelVersion: swVersion, - RosenpassEnabled: config.RosenpassEnabled, - RosenpassPermissive: config.RosenpassPermissive, - } - if config.ServerSSHAllowed != nil { - gio.ServerSSHAllowed = true + Kernel: sysName, + OSVersion: swVersion, + Platform: "unknown", + OS: sysName, + GoOS: runtime.GOOS, + CPUs: runtime.NumCPU(), + KernelVersion: swVersion, } gio.Hostname = extractDeviceName(ctx, "hostname") gio.WiretrusteeVersion = version.NetbirdVersion() diff --git a/client/system/info_linux.go b/client/system/info_linux.go index 457072e95..e337829cb 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -15,14 +15,12 @@ import ( log "github.com/sirupsen/logrus" "github.com/zcalusic/sysinfo" - "github.com/netbirdio/netbird/client/internal" "github.com/netbirdio/netbird/client/system/detect_cloud" "github.com/netbirdio/netbird/client/system/detect_platform" "github.com/netbirdio/netbird/version" ) -// GetInfo retrieves and parses the system information -func GetInfo(ctx context.Context, config internal.Config) *Info { +func getInfo(ctx context.Context) *Info { info := _getInfo() for strings.Contains(info, "broken pipe") { info = _getInfo() @@ -70,27 +68,21 @@ func GetInfo(ctx context.Context, config internal.Config) *Info { } gio := &Info{ - Kernel: osInfo[0], - Platform: osInfo[2], - OS: osName, - OSVersion: osVer, - Hostname: extractDeviceName(ctx, systemHostname), - GoOS: runtime.GOOS, - CPUs: runtime.NumCPU(), - WiretrusteeVersion: version.NetbirdVersion(), - UIVersion: extractUserAgent(ctx), - KernelVersion: osInfo[1], - NetworkAddresses: addrs, - SystemSerialNumber: serialNum, - SystemProductName: prodName, - SystemManufacturer: manufacturer, - Environment: env, - RosenpassEnabled: config.RosenpassEnabled, - RosenpassPermissive: config.RosenpassPermissive, - } - - if config.ServerSSHAllowed != nil { - gio.ServerSSHAllowed = *config.ServerSSHAllowed + Kernel: osInfo[0], + Platform: osInfo[2], + OS: osName, + OSVersion: osVer, + Hostname: extractDeviceName(ctx, systemHostname), + GoOS: runtime.GOOS, + CPUs: runtime.NumCPU(), + WiretrusteeVersion: version.NetbirdVersion(), + UIVersion: extractUserAgent(ctx), + KernelVersion: osInfo[1], + NetworkAddresses: addrs, + SystemSerialNumber: serialNum, + SystemProductName: prodName, + SystemManufacturer: manufacturer, + Environment: env, } return gio diff --git a/client/system/info_windows.go b/client/system/info_windows.go index 65be150fa..9a79b8112 100644 --- a/client/system/info_windows.go +++ b/client/system/info_windows.go @@ -31,8 +31,7 @@ type Win32_BIOS struct { SerialNumber string } -// GetInfo retrieves and parses the system information -func GetInfo(ctx context.Context, config internal.Config) *Info { +func getInfo(ctx context.Context) *Info { osName, osVersion := getOSNameAndVersion() buildVersion := getBuildVersion() @@ -62,24 +61,18 @@ func GetInfo(ctx context.Context, config internal.Config) *Info { } gio := &Info{ - Kernel: "windows", - OSVersion: osVersion, - Platform: "unknown", - OS: osName, - GoOS: runtime.GOOS, - CPUs: runtime.NumCPU(), - KernelVersion: buildVersion, - NetworkAddresses: addrs, - SystemSerialNumber: serialNum, - SystemProductName: prodName, - SystemManufacturer: manufacturer, - Environment: env, - RosenpassEnabled: config.RosenpassEnabled, - RosenpassPermissive: config.RosenpassPermissive, - } - - if config.ServerSSHAllowed != nil { - gio.ServerSSHAllowed = *config.ServerSSHAllowed + Kernel: "windows", + OSVersion: osVersion, + Platform: "unknown", + OS: osName, + GoOS: runtime.GOOS, + CPUs: runtime.NumCPU(), + KernelVersion: buildVersion, + NetworkAddresses: addrs, + SystemSerialNumber: serialNum, + SystemProductName: prodName, + SystemManufacturer: manufacturer, + Environment: env, } systemHostname, _ := os.Hostname()