2023-03-17 10:37:27 +01:00
|
|
|
//go:build !android
|
|
|
|
// +build !android
|
|
|
|
|
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"
|
|
|
|
"os/exec"
|
2024-07-15 14:43:50 +02:00
|
|
|
"regexp"
|
2021-08-27 11:34:38 +02:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-03-15 07:54:51 +01:00
|
|
|
|
2023-11-10 16:33:13 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-02-20 11:53:11 +01:00
|
|
|
"github.com/zcalusic/sysinfo"
|
2023-11-10 16:33:13 +01:00
|
|
|
|
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 {
|
2021-08-27 11:34:38 +02:00
|
|
|
info := _getInfo()
|
2021-08-29 13:22:52 +02:00
|
|
|
for strings.Contains(info, "broken pipe") {
|
2021-08-27 11:34:38 +02:00
|
|
|
info = _getInfo()
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2023-11-27 16:40:02 +01:00
|
|
|
osStr := strings.ReplaceAll(info, "\n", "")
|
|
|
|
osStr = strings.ReplaceAll(osStr, "\r\n", "")
|
2021-08-27 11:34:38 +02:00
|
|
|
osInfo := strings.Split(osStr, " ")
|
2024-06-13 13:24:24 +02:00
|
|
|
|
|
|
|
osName, osVersion := readOsReleaseFile()
|
2021-08-27 11:34:38 +02:00
|
|
|
if osName == "" {
|
|
|
|
osName = osInfo[3]
|
|
|
|
}
|
2024-02-20 11:53:11 +01:00
|
|
|
|
2023-04-20 16:00:22 +02:00
|
|
|
systemHostname, _ := os.Hostname()
|
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: osInfo[0],
|
|
|
|
Platform: osInfo[2],
|
|
|
|
OS: osName,
|
2024-06-13 13:24:24 +02:00
|
|
|
OSVersion: osVersion,
|
2024-02-20 11:53:11 +01:00
|
|
|
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,
|
|
|
|
}
|
2022-02-03 18:35:54 +01:00
|
|
|
|
2021-08-27 11:34:38 +02:00
|
|
|
return gio
|
|
|
|
}
|
|
|
|
|
|
|
|
func _getInfo() string {
|
|
|
|
cmd := exec.Command("uname", "-srio")
|
|
|
|
cmd.Stdin = strings.NewReader("some")
|
|
|
|
var out bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2023-11-10 16:33:13 +01:00
|
|
|
log.Warnf("getInfo: %s", err)
|
2021-08-27 11:34:38 +02:00
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
2024-02-20 11:53:11 +01:00
|
|
|
func sysInfo() (serialNumber string, productName string, manufacturer string) {
|
|
|
|
var si sysinfo.SysInfo
|
|
|
|
si.GetSysInfo()
|
2024-07-15 14:43:50 +02:00
|
|
|
isascii := regexp.MustCompile("^[[:ascii:]]+$")
|
2024-07-02 13:19:08 +02:00
|
|
|
serial := si.Chassis.Serial
|
|
|
|
if (serial == "Default string" || serial == "") && si.Product.Serial != "" {
|
|
|
|
serial = si.Product.Serial
|
|
|
|
}
|
2024-07-15 14:43:50 +02:00
|
|
|
if (!isascii.MatchString(serial)) && si.Board.Serial != "" {
|
|
|
|
serial = si.Board.Serial
|
|
|
|
}
|
|
|
|
name := si.Product.Name
|
|
|
|
if (!isascii.MatchString(name)) && si.Board.Name != "" {
|
|
|
|
name = si.Board.Name
|
|
|
|
}
|
|
|
|
return serial, name, si.Product.Vendor
|
2024-02-20 11:53:11 +01:00
|
|
|
}
|