Skip to content

Commit

Permalink
Merge pull request #1895 from mrpalide/fix/mac-survey-fix
Browse files Browse the repository at this point in the history
improve and fix Mac survey
  • Loading branch information
mrpalide authored Oct 30, 2024
2 parents 93ecf50 + f50db9d commit 3d2d278
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
37 changes: 29 additions & 8 deletions pkg/visor/visorconfig/values_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package visorconfig

import (
"net"
"os/user"
"runtime"
"time"

"github.com/google/uuid"
"github.com/jaypipes/ghw"
"github.com/zcalusic/sysinfo"

"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire/pkg/skyenv"
Expand All @@ -29,10 +32,12 @@ func UserConfig() skyenv.PkgConfig {

// Survey system hardware survey struct
type Survey struct {
Timestamp time.Time `json:"timestamp"`
PubKey cipher.PubKey `json:"public_key,omitempty"`
SkycoinAddress string `json:"skycoin_address,omitempty"`
GOOS string `json:"go_os,omitempty"`
GOARCH string `json:"go_arch,omitempty"`
SYSINFO customSysinfo `json:"zcalusic_sysinfo,omitempty"`
IPAddr string `json:"ip_address,omitempty"`
Disks *ghw.BlockInfo `json:"ghw_blockinfo,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
Expand All @@ -47,17 +52,11 @@ func SystemSurvey() (Survey, error) {
if err != nil {
return Survey{}, err
}
// var ipAddr string
// for {
// ipAddr, err = FetchIP(dmsgDisc)
// if err == nil {
// break
// }
// }
s := Survey{
// IPAddr: ipAddr,
Timestamp: time.Now(),
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
SYSINFO: getMacAddr(),
UUID: uuid.New(),
Disks: disks,
SkywireVersion: Version(),
Expand All @@ -70,3 +69,25 @@ func IsRoot() bool {
userLvl, _ := user.Current() //nolint
return userLvl.Username == "root"
}

type customSysinfo struct {
Network []sysinfo.NetworkDevice `json:"network,omitempty"`
}

func getMacAddr() customSysinfo {
var sysInfo customSysinfo
si := make([]sysinfo.NetworkDevice, 1)
interfaces, err := net.Interfaces()
if err != nil {
return sysInfo
}

for _, ifa := range interfaces {
si[0].MACAddress = ifa.HardwareAddr.String()
if si[0].MACAddress != "" {
sysInfo.Network = si
return sysInfo
}
}
return sysInfo
}
11 changes: 2 additions & 9 deletions pkg/visor/visorconfig/values_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,9 @@ func SystemSurvey() (Survey, error) {
if err != nil && !strings.Contains(err.Error(), "Could not determine total usable bytes of memory") {
return Survey{}, err
}
// var ipAddr string
// for {
// ipAddr, err = FetchIP(dmsgDisc)
// if err == nil {
// break
// }
// }

s := Survey{
Timestamp: time.Now(),
// IPAddr: ipAddr,
Timestamp: time.Now(),
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
SYSINFO: si,
Expand Down
38 changes: 17 additions & 21 deletions pkg/visor/visorconfig/values_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"runtime"
"time"

"golang.org/x/sys/windows"

Expand All @@ -32,11 +33,12 @@ func UserConfig() skyenv.PkgConfig {

// Survey system hardware survey struct
type Survey struct {
Timestamp time.Time `json:"timestamp"`
PubKey cipher.PubKey `json:"public_key,omitempty"`
SkycoinAddress string `json:"skycoin_address,omitempty"`
GOOS string `json:"go_os,omitempty"`
GOARCH string `json:"go_arch,omitempty"`
SYSINFO []NetworkDevice `json:"zcalusic_sysinfo,omitempty"`
SYSINFO customSysinfo `json:"zcalusic_sysinfo,omitempty"`
IPAddr string `json:"ip_address,omitempty"`
Disks *ghw.BlockInfo `json:"ghw_blockinfo,omitempty"`
Product *ghw.ProductInfo `json:"ghw_productinfo,omitempty"`
Expand All @@ -61,16 +63,9 @@ func SystemSurvey() (Survey, error) {
if err != nil {
return Survey{}, err
}
// var ipAddr string
// for {
// ipAddr, err = FetchIP(dmsgDisc)
// if err == nil {
// break
// }
// }

s := Survey{
// IPAddr: ipAddr,
Timestamp: time.Now(),
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
SYSINFO: getMacAddr(),
Expand All @@ -83,28 +78,29 @@ func SystemSurvey() (Survey, error) {
return s, nil
}

type NetworkDevice struct {
Name string `json:"name,omitempty"`
Driver string `json:"driver,omitempty"`
type customSysinfo struct {
Network []networkDevice `json:"network,omitempty"`
}
type networkDevice struct {
MACAddress string `json:"macaddress,omitempty"`
Port string `json:"port,omitempty"`
Speed uint `json:"speed,omitempty"` // device max supported speed in Mbps
}

func getMacAddr() []NetworkDevice {
si := make([]NetworkDevice, 1)
ifas, err := net.Interfaces()
func getMacAddr() customSysinfo {
var sysInfo customSysinfo
si := make([]networkDevice, 1)
interfaces, err := net.Interfaces()
if err != nil {
return nil
return sysInfo
}

for _, ifa := range ifas {
for _, ifa := range interfaces {
si[0].MACAddress = ifa.HardwareAddr.String()
if si[0].MACAddress != "" {
return si
sysInfo.Network = si
return sysInfo
}
}
return nil
return sysInfo
}

// IsRoot checks for root permissions
Expand Down

0 comments on commit 3d2d278

Please sign in to comment.