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
|
|
|
)
|
|
|
|
|
2024-08-01 16:46:55 +02:00
|
|
|
type SysInfoGetter interface {
|
|
|
|
GetSysInfo() SysInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type SysInfoWrapper struct {
|
|
|
|
si sysinfo.SysInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SysInfoWrapper) GetSysInfo() SysInfo {
|
|
|
|
s.si.GetSysInfo()
|
|
|
|
return SysInfo{
|
|
|
|
ChassisSerial: s.si.Chassis.Serial,
|
|
|
|
ProductSerial: s.si.Product.Serial,
|
|
|
|
BoardSerial: s.si.Board.Serial,
|
|
|
|
ProductName: s.si.Product.Name,
|
|
|
|
BoardName: s.si.Board.Name,
|
|
|
|
ProductVendor: s.si.Product.Vendor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-01 16:46:55 +02:00
|
|
|
si := SysInfoWrapper{}
|
|
|
|
serialNum, prodName, manufacturer := sysInfo(si.GetSysInfo())
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
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-08-01 16:46:55 +02:00
|
|
|
func sysInfo(si SysInfo) (string, string, string) {
|
2024-07-15 14:43:50 +02:00
|
|
|
isascii := regexp.MustCompile("^[[:ascii:]]+$")
|
2024-08-01 16:46:55 +02:00
|
|
|
|
|
|
|
serials := []string{si.ChassisSerial, si.ProductSerial}
|
|
|
|
serial := ""
|
|
|
|
|
|
|
|
for _, s := range serials {
|
|
|
|
if isascii.MatchString(s) {
|
|
|
|
serial = s
|
|
|
|
if s != "Default string" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if serial == "" && isascii.MatchString(si.BoardSerial) {
|
|
|
|
serial = si.BoardSerial
|
2024-07-02 13:19:08 +02:00
|
|
|
}
|
2024-08-01 16:46:55 +02:00
|
|
|
|
|
|
|
var name string
|
|
|
|
for _, n := range []string{si.ProductName, si.BoardName} {
|
|
|
|
if isascii.MatchString(n) {
|
|
|
|
name = n
|
|
|
|
break
|
|
|
|
}
|
2024-07-15 14:43:50 +02:00
|
|
|
}
|
2024-08-01 16:46:55 +02:00
|
|
|
|
|
|
|
var manufacturer string
|
|
|
|
if isascii.MatchString(si.ProductVendor) {
|
|
|
|
manufacturer = si.ProductVendor
|
2024-07-15 14:43:50 +02:00
|
|
|
}
|
2024-08-01 16:46:55 +02:00
|
|
|
return serial, name, manufacturer
|
2024-02-20 11:53:11 +01:00
|
|
|
}
|