From 9ff6b3aaa26e0065f43e8bb4a359372b672fcf81 Mon Sep 17 00:00:00 2001 From: Erson Pereira <38801159+ersonp@users.noreply.github.com> Date: Mon, 24 Oct 2022 18:56:52 +0530 Subject: [PATCH] Add display node ip field to the main config (#1392) * Add display node ip field to the main config This commit adds the fiels DisplayNodeIP to the main config in order to send it to the service discovery. * Minor fix * Add publicip flag to config gen This commit adds a new flag publicip to config gen. We remove the fame flag from priv as it is moved from the private config to the main one. * Fix json output of vpn start subcommand * Update changelog * Fix tp ls command * Fix lsTpCmd --- CHANGELOG.md | 2 +- cmd/skywire-cli/commands/config/gen.go | 4 +++ cmd/skywire-cli/commands/config/private.go | 3 --- cmd/skywire-cli/commands/config/root.go | 1 + cmd/skywire-cli/commands/visor/privacy.go | 4 +-- cmd/skywire-cli/commands/visor/transports.go | 5 ++-- cmd/skywire-cli/commands/vpn/vvpn.go | 26 ++++++++++++-------- cmd/skywire-cli/internal/internal.go | 16 ++++++------ pkg/app/appdisc/factory.go | 12 +++++---- pkg/app/launcher/launcher.go | 11 +++++---- pkg/servicedisc/client.go | 18 ++++++++------ pkg/servicedisc/types.go | 17 +++++++------ pkg/visor/api.go | 9 ++++--- pkg/visor/init.go | 23 +++++++++-------- pkg/visor/privacyconfig/privacy.go | 1 - pkg/visor/visorconfig/config.go | 9 ++++--- pkg/visor/visorconfig/v1.go | 23 +++++++++-------- 17 files changed, 103 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fdc65195..3c81f8bd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,9 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `log_store` field to `transport` in config [#1386](https://github.com/skycoin/skywire/pull/1386) - `type`, `location`, `rotation_interval`, field to `log_store` inside `transport` in config [#1374](https://github.com/skycoin/skywire/pull/1374) - transport file logging to CSV [#1374](https://github.com/skycoin/skywire/pull/1374) -- transport file logging to CSV [#1374](https://github.com/skycoin/skywire/pull/1374) - `skywire-cli config priv` & `skywire-cli visor priv` subcommands and rpc [#1369](https://github.com/skycoin/skywire/issues/1369) - dmsghttp server [#1364](https://github.com/skycoin/skywire/issues/1364) +- `display_node_ip` field to `launcher` in config [#1392](https://github.com/skycoin/skywire/pull/1392) ### Changed - moved `skywire-cli visor` subcommands into `skywire-cil visor hv` [#1390](https://github.com/skycoin/skywire/pull/1390) diff --git a/cmd/skywire-cli/commands/config/gen.go b/cmd/skywire-cli/commands/config/gen.go index 7818d4e68..ca2af851d 100644 --- a/cmd/skywire-cli/commands/config/gen.go +++ b/cmd/skywire-cli/commands/config/gen.go @@ -79,6 +79,7 @@ func init() { gHiddenFlags = append(gHiddenFlags, "hide") genConfigCmd.Flags().BoolVarP(&isPublic, "public", "z", false, "publicize visor in service discovery") gHiddenFlags = append(gHiddenFlags, "public") + genConfigCmd.Flags().BoolVar(&displayNodeIP, "publicip", false, "display node ip") genConfigCmd.Flags().StringVar(&ver, "version", "", "custom version testing override") gHiddenFlags = append(gHiddenFlags, "version") genConfigCmd.Flags().BoolVar(&isAll, "all", false, "show all flags") @@ -357,6 +358,9 @@ var genConfigCmd = &cobra.Command{ conf.IsPublic = true } + if displayNodeIP { + conf.Launcher.DisplayNodeIP = true + } //don't write file with stdout if !isStdout { // Save config to file. diff --git a/cmd/skywire-cli/commands/config/private.go b/cmd/skywire-cli/commands/config/private.go index ac1cbf9cc..6a3b58c15 100644 --- a/cmd/skywire-cli/commands/config/private.go +++ b/cmd/skywire-cli/commands/config/private.go @@ -13,7 +13,6 @@ import ( ) var ( - displayNodeIP bool rewardAddress string ) @@ -23,7 +22,6 @@ func init() { RootCmd.AddCommand(privacyConfigCmd) privacyConfigCmd.AddCommand(setPrivacyConfigCmd) privacyConfigCmd.AddCommand(getPrivacyConfigCmd) - setPrivacyConfigCmd.Flags().BoolVarP(&displayNodeIP, "publicip", "i", false, "display node ip") // default is genesis address for skycoin blockchain ; for testing setPrivacyConfigCmd.Flags().StringVarP(&rewardAddress, "address", "a", "2jBbGxZRGoQG1mqhPBnXnLTxK6oxsTf8os6", "reward address") @@ -70,7 +68,6 @@ var setPrivacyConfigCmd = &cobra.Command{ } confP := &privacyconfig.Privacy{ - DisplayNodeIP: displayNodeIP, RewardAddress: cAddr.String(), } diff --git a/cmd/skywire-cli/commands/config/root.go b/cmd/skywire-cli/commands/config/root.go index 0bc01a233..e67c77d84 100644 --- a/cmd/skywire-cli/commands/config/root.go +++ b/cmd/skywire-cli/commands/config/root.go @@ -74,6 +74,7 @@ var ( isUsr bool isPublic bool isPublicAutoConn bool + displayNodeIP bool ) // RootCmd contains commands that interact with the config of local skywire-visor diff --git a/cmd/skywire-cli/commands/visor/privacy.go b/cmd/skywire-cli/commands/visor/privacy.go index 26aa6c545..8fdfb3dca 100644 --- a/cmd/skywire-cli/commands/visor/privacy.go +++ b/cmd/skywire-cli/commands/visor/privacy.go @@ -13,7 +13,6 @@ import ( ) var ( - displayNodeIP bool rewardAddress string ) @@ -23,7 +22,6 @@ func init() { privacyCmd.Flags().SortFlags = false privacyCmd.AddCommand(setPrivacyCmd) privacyCmd.AddCommand(getPrivacyCmd) - setPrivacyCmd.Flags().BoolVarP(&displayNodeIP, "publicip", "i", false, "display node ip") // default is genesis address for skycoin blockchain ; for testing setPrivacyCmd.Flags().StringVarP(&rewardAddress, "address", "a", "2jBbGxZRGoQG1mqhPBnXnLTxK6oxsTf8os6", "reward address") } @@ -46,7 +44,7 @@ var setPrivacyCmd = &cobra.Command{ internal.PrintFatalError(cmd.Flags(), fmt.Errorf("invalid address specified: %v", err)) } - pConfig, err := client.SetPrivacy(&privacyconfig.Privacy{DisplayNodeIP: displayNodeIP, RewardAddress: cAddr.String()}) + pConfig, err := client.SetPrivacy(&privacyconfig.Privacy{RewardAddress: cAddr.String()}) if err != nil { internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err)) } diff --git a/cmd/skywire-cli/commands/visor/transports.go b/cmd/skywire-cli/commands/visor/transports.go index e7413d399..9ac8c6eec 100644 --- a/cmd/skywire-cli/commands/visor/transports.go +++ b/cmd/skywire-cli/commands/visor/transports.go @@ -78,8 +78,9 @@ var lsTpCmd = &cobra.Command{ Long: "\n Available transports\n\n displays transports of the local visor", Run: func(cmd *cobra.Command, _ []string) { var pks cipher.PubKeys - - internal.Catch(cmd.Flags(), pks.Set(strings.Join(filterPubKeys, ","))) + if filterPubKeys != nil { + internal.Catch(cmd.Flags(), pks.Set(strings.Join(filterPubKeys, ","))) + } transports, err := clirpc.Client(cmd.Flags()).Transports(filterTypes, pks, showLogs) internal.Catch(cmd.Flags(), err) PrintTransports(cmd.Flags(), transports...) diff --git a/cmd/skywire-cli/commands/vpn/vvpn.go b/cmd/skywire-cli/commands/vpn/vvpn.go index 76e0ce6d0..54db29dd4 100644 --- a/cmd/skywire-cli/commands/vpn/vvpn.go +++ b/cmd/skywire-cli/commands/vpn/vvpn.go @@ -147,32 +147,38 @@ var vpnStartCmd = &cobra.Command{ var pk cipher.PubKey internal.Catch(cmd.Flags(), pk.Set(args[0])) internal.Catch(cmd.Flags(), clirpc.Client(cmd.Flags()).StartVPNClient(pk)) - internal.PrintOutput(cmd.Flags(), "Starting.", "Starting.") + internal.PrintOutput(cmd.Flags(), nil, "Starting.") startProcess := true for startProcess { time.Sleep(time.Second * 1) - internal.PrintOutput(cmd.Flags(), ".", ".") + internal.PrintOutput(cmd.Flags(), nil, ".") states, err := clirpc.Client(cmd.Flags()).Apps() internal.Catch(cmd.Flags(), err) - var b bytes.Buffer - w := tabwriter.NewWriter(&b, 0, 0, 5, ' ', tabwriter.TabIndent) - internal.Catch(cmd.Flags(), err) + type output struct { + CurrentIP string `json:"current_ip,omitempty"` + AppError string `json:"app_error,omitempty"` + } + for _, state := range states { if state.Name == "vpn-client" { if state.Status == appserver.AppStatusRunning { startProcess = false - internal.Catch(cmd.Flags(), w.Flush()) - internal.PrintOutput(cmd.Flags(), "\nRunning!", fmt.Sprintln("\nRunning!")) + internal.PrintOutput(cmd.Flags(), nil, fmt.Sprintln("\nRunning!")) ip, err := visor.GetIP() + out := output{ + CurrentIP: ip, + } if err == nil { - internal.PrintOutput(cmd.Flags(), fmt.Sprintf("\nYour current IP: %s", ip), fmt.Sprintf("Your current IP: %s\n", ip)) + internal.PrintOutput(cmd.Flags(), out, fmt.Sprintf("Your current IP: %s\n", ip)) } } if state.Status == appserver.AppStatusErrored { startProcess = false - internal.Catch(cmd.Flags(), w.Flush()) - internal.PrintOutput(cmd.Flags(), "\nError! > "+state.DetailedStatus, fmt.Sprintln("\nError! > "+state.DetailedStatus)) + out := output{ + AppError: state.DetailedStatus, + } + internal.PrintOutput(cmd.Flags(), out, fmt.Sprintln("\nError! > "+state.DetailedStatus)) } } } diff --git a/cmd/skywire-cli/internal/internal.go b/cmd/skywire-cli/internal/internal.go index 21eb3c1b0..e31094ac8 100644 --- a/cmd/skywire-cli/internal/internal.go +++ b/cmd/skywire-cli/internal/internal.go @@ -71,14 +71,16 @@ type CLIOutput struct { func PrintOutput(cmdFlags *pflag.FlagSet, outputJSON, output interface{}) { isJSON, _ := cmdFlags.GetBool(JSONString) //nolint:errcheck if isJSON { - outputJSON := CLIOutput{ - Output: outputJSON, + if outputJSON != nil { + outputJSON := CLIOutput{ + Output: outputJSON, + } + b, err := json.MarshalIndent(outputJSON, "", " ") + if err != nil { + fmt.Println(err) + } + fmt.Print(string(b) + "\n") } - b, err := json.MarshalIndent(outputJSON, "", " ") - if err != nil { - fmt.Println(err) - } - fmt.Print(string(b) + "\n") return } if output != "" { diff --git a/pkg/app/appdisc/factory.go b/pkg/app/appdisc/factory.go index fd0e94fab..429c917b1 100644 --- a/pkg/app/appdisc/factory.go +++ b/pkg/app/appdisc/factory.go @@ -20,6 +20,7 @@ type Factory struct { PK cipher.PubKey SK cipher.SecKey ServiceDisc string // Address of service-discovery + DisplayNodeIP bool Client *http.Client ClientPublicIP string } @@ -41,11 +42,12 @@ func (f *Factory) VisorUpdater(port uint16) Updater { } conf := servicedisc.Config{ - Type: servicedisc.ServiceTypeVisor, - PK: f.PK, - SK: f.SK, - Port: port, - DiscAddr: f.ServiceDisc, + Type: servicedisc.ServiceTypeVisor, + PK: f.PK, + SK: f.SK, + Port: port, + DiscAddr: f.ServiceDisc, + DisplayNodeIP: f.DisplayNodeIP, } return &serviceUpdater{ diff --git a/pkg/app/launcher/launcher.go b/pkg/app/launcher/launcher.go index 4e34d771b..da5e87513 100644 --- a/pkg/app/launcher/launcher.go +++ b/pkg/app/launcher/launcher.go @@ -36,11 +36,12 @@ var ( // Config configures the launcher. type Config struct { - VisorPK cipher.PubKey - Apps []appserver.AppConfig - ServerAddr string - BinPath string - LocalPath string + VisorPK cipher.PubKey + Apps []appserver.AppConfig + ServerAddr string + BinPath string + LocalPath string + DisplayNodeIP bool } // Launcher is responsible for launching and keeping track of app states. diff --git a/pkg/servicedisc/client.go b/pkg/servicedisc/client.go index c50619b90..8c3f17796 100644 --- a/pkg/servicedisc/client.go +++ b/pkg/servicedisc/client.go @@ -35,11 +35,12 @@ const ( // Config configures the HTTPClient. type Config struct { - Type string - PK cipher.PubKey - SK cipher.SecKey - Port uint16 - DiscAddr string + Type string + PK cipher.PubKey + SK cipher.SecKey + Port uint16 + DiscAddr string + DisplayNodeIP bool } // HTTPClient is responsible for interacting with the service-discovery @@ -60,9 +61,10 @@ func NewClient(log logrus.FieldLogger, mLog *logging.MasterLogger, conf Config, mLog: mLog, conf: conf, entry: Service{ - Addr: NewSWAddr(conf.PK, conf.Port), - Type: conf.Type, - Version: buildinfo.Version(), + Addr: NewSWAddr(conf.PK, conf.Port), + Type: conf.Type, + Version: buildinfo.Version(), + DisplayNodeIP: conf.DisplayNodeIP, }, client: client, clientPublicIP: clientPublicIP, diff --git a/pkg/servicedisc/types.go b/pkg/servicedisc/types.go index c9a1bb0e1..52383c5f5 100644 --- a/pkg/servicedisc/types.go +++ b/pkg/servicedisc/types.go @@ -117,14 +117,15 @@ func (a SWAddr) Value() (driver.Value, error) { // Service represents a service entry in service-discovery. type Service struct { - ID uint `json:"-" gorm:"primarykey"` - CreatedAt time.Time `json:"-"` - Addr SWAddr `json:"address"` - Type string `json:"type"` - Geo *geo.LocationData `json:"geo,omitempty" gorm:"embedded"` - Version string `json:"version,omitempty"` - LocalIPs pq.StringArray `json:"local_ips,omitempty" gorm:"type:text[]"` - Info *VPNInfo `json:"info,omitempty" gorm:"-"` + ID uint `json:"-" gorm:"primarykey"` + CreatedAt time.Time `json:"-"` + Addr SWAddr `json:"address"` + Type string `json:"type"` + Geo *geo.LocationData `json:"geo,omitempty" gorm:"embedded"` + DisplayNodeIP bool `json:"display_node_ip,omitempty"` + Version string `json:"version,omitempty"` + LocalIPs pq.StringArray `json:"local_ips,omitempty" gorm:"type:text[]"` + Info *VPNInfo `json:"info,omitempty" gorm:"-"` } // VPNInfo used for showing VPN metrics info, like latency, uptime and count of connections diff --git a/pkg/visor/api.go b/pkg/visor/api.go index 25919a527..6d413ce9d 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -659,10 +659,11 @@ func (v *Visor) VPNServers(version, country string) ([]servicedisc.Service, erro vLog.SetLevel(logrus.InfoLevel) sdClient := servicedisc.NewClient(log, vLog, servicedisc.Config{ - Type: servicedisc.ServiceTypeVPN, - PK: v.conf.PK, - SK: v.conf.SK, - DiscAddr: v.conf.Launcher.ServiceDisc, + Type: servicedisc.ServiceTypeVPN, + PK: v.conf.PK, + SK: v.conf.SK, + DiscAddr: v.conf.Launcher.ServiceDisc, + DisplayNodeIP: v.conf.Launcher.DisplayNodeIP, }, &http.Client{Timeout: time.Duration(20) * time.Second}, "") vpnServers, err := sdClient.Services(context.Background(), 0, version, country) if err != nil { diff --git a/pkg/visor/init.go b/pkg/visor/init.go index 03f13f226..3b2b58a55 100644 --- a/pkg/visor/init.go +++ b/pkg/visor/init.go @@ -258,6 +258,7 @@ func initDiscovery(ctx context.Context, v *Visor, log *logging.Logger) error { factory.PK = v.conf.PK factory.SK = v.conf.SK factory.ServiceDisc = conf.ServiceDisc + factory.DisplayNodeIP = conf.DisplayNodeIP factory.Client = httpC // only needed for dmsghttp pIP, err := getPublicIP(v, conf.ServiceDisc) @@ -709,11 +710,12 @@ func initLauncher(ctx context.Context, v *Visor, log *logging.Logger) error { // Prepare launcher. launchConf := launcher.Config{ - VisorPK: v.conf.PK, - Apps: conf.Apps, - ServerAddr: conf.ServerAddr, - BinPath: conf.BinPath, - LocalPath: v.conf.LocalPath, + VisorPK: v.conf.PK, + Apps: conf.Apps, + ServerAddr: conf.ServerAddr, + BinPath: conf.BinPath, + LocalPath: v.conf.LocalPath, + DisplayNodeIP: conf.DisplayNodeIP, } launchLog := v.MasterLogger().PackageLogger("launcher") @@ -1081,11 +1083,12 @@ func initPublicAutoconnect(ctx context.Context, v *Visor, log *logging.Logger) e // advertising oneself and requires things like port that are not used // in connecting to services conf := servicedisc.Config{ - Type: servicedisc.ServiceTypeVisor, - PK: v.conf.PK, - SK: v.conf.SK, - Port: uint16(0), - DiscAddr: serviceDisc, + Type: servicedisc.ServiceTypeVisor, + PK: v.conf.PK, + SK: v.conf.SK, + Port: uint16(0), + DiscAddr: serviceDisc, + DisplayNodeIP: v.conf.Launcher.DisplayNodeIP, } // only needed for dmsghttp pIP, err := getPublicIP(v, serviceDisc) diff --git a/pkg/visor/privacyconfig/privacy.go b/pkg/visor/privacyconfig/privacy.go index 3d6266134..41bc0dfdb 100644 --- a/pkg/visor/privacyconfig/privacy.go +++ b/pkg/visor/privacyconfig/privacy.go @@ -10,7 +10,6 @@ import ( // Privacy represents the json-encoded contents of the privacy.json file type Privacy struct { - DisplayNodeIP bool `json:"display_node_ip"` RewardAddress string `json:"reward_address,omitempty"` } diff --git a/pkg/visor/visorconfig/config.go b/pkg/visor/visorconfig/config.go index 3ffd0252e..aa545e587 100644 --- a/pkg/visor/visorconfig/config.go +++ b/pkg/visor/visorconfig/config.go @@ -78,10 +78,11 @@ func MakeBaseConfig(common *Common, testEnv bool, dmsgHTTP bool, services *Servi RouteFinderTimeout: DefaultTimeout, } conf.Launcher = &Launcher{ - ServiceDisc: services.ServiceDiscovery, //utilenv.ServiceDiscAddr, - Apps: nil, - ServerAddr: skyenv.AppSrvAddr, - BinPath: skyenv.AppBinPath, + ServiceDisc: services.ServiceDiscovery, //utilenv.ServiceDiscAddr, + Apps: nil, + ServerAddr: skyenv.AppSrvAddr, + BinPath: skyenv.AppBinPath, + DisplayNodeIP: false, } conf.UptimeTracker = &UptimeTracker{ Addr: services.UptimeTracker, //utilenv.UptimeTrackerAddr, diff --git a/pkg/visor/visorconfig/v1.go b/pkg/visor/visorconfig/v1.go index 73e7ea20c..dcd9643c6 100644 --- a/pkg/visor/visorconfig/v1.go +++ b/pkg/visor/visorconfig/v1.go @@ -81,10 +81,11 @@ type UptimeTracker struct { // Launcher configures the app appserver. type Launcher struct { - ServiceDisc string `json:"service_discovery"` - Apps []appserver.AppConfig `json:"apps"` - ServerAddr string `json:"server_addr"` - BinPath string `json:"bin_path"` + ServiceDisc string `json:"service_discovery"` + Apps []appserver.AppConfig `json:"apps"` + ServerAddr string `json:"server_addr"` + BinPath string `json:"bin_path"` + DisplayNodeIP bool `json:"display_node_ip"` } // Flush flushes the config to file (if specified). @@ -117,9 +118,10 @@ func (v1 *V1) UpdateAppAutostart(launch *launcher.Launcher, appName string, auto } launch.ResetConfig(launcher.Config{ - VisorPK: v1.PK, - Apps: conf.Apps, - ServerAddr: conf.ServerAddr, + VisorPK: v1.PK, + Apps: conf.Apps, + ServerAddr: conf.ServerAddr, + DisplayNodeIP: conf.DisplayNodeIP, }) return v1.flush(v1) } @@ -147,9 +149,10 @@ func (v1 *V1) UpdateAppArg(launch *launcher.Launcher, appName, argName string, v } launch.ResetConfig(launcher.Config{ - VisorPK: v1.PK, - Apps: conf.Apps, - ServerAddr: conf.ServerAddr, + VisorPK: v1.PK, + Apps: conf.Apps, + ServerAddr: conf.ServerAddr, + DisplayNodeIP: conf.DisplayNodeIP, }) return v1.flush(v1)