Skip to content

Commit

Permalink
Fix OS version info on FreeBSD (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
pappz authored May 31, 2024
1 parent e3c9f1d commit d403658
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 39 deletions.
19 changes: 14 additions & 5 deletions client/system/info_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 3 additions & 34 deletions client/system/info_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand All @@ -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(),
Expand Down Expand Up @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions client/system/osrelease_unix.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit d403658

Please sign in to comment.