From 5c7260298f2c22a12f74b8a91a85bf1f55dc342d Mon Sep 17 00:00:00 2001 From: braginini Date: Fri, 27 Aug 2021 11:34:38 +0200 Subject: [PATCH] chore: adjust system info discovery methods --- client/system/Info_darwin.go | 39 +++++++++++++ client/system/Info_linux.go | 76 +++++++++++++++++++++++++ client/system/info.go | 14 +++++ client/system/info_freebsd.go | 39 +++++++++++++ client/system/info_windows.go | 35 ++++++++++++ go.mod | 1 - go.sum | 2 - management/client/client.go | 6 +- management/server/http/handler/peers.go | 2 +- 9 files changed, 207 insertions(+), 7 deletions(-) create mode 100644 client/system/Info_darwin.go create mode 100644 client/system/Info_linux.go create mode 100644 client/system/info.go create mode 100644 client/system/info_freebsd.go create mode 100644 client/system/info_windows.go diff --git a/client/system/Info_darwin.go b/client/system/Info_darwin.go new file mode 100644 index 000000000..3190dd702 --- /dev/null +++ b/client/system/Info_darwin.go @@ -0,0 +1,39 @@ +package system + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "runtime" + "strings" + "time" +) + +func GetInfo() *Info { + out := _getInfo() + for strings.Index(out, "broken pipe") != -1 { + out = _getInfo() + time.Sleep(500 * time.Millisecond) + } + osStr := strings.Replace(out, "\n", "", -1) + osStr = strings.Replace(osStr, "\r\n", "", -1) + osInfo := strings.Split(osStr, " ") + gio := &Info{Kernel: osInfo[0], OSVersion: osInfo[1], Core: osInfo[1], Platform: osInfo[2], OS: osInfo[0], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} + gio.Hostname, _ = os.Hostname() + return gio +} + +func _getInfo() string { + cmd := exec.Command("uname", "-srm") + cmd.Stdin = strings.NewReader("some input") + var out bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &stderr + err := cmd.Run() + if err != nil { + fmt.Println("getInfo:", err) + } + return out.String() +} diff --git a/client/system/Info_linux.go b/client/system/Info_linux.go new file mode 100644 index 000000000..7476af794 --- /dev/null +++ b/client/system/Info_linux.go @@ -0,0 +1,76 @@ +package system + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "runtime" + "strings" + "time" +) + +func GetInfo() *Info { + info := _getInfo() + for strings.Index(info, "broken pipe") != -1 { + info = _getInfo() + time.Sleep(500 * time.Millisecond) + } + + releaseInfo := _getReleaseInfo() + for strings.Index(info, "broken pipe") != -1 { + 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.Replace(info, "\n", "", -1) + osStr = strings.Replace(osStr, "\r\n", "", -1) + osInfo := strings.Split(osStr, " ") + 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()} + gio.Hostname, _ = os.Hostname() + 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 { + fmt.Println("getInfo:", err) + } + 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 { + fmt.Println("getReleaseInfo:", err) + } + return out.String() +} diff --git a/client/system/info.go b/client/system/info.go new file mode 100644 index 000000000..1275e03ea --- /dev/null +++ b/client/system/info.go @@ -0,0 +1,14 @@ +package system + +//Info is an object that contains machine information +// Most of the code is taken from https://github.com/matishsiao/goInfo +type Info struct { + GoOS string + Kernel string + Core string + Platform string + OS string + OSVersion string + Hostname string + CPUs int +} diff --git a/client/system/info_freebsd.go b/client/system/info_freebsd.go new file mode 100644 index 000000000..9267c498d --- /dev/null +++ b/client/system/info_freebsd.go @@ -0,0 +1,39 @@ +package system + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "runtime" + "strings" + "time" +) + +func GetInfo() *Info { + out := _getInfo() + for strings.Index(out, "broken pipe") != -1 { + out = _getInfo() + time.Sleep(500 * time.Millisecond) + } + osStr := strings.Replace(out, "\n", "", -1) + osStr = strings.Replace(osStr, "\r\n", "", -1) + osInfo := strings.Split(osStr, " ") + gio := &Info{Kernel: osInfo[0], Core: osInfo[1], Platform: runtime.GOARCH, OS: osInfo[2], GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} + gio.Hostname, _ = os.Hostname() + return gio +} + +func _getInfo() string { + cmd := exec.Command("uname", "-sri") + cmd.Stdin = strings.NewReader("some input") + var out bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &stderr + err := cmd.Run() + if err != nil { + fmt.Println("getInfo:", err) + } + return out.String() +} diff --git a/client/system/info_windows.go b/client/system/info_windows.go new file mode 100644 index 000000000..d60711d6a --- /dev/null +++ b/client/system/info_windows.go @@ -0,0 +1,35 @@ +package system + +import ( + "bytes" + "os" + "os/exec" + "runtime" + "strings" +) + +func GetInfo() *Info { + cmd := exec.Command("cmd", "ver") + 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 { + panic(err) + } + osStr := strings.Replace(out.String(), "\n", "", -1) + osStr = strings.Replace(osStr, "\r\n", "", -1) + tmp1 := strings.Index(osStr, "[Version") + tmp2 := strings.Index(osStr, "]") + var ver string + if tmp1 == -1 || tmp2 == -1 { + ver = "unknown" + } else { + ver = osStr[tmp1+9 : tmp2] + } + gio := &Info{Kernel: "windows", OSVersion: ver, Core: ver, Platform: "unknown", OS: "windows", GoOS: runtime.GOOS, CPUs: runtime.NumCPU()} + gio.Hostname, _ = os.Hostname() + return gio +} diff --git a/go.mod b/go.mod index 1e079ce7a..53a62ef1c 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/google/uuid v1.2.0 github.com/gorilla/mux v1.8.0 github.com/kardianos/service v1.2.0 - github.com/matishsiao/goInfo v0.0.0-20200404012835-b5f882ee2288 github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.13.0 github.com/pion/ice/v2 v2.1.7 diff --git a/go.sum b/go.sum index edbacc149..8cd41ec23 100644 --- a/go.sum +++ b/go.sum @@ -160,8 +160,6 @@ github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdA github.com/lxn/walk v0.0.0-20210112085537-c389da54e794/go.mod h1:E23UucZGqpuUANJooIbHWCufXvOcT6E7Stq81gU+CSQ= github.com/lxn/win v0.0.0-20210218163916-a377121e959e/go.mod h1:KxxjdtRkfNoYDCUP5ryK7XJJNTnpC8atvtmTheChOtk= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/matishsiao/goInfo v0.0.0-20200404012835-b5f882ee2288 h1:cdM7et8/VlNnSBpq3KbyQWsYLCY0WsB7tvV8Fr0DUNE= -github.com/matishsiao/goInfo v0.0.0-20200404012835-b5f882ee2288/go.mod h1:yLZrFIhv+Z20hxHvcZpEyKVQp9HMsOJkXAxx7yDqtvg= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= diff --git a/management/client/client.go b/management/client/client.go index c96d7f170..747f4d081 100644 --- a/management/client/client.go +++ b/management/client/client.go @@ -4,8 +4,8 @@ import ( "context" "crypto/tls" "github.com/cenkalti/backoff/v4" - "github.com/matishsiao/goInfo" log "github.com/sirupsen/logrus" + "github.com/wiretrustee/wiretrustee/client/system" "github.com/wiretrustee/wiretrustee/encryption" "github.com/wiretrustee/wiretrustee/management/proto" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -206,12 +206,12 @@ func (c *Client) login(serverKey wgtypes.Key, req *proto.LoginRequest) (*proto.L // Takes care of encrypting and decrypting messages. // This method will also collect system info and send it with the request (e.g. hostname, os, etc) func (c *Client) Register(serverKey wgtypes.Key, setupKey string) (*proto.LoginResponse, error) { - gi := goInfo.GetInfo() + gi := system.GetInfo() meta := &proto.PeerSystemMeta{ Hostname: gi.Hostname, GoOS: gi.GoOS, OS: gi.OS, - Core: gi.Core, + Core: gi.OSVersion, Platform: gi.Platform, Kernel: gi.Kernel, WiretrusteeVersion: "", diff --git a/management/server/http/handler/peers.go b/management/server/http/handler/peers.go index 11b2b7c93..8b27adb09 100644 --- a/management/server/http/handler/peers.go +++ b/management/server/http/handler/peers.go @@ -122,6 +122,6 @@ func toPeerResponse(peer *server.Peer) *PeerResponse { IP: peer.IP.String(), Connected: peer.Status.Connected, LastSeen: peer.Status.LastSeen, - OS: fmt.Sprintf("%s %s", peer.Meta.GoOS, peer.Meta.Core), + OS: fmt.Sprintf("%s %s", peer.Meta.OS, peer.Meta.Core), } }