Extend the system informations

This commit is contained in:
Zoltán Papp
2024-01-17 09:03:41 +01:00
parent 9fa0fbda0d
commit ef4a2ccbca
9 changed files with 286 additions and 12 deletions

119
client/system/chassis.go Normal file
View File

@@ -0,0 +1,119 @@
package system
const (
ChassisTypeOther uint = 0x01 // Other
ChassisTypeUnknown uint = 0x02 // Unknown
ChassisTypeDesktop uint = 0x03 // Desktop
ChassisTypeLowProfileDesktop uint = 0x04 // Low Profile Desktop
ChassisTypePizzaBox uint = 0x05 // Pizza Box
ChassisTypeMiniTower uint = 0x06 // Mini Tower
ChassisTypeTower uint = 0x07 // Tower
ChassisTypePortable uint = 0x08 // Portable
ChassisTypeLaptop uint = 0x09 // Laptop
ChassisTypeNotebook uint = 0x0a // Notebook
ChassisTypeHandHeld uint = 0x0b // Hand Held
ChassisTypeDockingStation uint = 0x0c // Docking Station
ChassisTypeAllInOne uint = 0x0d // All in One
ChassisTypeSubNotebook uint = 0x0e // Sub Notebook
ChassisTypeSpacesaving uint = 0x0f // Space-saving
ChassisTypeLunchBox uint = 0x10 // Lunch Box
ChassisTypeMainServerChassis uint = 0x11 // Main Server Chassis
ChassisTypeExpansionChassis uint = 0x12 // Expansion Chassis
ChassisTypeSubChassis uint = 0x13 // SubChassis
ChassisTypeBusExpansionChassis uint = 0x14 // Bus Expansion Chassis
ChassisTypePeripheralChassis uint = 0x15 // Peripheral Chassis
ChassisTypeRAIDChassis uint = 0x16 // RAID Chassis
ChassisTypeRackMountChassis uint = 0x17 // Rack Mount Chassis
ChassisTypeSealedcasePC uint = 0x18 // Sealed-case PC
ChassisTypeMultisystemChassis uint = 0x19 // Multi-system chassis
ChassisTypeCompactPCI uint = 0x1a // Compact PCI
ChassisTypeAdvancedTCA uint = 0x1b // Advanced TCA
ChassisTypeBlade uint = 0x1c // Blade
ChassisTypeBladeChassis uint = 0x1d // Blade Chassis
ChassisTypeTablet uint = 0x1e // Tablet
ChassisTypeConvertible uint = 0x1f // Convertible
ChassisTypeDetachable uint = 0x20 // Detachable
ChassisTypeIoTGateway uint = 0x21 // IoT Gateway
ChassisTypeEmbeddedPC uint = 0x22 // Embedded PC
ChassisTypeMiniPC uint = 0x23 // Mini PC
ChassisTypeStickPC uint = 0x24 // Stick PC
)
func chassisTypeDesc(id uint) string {
switch id {
case ChassisTypeOther:
return "Other"
case ChassisTypeUnknown:
return "Unknown"
case ChassisTypeDesktop:
return "Desktop"
case ChassisTypeLowProfileDesktop:
return "Low Profile Desktop"
case ChassisTypePizzaBox:
return "Pizza Box"
case ChassisTypeMiniTower:
return "Mini Tower"
case ChassisTypeTower:
return "Tower"
case ChassisTypePortable:
return "Portable"
case ChassisTypeLaptop:
return "Laptop"
case ChassisTypeNotebook:
return "Notebook"
case ChassisTypeHandHeld:
return "Hand Held"
case ChassisTypeDockingStation:
return "Docking Station"
case ChassisTypeAllInOne:
return "All In One"
case ChassisTypeSubNotebook:
return "Sub Notebook"
case ChassisTypeSpacesaving:
return "Space-saving"
case ChassisTypeLunchBox:
return "Lunch Box"
case ChassisTypeMainServerChassis:
return "Main Server Chassis"
case ChassisTypeExpansionChassis:
return "Expansion Chassis"
case ChassisTypeSubChassis:
return "Sub Chassis"
case ChassisTypeBusExpansionChassis:
return "Bus Expansion Chassis"
case ChassisTypePeripheralChassis:
return "Peripheral Chassis"
case ChassisTypeRAIDChassis:
return "RAID Chassis"
case ChassisTypeRackMountChassis:
return "Rack Mount Chassis"
case ChassisTypeSealedcasePC:
return "Sealed-case PC"
case ChassisTypeMultisystemChassis:
return "Multi-system"
case ChassisTypeCompactPCI:
return "CompactPCI"
case ChassisTypeAdvancedTCA:
return "AdvancedTCA"
case ChassisTypeBlade:
return "Blade"
case ChassisTypeBladeChassis:
return "Blade Chassis"
case ChassisTypeTablet:
return "Tablet"
case ChassisTypeConvertible:
return "Convertible"
case ChassisTypeDetachable:
return "Detachable"
case ChassisTypeIoTGateway:
return "IoT Gateway"
case ChassisTypeEmbeddedPC:
return "Embedded PC"
case ChassisTypeMiniPC:
return "Mini PC"
case ChassisTypeStickPC:
return "Stick PC"
default:
return "Unknown"
}
}

View File

@@ -31,6 +31,27 @@ type Info struct {
CPUs int
WiretrusteeVersion string
UIVersion string
BiosManufacturer string
BiosVersion string
ChassisType uint
ChassisTypeDesc string
ConnectionIp string
ConnectionMacAddress string
CPUSignature string
DefaultGatewayIp string
ExternalIp string
LastReboot string
LocalIp string
MacAddress string
KernelMajorVersion string
KernelMinorVersion string
OSBuild string
OSProductName string
ProductTypeDesc string
SerialNumber string
SystemManufacturer string
SystemProductName string
}
// extractUserAgent extracts Netbird's agent (client) name and version from the outgoing context

