2023-12-18 11:46:58 +01:00
|
|
|
//go:build !ios
|
|
|
|
// +build !ios
|
|
|
|
|
2021-08-27 11:34:38 +02:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-25 23:25:02 +02:00
|
|
|
"context"
|
2021-08-27 11:34:38 +02:00
|
|
|
"os"
|
2023-02-03 21:40:30 +01:00
|
|
|
"os/exec"
|
2021-08-27 11:34:38 +02:00
|
|
|
"runtime"
|
2023-02-03 21:40:30 +01:00
|
|
|
"strings"
|
2023-03-15 07:54:51 +01:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2024-02-20 11:53:11 +01:00
|
|
|
"github.com/netbirdio/netbird/client/system/detect_cloud"
|
|
|
|
"github.com/netbirdio/netbird/client/system/detect_platform"
|
2023-03-15 07:54:51 +01:00
|
|
|
"github.com/netbirdio/netbird/version"
|
2021-08-27 11:34:38 +02:00
|
|
|
)
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
// GetInfo retrieves and parses the system information
|
|
|
|
func GetInfo(ctx context.Context) *Info {
|
2022-07-30 11:31:27 +02:00
|
|
|
utsname := unix.Utsname{}
|
|
|
|
err := unix.Uname(&utsname)
|
|
|
|
if err != nil {
|
2023-11-10 16:33:13 +01:00
|
|
|
log.Warnf("getInfo: %s", err)
|
2021-08-27 11:34:38 +02:00
|
|
|
}
|
2022-07-30 11:31:27 +02:00
|
|
|
sysName := string(bytes.Split(utsname.Sysname[:], []byte{0})[0])
|
|
|
|
machine := string(bytes.Split(utsname.Machine[:], []byte{0})[0])
|
|
|
|
release := string(bytes.Split(utsname.Release[:], []byte{0})[0])
|
2023-03-15 07:54:51 +01:00
|
|
|
swVersion, err := exec.Command("sw_vers", "-productVersion").Output()
|
2023-02-03 21:40:30 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Warnf("got an error while retrieving macOS version with sw_vers, error: %s. Using darwin version instead.\n", err)
|
2023-03-15 07:54:51 +01:00
|
|
|
swVersion = []byte(release)
|
2023-02-03 21:40:30 +01:00
|
|
|
}
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
addrs, err := networkAddresses()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to discover network addresses: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serialNum, prodName, manufacturer := sysInfo()
|
|
|
|
|
|
|
|
env := Environment{
|
|
|
|
Cloud: detect_cloud.Detect(ctx),
|
|
|
|
Platform: detect_platform.Detect(ctx),
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2021-08-27 11:34:38 +02:00
|
|
|
return gio
|
|
|
|
}
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
func sysInfo() (serialNumber string, productName string, manufacturer string) {
|
|
|
|
out, _ := exec.Command("/usr/sbin/ioreg", "-l").Output() // err ignored for brevity
|
|
|
|
for _, l := range strings.Split(string(out), "\n") {
|
|
|
|
if strings.Contains(l, "IOPlatformSerialNumber") {
|
|
|
|
serialNumber = trimIoRegLine(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(l, "ModelNumber") && productName == "" {
|
|
|
|
productName = trimIoRegLine(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(l, "device manufacturer") && manufacturer == "" {
|
|
|
|
manufacturer = trimIoRegLine(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimIoRegLine(l string) string {
|
|
|
|
kv := strings.Split(l, "=")
|
|
|
|
if len(kv) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
s := strings.TrimSpace(kv[1])
|
|
|
|
return strings.Trim(s, `"`)
|
|
|
|
}
|