Skip to content

Commit

Permalink
remove http mode on fetch IP, just keep dmsg mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpalide committed Jul 13, 2024
1 parent 556a5a3 commit 3c4c8e5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 120 deletions.
89 changes: 2 additions & 87 deletions pkg/visor/visorconfig/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package visorconfig

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -234,91 +232,8 @@ func IsRoot() bool {
return userLvl.Username == "root"
}

// IPAddr struct of `ip --json addr`
type IPAddr []struct {
Ifindex int `json:"ifindex"`
Ifname string `json:"ifname"`
Flags []string `json:"flags"`
Mtu int `json:"mtu"`
Qdisc string `json:"qdisc"`
Operstate string `json:"operstate"`
Group string `json:"group"`
Txqlen int `json:"txqlen"`
LinkType string `json:"link_type"`
Address string `json:"address"`
Broadcast string `json:"broadcast"`
AddrInfo []struct {
Family string `json:"family"`
Local string `json:"local"`
Prefixlen int `json:"prefixlen"`
Scope string `json:"scope"`
Label string `json:"label,omitempty"`
ValidLifeTime int64 `json:"valid_life_time"`
PreferredLifeTime int64 `json:"preferred_life_time"`
} `json:"addr_info"`
}

// IPA returns IPAddr struct filled in with the json response from `ip --json addr` command ; fail silently on errors
func IPA() (ip *IPAddr) {
//non-critical logic implemented with bitfield/script
ipa, err := script.Exec(`ip --json addr`).String()
if err != nil {
return nil
}
err = json.Unmarshal([]byte(ipa), &ip)
if err != nil {
return nil
}
return ip
}

// IPSkycoin struct of ip.skycoin.com json
type IPSkycoin struct {
IPAddress string `json:"ip_address"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
PostalCode string `json:"postal_code"`
ContinentCode string `json:"continent_code"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
RegionCode string `json:"region_code"`
RegionName string `json:"region_name"`
ProvinceCode string `json:"province_code"`
ProvinceName string `json:"province_name"`
CityName string `json:"city_name"`
Timezone string `json:"timezone"`
}

