diff --git a/client/system/info_freebsd.go b/client/system/info_freebsd.go index 148fbcb6b3e..454e58a0b68 100644 --- a/client/system/info_freebsd.go +++ b/client/system/info_freebsd.go @@ -34,14 +34,23 @@ func GetInfo(ctx context.Context) *Info { Platform: detect_platform.Detect(ctx), } - gio := &Info{Kernel: osInfo[0], Platform: runtime.GOARCH, OS: osInfo[2], GoOS: runtime.GOOS, CPUs: runtime.NumCPU(), KernelVersion: osInfo[1], Environment: env} + osName, osVersion := readOsReleaseFile() systemHostname, _ := os.Hostname() - gio.Hostname = extractDeviceName(ctx, systemHostname) - gio.WiretrusteeVersion = version.NetbirdVersion() - gio.UIVersion = extractUserAgent(ctx) - return gio + return &Info{ + GoOS: runtime.GOOS, + Kernel: osInfo[0], + Platform: runtime.GOARCH, + OS: osName, + OSVersion: osVersion, + Hostname: extractDeviceName(ctx, systemHostname), + CPUs: runtime.NumCPU(), + WiretrusteeVersion: version.NetbirdVersion(), + UIVersion: extractUserAgent(ctx), + KernelVersion: osInfo[1], + Environment: env, + } } func _getInfo() string { diff --git a/client/system/info_linux.go b/client/system/info_linux.go index 652bc111518..1c5405d4dff 100644 --- a/client/system/info_linux.go +++ b/client/system/info_linux.go @@ -28,28 +28,11 @@ func GetInfo(ctx context.Context) *Info { time.Sleep(500 * time.Millisecond) } - releaseInfo := _getReleaseInfo() - for strings.Contains(info, "broken pipe") { - releaseInfo = _getReleaseInfo() - time.Sleep(500 * time.Millisecond) - } - - osRelease := strings.Split(releaseInfo, "\n") - var osName string - var osVer string - for _, s := range osRelease { - if strings.HasPrefix(s, "NAME=") { - osName = strings.Split(s, "=")[1] - osName = strings.ReplaceAll(osName, "\"", "") - } else if strings.HasPrefix(s, "VERSION_ID=") { - osVer = strings.Split(s, "=")[1] - osVer = strings.ReplaceAll(osVer, "\"", "") - } - } - osStr := strings.ReplaceAll(info, "\n", "") osStr = strings.ReplaceAll(osStr, "\r\n", "") osInfo := strings.Split(osStr, " ") + + osName, osVersion := readOsReleaseFile() if osName == "" { osName = osInfo[3] } @@ -72,7 +55,7 @@ func GetInfo(ctx context.Context) *Info { Kernel: osInfo[0], Platform: osInfo[2], OS: osName, - OSVersion: osVer, + OSVersion: osVersion, Hostname: extractDeviceName(ctx, systemHostname), GoOS: runtime.GOOS, CPUs: runtime.NumCPU(), @@ -103,20 +86,6 @@ func _getInfo() string { return out.String() } -func _getReleaseInfo() string { - cmd := exec.Command("cat", "/etc/os-release") - 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 { - log.Warnf("geucwReleaseInfo: %s", err) - } - return out.String() -} - func sysInfo() (serialNumber string, productName string, manufacturer string) { var si sysinfo.SysInfo si.GetSysInfo() diff --git a/client/system/osrelease_unix.go b/client/system/osrelease_unix.go new file mode 100644 index 00000000000..851633248b3 --- /dev/null +++ b/client/system/osrelease_unix.go @@ -0,0 +1,38 @@ +//go:build (linux && !android) || freebsd + +package system + +import ( + "bufio" + "os" + "strings" + + log "github.com/sirupsen/logrus" +) + +func readOsReleaseFile() (osName string, osVer string) { + file, err := os.Open("/etc/os-release") + if err != nil { + log.Warnf("failed to open file /etc/os-release: %s", err) + return "", "" + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "NAME=") { + osName = strings.ReplaceAll(strings.Split(line, "=")[1], "\"", "") + continue + } + if strings.HasPrefix(line, "VERSION_ID=") { + osVer = strings.ReplaceAll(strings.Split(line, "=")[1], "\"", "") + continue + } + + if osName != "" && osVer != "" { + break + } + } + return +}