diff --git a/cmd/skywire-cli/commands/rewards/calc.go b/cmd/skywire-cli/commands/rewards/calc.go index 1f27779b77..32f211d14d 100644 --- a/cmd/skywire-cli/commands/rewards/calc.go +++ b/cmd/skywire-cli/commands/rewards/calc.go @@ -119,9 +119,13 @@ Fetch uptimes: skywire-cli ut > ut.txt`, var grrInfos []nodeinfo for _, pk := range res { nodeInfo := fmt.Sprintf("%s/%s/node-info.json", hwSurveyPath, pk) - tpsn, tpsnErr := script.File(fmt.Sprintf("%s/%s/tp.json", tpsnSurveyPath, pk)).JQ(`.`).String() //nolint - ip, _ := script.File(nodeInfo).JQ(`."ip.skycoin.com".ip_address`).Replace(" ", "").Replace(`"`, "").String() //nolint + tpsn, tpsnErr := script.File(fmt.Sprintf("%s/%s/tp.json", tpsnSurveyPath, pk)).JQ(`.`).String() //nolint + ip, _ := script.File(nodeInfo).JQ(`.ip_address`).Replace(" ", "").Replace(`"`, "").String() //nolint ip = strings.TrimRight(ip, "\n") + if ip == "" { + ip, _ = script.File(nodeInfo).JQ(`."ip.skycoin.com".ip_address`).Replace(" ", "").Replace(`"`, "").String() //nolint + ip = strings.TrimRight(ip, "\n") + } sky, _ := script.File(nodeInfo).JQ(".skycoin_address").Replace(" ", "").Replace(`"`, "").String() //nolint sky = strings.TrimRight(sky, "\n") arch, _ := script.File(nodeInfo).JQ(".go_arch").Replace(" ", "").Replace(`"`, "").String() //nolint diff --git a/cmd/skywire-cli/commands/survey/root.go b/cmd/skywire-cli/commands/survey/root.go index f8372cbeac..813f20e819 100644 --- a/cmd/skywire-cli/commands/survey/root.go +++ b/cmd/skywire-cli/commands/survey/root.go @@ -2,12 +2,18 @@ package clisurvey import ( + "context" "encoding/json" "fmt" + "net/http" "os" + "github.com/skycoin/dmsg/pkg/disc" + "github.com/skycoin/dmsg/pkg/dmsg" "github.com/spf13/cobra" + "github.com/skycoin/skywire-utilities/pkg/cipher" + "github.com/skycoin/skywire-utilities/pkg/cmdutil" "github.com/skycoin/skywire-utilities/pkg/logging" "github.com/skycoin/skywire-utilities/pkg/skyenv" "github.com/skycoin/skywire/cmd/skywire-cli/internal" @@ -32,7 +38,7 @@ var ( func init() { surveyCmd.Flags().SortFlags = false surveyCmd.Flags().StringVarP(&confPath, "config", "c", "", "optionl config file to use (i.e.: "+visorconfig.ConfigName+")") - surveyCmd.Flags().StringVar(&dmsgDisc, "dmsg-disc", skyenv.DmsgDiscAddr, "value of dmsg discovery") + surveyCmd.Flags().StringVarP(&dmsgDisc, "dmsg-disc", "D", skyenv.DmsgDiscAddr, "value of dmsg discovery") // surveyCmd.Flags().StringVarP(&confArg, "confarg", "C", "", "supply config as argument") // surveyCmd.Flags().BoolVarP(&stdin, "stdin", "n", false, "read config from stdin") if _, err := os.Stat(visorconfig.SkywirePath + "/" + visorconfig.ConfigJSON); err == nil { @@ -64,19 +70,8 @@ var surveyCmd = &cobra.Command{ if usr { confPath = visorconfig.HomePath() + "/" + visorconfig.ConfigName } - // if confPath != "" && confArg != "" { - // log.Fatal("cannot specify both --config, -c and --confarg, -C") - // } - // if confPath != "" && stdin { - // log.Fatal("cannot specify both --config, -c and --stdin, -n") - // } - // if stdin && confArg != "" { - // log.Fatal("cannot specify both --confarg, -C and --stdin, -n") - // } - // if stdin || confArg != "" || confPath != "" { if confPath != "" { - // conf = initConfig() confJSON, err := os.ReadFile(confPath) //nolint if err != nil { log.WithError(err).Fatal("Failed to read config file") @@ -86,10 +81,10 @@ var surveyCmd = &cobra.Command{ log.WithError(err).Fatal("Failed to unmarshal old config json") } } - if conf != nil { - dmsgDisc = conf.Dmsg.Discovery - } - survey, err := visorconfig.SystemSurvey(dmsgDisc) + // if conf != nil { + // dmsgDisc = conf.Dmsg.Discovery + // } + survey, err := visorconfig.SystemSurvey() if err != nil { internal.Catch(cmd.Flags(), fmt.Errorf("Failed to generate system survey: %v", err)) } @@ -111,6 +106,13 @@ var surveyCmd = &cobra.Command{ survey.ServicesURLs.StunServers = conf.StunServers //survey.DmsgServers = v.dmsgC.ConnectedServersPK() } + if dmsgDisc != "" { + ipAddr, err := FetchIP(dmsgDisc) + if err == nil { + survey.IPAddr = ipAddr + } + } + s, err := json.MarshalIndent(survey, "", "\t") if err != nil { internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Could not marshal json: %v", err)) @@ -118,3 +120,44 @@ var surveyCmd = &cobra.Command{ fmt.Printf("%s", s) }, } + +// 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() + + pk, sk := cipher.GenerateKeyPair() + + dmsgC, closeDmsg, err := startDmsg(ctx, log, pk, sk, dmsgDisc) + if err != nil { + return "", fmt.Errorf("failed to start dmsg") + } + defer closeDmsg() + + ip, err := dmsgC.LookupIP(ctx, nil) + return ip.String(), err +} + +func startDmsg(ctx context.Context, log *logging.Logger, pk cipher.PubKey, sk cipher.SecKey, dmsgDisc string) (dmsgC *dmsg.Client, stop func(), err error) { + dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(dmsgDisc, &http.Client{}, log), &dmsg.Config{MinSessions: dmsg.DefaultMinSessions}) + go dmsgC.Serve(context.Background()) + + stop = func() { + err := dmsgC.Close() + log.WithError(err).Debug("Disconnected from dmsg network.") + fmt.Printf("\n") + } + log.WithField("public_key", pk.String()).WithField("dmsg_disc", dmsgDisc). + Debug("Connecting to dmsg network...") + + select { + case <-ctx.Done(): + stop() + return nil, nil, ctx.Err() + + case <-dmsgC.Ready(): + log.Debug("Dmsg network ready.") + return dmsgC, stop, nil + } +} diff --git a/go.sum b/go.sum index 7ec3e7fbdf..1cf0fb71e6 100644 --- a/go.sum +++ b/go.sum @@ -682,8 +682,6 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/skycoin/dmsg v1.3.22-0.20240702162730-1c2fcecd4bb6 h1:q+KeZyHWctCahSUbZ6b+iy1VQNHVKN2YZc7J2R0VIHw= -github.com/skycoin/dmsg v1.3.22-0.20240702162730-1c2fcecd4bb6/go.mod h1:72MC0HFDxKYqMLZ2RWGY/ZDNFq6965SP1PIrKlYqaiQ= github.com/skycoin/dmsg v1.3.24 h1:aDa36UucXcWQCnHri1q5UKAVydomGDg28osBWlpUoOI= github.com/skycoin/dmsg v1.3.24/go.mod h1:72MC0HFDxKYqMLZ2RWGY/ZDNFq6965SP1PIrKlYqaiQ= github.com/skycoin/noise v0.0.0-20180327030543-2492fe189ae6 h1:1Nc5EBY6pjfw1kwW0duwyG+7WliWz5u9kgk1h5MnLuA= diff --git a/pkg/visor/survey.go b/pkg/visor/survey.go index 4cfe17ad8e..7574b2a673 100644 --- a/pkg/visor/survey.go +++ b/pkg/visor/survey.go @@ -2,6 +2,7 @@ package visor import ( + "context" "encoding/json" "os" "strings" @@ -32,7 +33,7 @@ func GenerateSurvey(v *Visor, log *logging.Logger, routine bool) { log.Info("Skycoin reward address: ", cAddr.String()) //generate the system survey pathutil.EnsureDir(v.conf.LocalPath) //nolint - survey, err := visconf.SystemSurvey(v.conf.Dmsg.Discovery) + survey, err := visconf.SystemSurvey() if err != nil { log.WithError(err).Error("Could not read system info.") return @@ -51,6 +52,18 @@ func GenerateSurvey(v *Visor, log *logging.Logger, routine bool) { survey.ServicesURLs.StunServers = v.conf.StunServers survey.DmsgServers = v.dmsgC.ConnectedServersPK() + //use the existing dmsg client of the visor to get ip from dmsg server + tries := 8 + for tries > 0 { + ipAddr, err := v.dmsgC.LookupIP(context.Background(), nil) + if err != nil { + tries-- + continue + } + survey.IPAddr = ipAddr.String() + break + } + log.Info("Generating system survey") v.surveyLock.Lock() v.survey = survey @@ -71,7 +84,7 @@ func GenerateSurvey(v *Visor, log *logging.Logger, routine bool) { v.surveyLock.Lock() v.survey = visconf.Survey{} v.surveyLock.Unlock() - log.Debug("Removed hadware survey for visor not seeking rewards") + log.Debug("Removed survey for visor not seeking rewards") } // break loop for generate each 24hours if just reward address chenged if !routine { diff --git a/pkg/visor/visorconfig/values.go b/pkg/visor/visorconfig/values.go index da65029bee..4fa7cb9863 100644 --- a/pkg/visor/visorconfig/values.go +++ b/pkg/visor/visorconfig/values.go @@ -2,9 +2,7 @@ package visorconfig import ( - "context" "fmt" - "net/http" "os" "os/exec" "os/user" @@ -12,13 +10,10 @@ import ( "strings" "github.com/bitfield/script" - "github.com/skycoin/dmsg/pkg/disc" "github.com/skycoin/dmsg/pkg/dmsg" "github.com/skycoin/skywire-utilities/pkg/buildinfo" "github.com/skycoin/skywire-utilities/pkg/cipher" - "github.com/skycoin/skywire-utilities/pkg/cmdutil" - "github.com/skycoin/skywire-utilities/pkg/logging" "github.com/skycoin/skywire/pkg/skyenv" ) @@ -232,47 +227,6 @@ func IsRoot() bool { return userLvl.Username == "root" } -// 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() - - pk, sk := cipher.GenerateKeyPair() - - dmsgC, closeDmsg, err := startDmsg(ctx, log, pk, sk, dmsgDisc) - if err != nil { - return "", fmt.Errorf("failed to start dmsg") - } - defer closeDmsg() - - ip, err := dmsgC.LookupIP(ctx, nil) - return ip.String(), err -} - -func startDmsg(ctx context.Context, log *logging.Logger, pk cipher.PubKey, sk cipher.SecKey, dmsgDisc string) (dmsgC *dmsg.Client, stop func(), err error) { - dmsgC = dmsg.NewClient(pk, sk, disc.NewHTTP(dmsgDisc, &http.Client{}, log), &dmsg.Config{MinSessions: dmsg.DefaultMinSessions}) - go dmsgC.Serve(context.Background()) - - stop = func() { - err := dmsgC.Close() - log.WithError(err).Debug("Disconnected from dmsg network.") - fmt.Printf("\n") - } - log.WithField("public_key", pk.String()).WithField("dmsg_disc", dmsgDisc). - Debug("Connecting to dmsg network...") - - select { - case <-ctx.Done(): - stop() - return nil, nil, ctx.Err() - - case <-dmsgC.Ready(): - log.Debug("Dmsg network ready.") - return dmsgC, stop, nil - } -} - var ( // VisorConfigFile will contain the path to the visor's config or `stdin` to denote that the config was read from STDIN VisorConfigFile string diff --git a/pkg/visor/visorconfig/values_darwin.go b/pkg/visor/visorconfig/values_darwin.go index 4101ebe4ff..bc0e7e2eab 100644 --- a/pkg/visor/visorconfig/values_darwin.go +++ b/pkg/visor/visorconfig/values_darwin.go @@ -41,20 +41,20 @@ type Survey struct { } // SystemSurvey returns system survey -func SystemSurvey(dmsgDisc string) (Survey, error) { +func SystemSurvey() (Survey, error) { disks, err := ghw.Block(ghw.WithDisableWarnings()) if err != nil { return Survey{}, err } - var ipAddr string - for { - ipAddr, err = FetchIP(dmsgDisc) - if err == nil { - break - } - } + // var ipAddr string + // for { + // ipAddr, err = FetchIP(dmsgDisc) + // if err == nil { + // break + // } + // } s := Survey{ - IPAddr: ipAddr, + // IPAddr: ipAddr, GOOS: runtime.GOOS, GOARCH: runtime.GOARCH, UUID: uuid.New(), diff --git a/pkg/visor/visorconfig/values_linux.go b/pkg/visor/visorconfig/values_linux.go index b693feb768..33f675503b 100644 --- a/pkg/visor/visorconfig/values_linux.go +++ b/pkg/visor/visorconfig/values_linux.go @@ -48,7 +48,7 @@ type Survey struct { } // SystemSurvey returns system survey -func SystemSurvey(dmsgDisc string) (Survey, error) { +func SystemSurvey() (Survey, error) { var si sysinfo.SysInfo si.GetSysInfo() disks, err := ghw.Block(ghw.WithDisableWarnings()) @@ -63,16 +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 ipAddr string - for { - ipAddr, err = FetchIP(dmsgDisc) - if err == nil { - break - } - } + // var ipAddr string + // for { + // ipAddr, err = FetchIP(dmsgDisc) + // if err == nil { + // break + // } + // } s := Survey{ - Timestamp: time.Now(), - IPAddr: ipAddr, + Timestamp: time.Now(), + // IPAddr: ipAddr, GOOS: runtime.GOOS, GOARCH: runtime.GOARCH, SYSINFO: si, diff --git a/pkg/visor/visorconfig/values_windows.go b/pkg/visor/visorconfig/values_windows.go index a071a03c3d..a18312893f 100644 --- a/pkg/visor/visorconfig/values_windows.go +++ b/pkg/visor/visorconfig/values_windows.go @@ -43,7 +43,7 @@ type Survey struct { } // SystemSurvey returns system survey -func SystemSurvey(dmsgDisc string) (Survey, error) { +func SystemSurvey() (Survey, error) { disks, err := ghw.Block(ghw.WithDisableWarnings()) if err != nil { return Survey{}, err @@ -56,16 +56,16 @@ func SystemSurvey(dmsgDisc string) (Survey, error) { if err != nil { return Survey{}, err } - var ipAddr string - for { - ipAddr, err = FetchIP(dmsgDisc) - if err == nil { - break - } - } + // var ipAddr string + // for { + // ipAddr, err = FetchIP(dmsgDisc) + // if err == nil { + // break + // } + // } s := Survey{ - IPAddr: ipAddr, + // IPAddr: ipAddr, GOOS: runtime.GOOS, GOARCH: runtime.GOARCH, UUID: uuid.New(),