// IPSkycoinFetch fetches the json response from ip.skycoin.com or ip.plaintext.ir
func IPSkycoinFetch() (*IPSkycoin, error) {
var ipskycoin *IPSkycoin
var resp *http.Response
var err error

resp, err = http.Get("http://ip.skycoin.com/")
if err != nil {
resp, err = http.Get("http://ip.plaintext.ir/")
if err != nil {
return ipskycoin, err
}
}
if resp.Body != nil {
defer resp.Body.Close() //nolint
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
//fill in IPSkycoin struct with the response
err = json.Unmarshal(body, &ipskycoin)
if err != nil {
return nil, err
}
return ipskycoin, nil
}

// IPSkycoinFetchDmsg fetches the ip address from dmsg
func IPSkycoinFetchDmsg(dmsgDisc string) (string, error) {
// FetchIP fetches the ip address by dmsg servers
func FetchIP(dmsgDisc string) (string, error) {
log := logging.MustGetLogger("ip_skycoin_fetch_dmsg")
ctx, cancel := cmdutil.SignalContext(context.Background(), nil)
defer cancel()
Expand Down
17 changes: 6 additions & 11 deletions pkg/visor/visorconfig/values_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type Survey struct {
SkycoinAddress string `json:"skycoin_address,omitempty"`
GOOS string `json:"go_os,omitempty"`
GOARCH string `json:"go_arch,omitempty"`
IPInfo *IPSkycoin `json:"ip.skycoin.com,omitempty"`
IPAddr *IPAddr `json:"ip_addr,omitempty"`
IPAddr string `json:"ip_addr,omitempty"`
Disks *ghw.BlockInfo `json:"ghw_blockinfo,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
SkywireVersion string `json:"skywire_version,omitempty"`
Expand All @@ -47,19 +46,15 @@ func SystemSurvey(dmsgDisc string) (Survey, error) {
if err != nil {
return Survey{}, err
}
var ipInfo IPSkycoin
fetchedIPInfo, err := IPSkycoinFetch()
if err != nil {
ipValue, err := IPSkycoinFetchDmsg(dmsgDisc)
var ipAddr string
for {
ipAddr, err = FetchIP(dmsgDisc)
if err == nil {
ipInfo.IPAddress = ipValue
break
}
} else {
ipInfo = *fetchedIPInfo
}
s := Survey{
IPInfo: &ipInfo,
IPAddr: IPA(),
IPAddr: ipAddr,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
UUID: uuid.New(),
Expand Down
18 changes: 6 additions & 12 deletions pkg/visor/visorconfig/values_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ type Survey struct {
GOOS string `json:"go_os,omitempty"`
GOARCH string `json:"go_arch,omitempty"`
SYSINFO sysinfo.SysInfo `json:"zcalusic_sysinfo,omitempty"`
IPInfo *IPSkycoin `json:"ip.skycoin.com,omitempty"`
IPAddr *IPAddr `json:"ip_addr,omitempty"`
IPAddr string `json:"ip_addr,omitempty"`
Disks *ghw.BlockInfo `json:"ghw_blockinfo,omitempty"`
Product *ghw.ProductInfo `json:"ghw_productinfo,omitempty"`
Memory *ghw.MemoryInfo `json:"ghw_memoryinfo,omitempty"`
Expand All @@ -64,21 +63,16 @@ func SystemSurvey(dmsgDisc string) (Survey, error) {
if err != nil && !strings.Contains(err.Error(), "Could not determine total usable bytes of memory") {
return Survey{}, err
}
var ipInfo IPSkycoin
fetchedIPInfo, err := IPSkycoinFetch()
if err != nil {
ipValue, err := IPSkycoinFetchDmsg(dmsgDisc)
var ipAddr string
for {
ipAddr, err = FetchIP(dmsgDisc)
if err == nil {
ipInfo.IPAddress = ipValue
break
}
} else {
ipInfo = *fetchedIPInfo
}

s := Survey{
Timestamp: time.Now(),
IPInfo: &ipInfo,
IPAddr: IPA(),
IPAddr: ipAddr,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
SYSINFO: si,
Expand Down
17 changes: 7 additions & 10 deletions pkg/visor/visorconfig/values_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type Survey struct {
SkycoinAddress string `json:"skycoin_address,omitempty"`
GOOS string `json:"go_os,omitempty"`
GOARCH string `json:"go_arch,omitempty"`
IPInfo *IPSkycoin `json:"ip.skycoin.com,omitempty"`
IPAddr *IPAddr `json:"ip_addr,omitempty"`
IPAddr string `json:"ip_addr,omitempty"`
Disks *ghw.BlockInfo `json:"ghw_blockinfo,omitempty"`
Product *ghw.ProductInfo `json:"ghw_productinfo,omitempty"`
Memory *ghw.MemoryInfo `json:"ghw_memoryinfo,omitempty"`
Expand All @@ -57,18 +56,16 @@ func SystemSurvey(dmsgDisc string) (Survey, error) {
if err != nil {
return Survey{}, err
}
var ipInfo IPSkycoin
fetchedIPInfo, err := IPSkycoinFetch()
if err != nil {
ipValue, err := IPSkycoinFetchDmsg(dmsgDisc)
var ipAddr string
for {
ipAddr, err = FetchIP(dmsgDisc)
if err == nil {
ipInfo.IPAddress = ipValue
break
}
} else {
ipInfo = *fetchedIPInfo
}

s := Survey{
IPInfo: &ipInfo,
IPAddr: ipAddr,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
UUID: uuid.New(),
Expand Down

0 comments on commit 3c4c8e5

Please sign in to comment.