2021-08-27 11:34:38 +02:00
|
|
|
package system
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
import (
|
|
|
|
"context"
|
2024-02-20 11:53:11 +01:00
|
|
|
"net"
|
|
|
|
"net/netip"
|
2022-05-25 23:25:02 +02:00
|
|
|
"strings"
|
|
|
|
|
2023-03-15 07:54:51 +01:00
|
|
|
"google.golang.org/grpc/metadata"
|
2022-02-03 18:35:54 +01:00
|
|
|
|
2023-03-15 07:54:51 +01:00
|
|
|
"github.com/netbirdio/netbird/version"
|
|
|
|
)
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
// DeviceNameCtxKey context key for device name
|
|
|
|
const DeviceNameCtxKey = "deviceName"
|
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
// OsVersionCtxKey context key for operating system version
|
|
|
|
const OsVersionCtxKey = "OsVersion"
|
|
|
|
|
|
|
|
// OsNameCtxKey context key for operating system name
|
|
|
|
const OsNameCtxKey = "OsName"
|
|
|
|
|
2024-05-31 17:26:56 +02:00
|
|
|
// UiVersionCtxKey context key for user UI version
|
|
|
|
const UiVersionCtxKey = "user-agent"
|
|
|
|
|
2024-02-20 11:53:11 +01:00
|
|
|
type NetworkAddress struct {
|
|
|
|
NetIP netip.Prefix
|
|
|
|
Mac string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Environment struct {
|
|
|
|
Cloud string
|
|
|
|
Platform string
|
|
|
|
}
|
|
|
|
|
2023-03-15 07:54:51 +01:00
|
|
|
// Info is an object that contains machine information
|
2021-08-27 11:34:38 +02:00
|
|
|
// Most of the code is taken from https://github.com/matishsiao/goInfo
|
|
|
|
type Info struct {
|
2022-02-03 18:35:54 +01:00
|
|
|
GoOS string
|
|
|
|
Kernel string
|
|
|
|
Platform string
|
|
|
|
OS string
|
|
|
|
OSVersion string
|
|
|
|
Hostname string
|
|
|
|
CPUs int
|
|
|
|
WiretrusteeVersion string
|
2022-05-25 23:25:02 +02:00
|
|
|
UIVersion string
|
2024-02-20 09:59:56 +01:00
|
|
|
KernelVersion string
|
2024-02-20 11:53:11 +01:00
|
|
|
NetworkAddresses []NetworkAddress
|
|
|
|
SystemSerialNumber string
|
|
|
|
SystemProductName string
|
|
|
|
SystemManufacturer string
|
|
|
|
Environment Environment
|
2022-02-03 18:35:54 +01:00
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
// extractUserAgent extracts Netbird's agent (client) name and version from the outgoing context
|
|
|
|
func extractUserAgent(ctx context.Context) string {
|
|
|
|
md, hasMeta := metadata.FromOutgoingContext(ctx)
|
|
|
|
if hasMeta {
|
|
|
|
agent, ok := md["user-agent"]
|
|
|
|
if ok {
|
|
|
|
nbAgent := strings.Split(agent[0], " ")[0]
|
|
|
|
if strings.HasPrefix(nbAgent, "netbird") {
|
|
|
|
return nbAgent
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:00:22 +02:00
|
|
|
// extractDeviceName extracts device name from context or returns the default system name
|
|
|
|
func extractDeviceName(ctx context.Context, defaultName string) string {
|
|
|
|
v, ok := ctx.Value(DeviceNameCtxKey).(string)
|
|
|
|
if !ok {
|
|
|
|
return defaultName
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2022-05-25 23:25:02 +02:00
|
|
|
// GetDesktopUIUserAgent returns the Desktop ui user agent
|
|
|
|
func GetDesktopUIUserAgent() string {
|
2023-03-15 07:54:51 +01:00
|
|
|
return "netbird-desktop-ui/" + version.NetbirdVersion()
|
2022-05-25 23:25:02 +02:00
|
|
|
}
|
2024-02-20 11:53:11 +01:00
|
|
|
|
|
|
|
func networkAddresses() ([]NetworkAddress, error) {
|
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var netAddresses []NetworkAddress
|
|
|
|
for _, iface := range interfaces {
|
|
|
|
if iface.HardwareAddr.String() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range addrs {
|
|
|
|
ipNet, ok := address.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipNet.IP.IsLoopback() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
netAddr := NetworkAddress{
|
|
|
|
NetIP: netip.MustParsePrefix(ipNet.String()),
|
|
|
|
Mac: iface.HardwareAddr.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDuplicated(netAddresses, netAddr) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
netAddresses = append(netAddresses, netAddr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return netAddresses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDuplicated(addresses []NetworkAddress, addr NetworkAddress) bool {
|
|
|
|
for _, duplicated := range addresses {
|
|
|
|
if duplicated.NetIP == addr.NetIP {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|