View File

@@ -33,11 +33,23 @@ func GetInfo(ctx context.Context) *Info {
log.Warnf("got an error while retrieving macOS version with sw_vers, error: %s. Using darwin version instead.\n", err)
swVersion = []byte(release)
}
gio := &Info{Kernel: sysName, OSVersion: strings.TrimSpace(string(swVersion)), Core: release, Platform: machine, OS: sysName, GoOS: runtime.GOOS, CPUs: runtime.NumCPU()}
systemHostname, _ := os.Hostname()
gio.Hostname = extractDeviceName(ctx, systemHostname)
gio.WiretrusteeVersion = version.NetbirdVersion()
gio.UIVersion = extractUserAgent(ctx)
systemHostname, _ := os.Hostname()
localAddr, macAddr := localAddresses()
gio := &Info{
Kernel: sysName,
OSVersion: strings.TrimSpace(string(swVersion)),
Core: release,
Platform: machine,
OS: sysName,
GoOS: runtime.GOOS,
CPUs: runtime.NumCPU(),
Hostname: extractDeviceName(ctx, systemHostname),
WiretrusteeVersion: version.NetbirdVersion(),
UIVersion: extractUserAgent(ctx),
LastReboot: lastReboot(),
LocalIp: localAddr,
MacAddress: macAddr,
}
return gio
}

View File

@@ -13,6 +13,7 @@ import (
"time"
log "github.com/sirupsen/logrus"
"github.com/zcalusic/sysinfo"
"github.com/netbirdio/netbird/version"
)
@@ -26,7 +27,7 @@ func GetInfo(ctx context.Context) *Info {
}
releaseInfo := _getReleaseInfo()
for strings.Contains(info, "broken pipe") {
for strings.Contains(releaseInfo, "broken pipe") {
releaseInfo = _getReleaseInfo()
time.Sleep(500 * time.Millisecond)
}
@@ -50,15 +51,51 @@ func GetInfo(ctx context.Context) *Info {
if osName == "" {
osName = osInfo[3]
}
gio := &Info{Kernel: osInfo[0], Core: osInfo[1], Platform: osInfo[2], OS: osName, OSVersion: osVer, GoOS: runtime.GOOS, CPUs: runtime.NumCPU()}
systemHostname, _ := os.Hostname()
gio.Hostname = extractDeviceName(ctx, systemHostname)
gio.WiretrusteeVersion = version.NetbirdVersion()
gio.UIVersion = extractUserAgent(ctx)
sysinfo := extendedInfo()
localAddr, macAddr := localAddresses()
gio := &Info{
Kernel: osInfo[0],
Core: osInfo[1],
Platform: osInfo[2],
OS: osName,
OSVersion: osVer,
GoOS: runtime.GOOS,
CPUs: runtime.NumCPU(),
Hostname: extractDeviceName(ctx, systemHostname),
WiretrusteeVersion: version.NetbirdVersion(),
UIVersion: extractUserAgent(ctx),
BiosManufacturer: sysinfo.BIOS.Vendor,
BiosVersion: sysinfo.BIOS.Version,
ChassisType: sysinfo.Chassis.Type,
ChassisTypeDesc: chassisTypeDesc(sysinfo.Chassis.Type), // make no sense to send the string to the server
/*
ConnectionIp: "", // "10.145.236.123",
ConnectionMacAddress: "", // 52-54-00-1a-31-05"
CPUSignature: "", // "198339"
*/
LastReboot: lastReboot(),
LocalIp: localAddr,
MacAddress: macAddr,
/*
OSBuild: string // 22621
OSProductName: string // "Windows 11 Home"
ProductTypeDesc: string // "Workstation"
SerialNumber: string // "MP1PKC2C""
SystemProductName: string // "81ND", # how to get this?
*/
SystemManufacturer: sysinfo.Product.Vendor, // todo validate
}
return gio
}
func extendedInfo() sysinfo.SysInfo {
var si sysinfo.SysInfo
si.GetSysInfo()
return si
}
func _getInfo() string {
cmd := exec.Command("uname", "-srio")
cmd.Stdin = strings.NewReader("some")

View File

@@ -0,0 +1,13 @@
package system
import (
"context"
"testing"
log "github.com/sirupsen/logrus"
)
func TestGetInfo(t *testing.T) {
info := GetInfo(context.Background())
log.Infof("info: %+v", info)
}

42
client/system/ip.go Normal file
View File

@@ -0,0 +1,42 @@
package system
import (
"net"
log "github.com/sirupsen/logrus"
)
func localAddresses() (string, string) {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
log.Errorf("failed to check ip: %s", err)
return "", ""
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
ifaces, err := net.Interfaces()
if err != nil {
log.Errorf("failed to list interfaces: %s", err)
return "", ""
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Errorf("failed to list addresses: %s", err)
continue
}
for _, addr := range addrs {
cidr, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
if localAddr.IP.String() == cidr.String() {
return addr.String(), i.HardwareAddr.String()
}
}
}
return "", ""
}

17
client/system/reboot.go Normal file
View File

@@ -0,0 +1,17 @@
package system
import (
"time"
"github.com/shirou/gopsutil/host"
log "github.com/sirupsen/logrus"
)
func lastReboot() string {
info, err := host.Info()
if err != nil {
log.Errorf("failed to get boot time: %s", err)
return ""
}
return time.Unix(int64(info.BootTime), 0).String()
}