From 8d33d8c8893d9d780103e3c5a7f14942188cdd7b Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Thu, 23 Nov 2023 23:57:26 +0000 Subject: [PATCH 01/10] improve output of skywire-cli proxy status command --- cmd/skywire-cli/commands/proxy/proxy.go | 34 ++++++++++++++++++++----- cmd/skywire-cli/commands/proxy/root.go | 1 + 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/cmd/skywire-cli/commands/proxy/proxy.go b/cmd/skywire-cli/commands/proxy/proxy.go index 83d5866260..37f5d28329 100644 --- a/cmd/skywire-cli/commands/proxy/proxy.go +++ b/cmd/skywire-cli/commands/proxy/proxy.go @@ -20,6 +20,7 @@ import ( clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc" "github.com/skycoin/skywire/cmd/skywire-cli/internal" "github.com/skycoin/skywire/pkg/app/appserver" + "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/servicedisc" ) @@ -128,12 +129,16 @@ var statusCmd = &cobra.Command{ w := tabwriter.NewWriter(&b, 0, 0, 5, ' ', tabwriter.TabIndent) internal.Catch(cmd.Flags(), err) type appState struct { - Status string `json:"status"` + Name string `json:"name"` + Status string `json:"status"` + AutoStart bool `json:"autostart"` + Args []string `json:"args"` + AppPort routing.Port `json:"app_port"` } - var jsonAppStatus appState + var jsonAppStatus []appState + fmt.Fprintf(w, "---- All Proxy List -----------------------------------------------------\n\n") for _, state := range states { - if state.Name == stateName { - + if state.AppConfig.Binary == binaryName { status := "stopped" if state.Status == appserver.AppStatusRunning { status = "running" @@ -141,13 +146,28 @@ var statusCmd = &cobra.Command{ if state.Status == appserver.AppStatusErrored { status = "errored" } - jsonAppStatus = appState{ - Status: status, + jsonAppStatus = append(jsonAppStatus, appState{ + Name: state.Name, + Status: status, + AutoStart: state.AutoStart, + Args: state.Args, + AppPort: state.Port, + }) + var tmpAddr string + var tmpSrv string + for idx, arg := range state.Args { + if arg == "-srv" { + tmpSrv = state.Args[idx+1] + } + if arg == "-addr" { + tmpAddr = "127.0.0.1" + state.Args[idx+1] + } } - _, err = fmt.Fprintf(w, "%s\n", status) + _, err = fmt.Fprintf(w, "Name: %s\nStatus: %s\nServer: %s\nAddress: %s\nAppPort: %d\nAutoStart: %t\n\n", state.Name, status, tmpSrv, tmpAddr, state.Port, state.AutoStart) internal.Catch(cmd.Flags(), err) } } + fmt.Fprintf(w, "-------------------------------------------------------------------------\n") internal.Catch(cmd.Flags(), w.Flush()) internal.PrintOutput(cmd.Flags(), jsonAppStatus, b.String()) }, diff --git a/cmd/skywire-cli/commands/proxy/root.go b/cmd/skywire-cli/commands/proxy/root.go index 42a69590b6..c4d3722608 100644 --- a/cmd/skywire-cli/commands/proxy/root.go +++ b/cmd/skywire-cli/commands/proxy/root.go @@ -9,6 +9,7 @@ import ( ) var ( + binaryName = "skysocks-client" stateName = "skysocks-client" serviceType = servicedisc.ServiceTypeProxy servicePort = ":44" From 24570d9941efd7723aca6775935a6bfa20314081 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Thu, 23 Nov 2023 23:58:55 +0000 Subject: [PATCH 02/10] add -addr value to generated config for skysocks-client --- cmd/skywire-cli/commands/config/gen.go | 1 + pkg/visor/visorconfig/config.go | 1 + 2 files changed, 2 insertions(+) diff --git a/cmd/skywire-cli/commands/config/gen.go b/cmd/skywire-cli/commands/config/gen.go index 0635419b11..60da07fb2a 100644 --- a/cmd/skywire-cli/commands/config/gen.go +++ b/cmd/skywire-cli/commands/config/gen.go @@ -907,6 +907,7 @@ var genConfigCmd = &cobra.Command{ Binary: visorconfig.SkysocksClientName, AutoStart: false, Port: routing.Port(visorconfig.SkysocksClientPort), + Args: []string{"-addr", visorconfig.SkysocksClientAddr}, }, { Name: visorconfig.VPNServerName, diff --git a/pkg/visor/visorconfig/config.go b/pkg/visor/visorconfig/config.go index 4c5e7619a0..3cd40c8869 100644 --- a/pkg/visor/visorconfig/config.go +++ b/pkg/visor/visorconfig/config.go @@ -257,6 +257,7 @@ func makeDefaultLauncherAppsConfig(dnsServer string) []appserver.AppConfig { Binary: SkysocksClientName, AutoStart: false, Port: routing.Port(skyenv.SkysocksClientPort), + Args: []string{"-addr", SkysocksClientAddr}, }, { Name: VPNServerName, From d0f736198e54127578f240437901b2a1f7249008 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Fri, 24 Nov 2023 00:50:20 +0000 Subject: [PATCH 03/10] improve skywire-cli proxy stop command with add two flag --all and --name --- cmd/skywire-cli/commands/proxy/proxy.go | 14 ++++++++++++-- cmd/skywire-cli/commands/proxy/root.go | 2 ++ pkg/visor/api.go | 14 +++++++++----- pkg/visor/rpc.go | 8 ++++---- pkg/visor/rpc_client.go | 10 +++++----- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/cmd/skywire-cli/commands/proxy/proxy.go b/cmd/skywire-cli/commands/proxy/proxy.go index 37f5d28329..8fe1144b31 100644 --- a/cmd/skywire-cli/commands/proxy/proxy.go +++ b/cmd/skywire-cli/commands/proxy/proxy.go @@ -37,6 +37,8 @@ func init() { version = "" } startCmd.Flags().StringVarP(&pk, "pk", "k", "", "server public key") + stopCmd.Flags().BoolVar(&allClients, "all", false, "stop all skysocks client") + stopCmd.Flags().StringVar(&clientName, "name", "", "specific skysocks client that want stop") listCmd.Flags().StringVarP(&sdURL, "url", "a", "", "service discovery url default:\n"+skyenv.ServiceDiscAddr) listCmd.Flags().BoolVarP(&directQuery, "direct", "b", false, "query service discovery directly") listCmd.Flags().StringVarP(&pk, "pk", "k", "", "check "+serviceType+" service discovery for public key") @@ -108,8 +110,16 @@ var stopCmd = &cobra.Command{ if err != nil { internal.PrintFatalError(cmd.Flags(), fmt.Errorf("unable to create RPC client: %w", err)) } - internal.Catch(cmd.Flags(), rpcClient.StopSkysocksClient()) - internal.PrintOutput(cmd.Flags(), "OK", fmt.Sprintln("OK")) + if allClients && clientName != "" { + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("cannot use both --all and --name flag in together")) + } + if allClients { + internal.Catch(cmd.Flags(), rpcClient.StopSkysocksClients()) + internal.PrintOutput(cmd.Flags(), "All Skysocks Client stopped", fmt.Sprintln("All Skysocks Client stopped")) + return + } + internal.Catch(cmd.Flags(), rpcClient.StopApp(clientName)) + internal.PrintOutput(cmd.Flags(), fmt.Sprintf("Skysocks Client %s stopped", clientName), fmt.Sprintf("Skysocks Client %s stopped\n", clientName)) }, } diff --git a/cmd/skywire-cli/commands/proxy/root.go b/cmd/skywire-cli/commands/proxy/root.go index c4d3722608..94ee37929c 100644 --- a/cmd/skywire-cli/commands/proxy/root.go +++ b/cmd/skywire-cli/commands/proxy/root.go @@ -23,6 +23,8 @@ var ( sdURL string directQuery bool servers []servicedisc.Service + allClients bool + clientName string ) // RootCmd contains commands that interact with the skywire-visor diff --git a/pkg/visor/api.go b/pkg/visor/api.go index d829a94dc8..2713ad3d9a 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -89,7 +89,7 @@ type API interface { //skysocks-client controls StartSkysocksClient(pk string) error - StopSkysocksClient() error + StopSkysocksClients() error ProxyServers(version, country string) ([]servicedisc.Service, error) //transports @@ -598,15 +598,19 @@ func (v *Visor) StartSkysocksClient(serverKey string) error { return errors.New("no skysocks-client app configuration found") } -// StopSkysocksClient implements API. -func (v *Visor) StopSkysocksClient() error { +// StopSkysocksClients implements API. +func (v *Visor) StopSkysocksClients() error { // check process manager and app launcher availability if v.appL == nil { return ErrAppLauncherNotAvailable } if v.procM != nil { - _, err := v.appL.StopApp(visorconfig.SkysocksClientName) //nolint:errcheck - return err + for _, app := range v.conf.Launcher.Apps { + if app.Binary == visorconfig.SkysocksClientName { + v.appL.StopApp(app.Name) //nolint + } + } + return nil } return ErrProcNotAvailable } diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index a292b40733..8ca2b03824 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -290,11 +290,11 @@ func (r *RPC) StartSkysocksClient(pk string, _ *struct{}) (err error) { return r.visor.StartSkysocksClient(pk) } -// StopSkysocksClient stops SkysocksClient App -func (r *RPC) StopSkysocksClient(_ *struct{}, _ *struct{}) (err error) { - defer rpcutil.LogCall(r.log, "StopSkysocksClient", nil)(nil, &err) +// StopSkysocksClients stops all SkysocksClient Apps +func (r *RPC) StopSkysocksClients(_ *struct{}, _ *struct{}) (err error) { + defer rpcutil.LogCall(r.log, "StopSkysocksClients", nil)(nil, &err) - return r.visor.StopSkysocksClient() + return r.visor.StopSkysocksClients() } // RestartApp restarts App with provided name. diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index be561480d6..0886226d9c 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -193,9 +193,9 @@ func (rc *rpcClient) StartSkysocksClient(pk string) error { return rc.Call("StartSkysocksClient", pk, &struct{}{}) } -// StopSkysocksClient calls StopSkysocksClient. -func (rc *rpcClient) StopSkysocksClient() error { - return rc.Call("StopSkysocksClient", &struct{}{}, &struct{}{}) +// StopSkysocksCliens calls StopSkysocksCliens. +func (rc *rpcClient) StopSkysocksClients() error { + return rc.Call("StopSkysocksClients", &struct{}{}, &struct{}{}) } // SetAppDetailedStatus sets app's detailed state. @@ -857,8 +857,8 @@ func (*mockRPCClient) StartSkysocksClient(string) error { return nil } -// StopSkysocksClient implements API. -func (*mockRPCClient) StopSkysocksClient() error { +// StopSkysocksClients implements API. +func (*mockRPCClient) StopSkysocksClients() error { return nil } From 43e057e4ccc7faf2f7927cf905f1c9d9094a34b3 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Fri, 24 Nov 2023 00:51:14 +0000 Subject: [PATCH 04/10] fix no flag choosen issue --- cmd/skywire-cli/commands/proxy/proxy.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/skywire-cli/commands/proxy/proxy.go b/cmd/skywire-cli/commands/proxy/proxy.go index 8fe1144b31..b063995459 100644 --- a/cmd/skywire-cli/commands/proxy/proxy.go +++ b/cmd/skywire-cli/commands/proxy/proxy.go @@ -113,6 +113,9 @@ var stopCmd = &cobra.Command{ if allClients && clientName != "" { internal.PrintFatalError(cmd.Flags(), fmt.Errorf("cannot use both --all and --name flag in together")) } + if !allClients && clientName == "" { + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("you should use one of flags, --all or --name")) + } if allClients { internal.Catch(cmd.Flags(), rpcClient.StopSkysocksClients()) internal.PrintOutput(cmd.Flags(), "All Skysocks Client stopped", fmt.Sprintln("All Skysocks Client stopped")) From 4caf3fac45b4a7eaf6b9db6bdfa1c8b3faf0e17f Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Fri, 24 Nov 2023 01:02:33 +0000 Subject: [PATCH 05/10] add condition on choosen at least one flag for stop command --- cmd/skywire-cli/commands/proxy/proxy.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/skywire-cli/commands/proxy/proxy.go b/cmd/skywire-cli/commands/proxy/proxy.go index b063995459..295bfb619a 100644 --- a/cmd/skywire-cli/commands/proxy/proxy.go +++ b/cmd/skywire-cli/commands/proxy/proxy.go @@ -118,11 +118,11 @@ var stopCmd = &cobra.Command{ } if allClients { internal.Catch(cmd.Flags(), rpcClient.StopSkysocksClients()) - internal.PrintOutput(cmd.Flags(), "All Skysocks Client stopped", fmt.Sprintln("All Skysocks Client stopped")) + internal.PrintOutput(cmd.Flags(), "all skysocks client stopped", fmt.Sprintln("all skysocks clients stopped")) return } internal.Catch(cmd.Flags(), rpcClient.StopApp(clientName)) - internal.PrintOutput(cmd.Flags(), fmt.Sprintf("Skysocks Client %s stopped", clientName), fmt.Sprintf("Skysocks Client %s stopped\n", clientName)) + internal.PrintOutput(cmd.Flags(), fmt.Sprintf("skysocks client %s stopped", clientName), fmt.Sprintf("skysocks client %s stopped\n", clientName)) }, } From ecb8c9b082569922668794adcf84ec069b094dbc Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Fri, 24 Nov 2023 02:53:18 +0000 Subject: [PATCH 06/10] implement proxy start command | implement rpc for custom setting arguments --- cmd/skywire-cli/commands/proxy/proxy.go | 74 ++++++++++++++++++++----- cmd/skywire-cli/commands/proxy/root.go | 1 + pkg/visor/api.go | 10 ++++ pkg/visor/rpc.go | 26 +++++++++ pkg/visor/rpc_client.go | 13 +++++ pkg/visor/visorconfig/v1.go | 36 ++++++++++++ 6 files changed, 147 insertions(+), 13 deletions(-) diff --git a/cmd/skywire-cli/commands/proxy/proxy.go b/cmd/skywire-cli/commands/proxy/proxy.go index 295bfb619a..bb01bce0ad 100644 --- a/cmd/skywire-cli/commands/proxy/proxy.go +++ b/cmd/skywire-cli/commands/proxy/proxy.go @@ -37,6 +37,8 @@ func init() { version = "" } startCmd.Flags().StringVarP(&pk, "pk", "k", "", "server public key") + startCmd.Flags().StringVarP(&addr, "addr", "a", "", "address of proxy for use") + startCmd.Flags().StringVarP(&clientName, "name", "n", "", "name of skysocks client") stopCmd.Flags().BoolVar(&allClients, "all", false, "stop all skysocks client") stopCmd.Flags().StringVar(&clientName, "name", "", "specific skysocks client that want stop") listCmd.Flags().StringVarP(&sdURL, "url", "a", "", "service discovery url default:\n"+skyenv.ServiceDiscAddr) @@ -53,25 +55,71 @@ var startCmd = &cobra.Command{ Use: "start", Short: "start the " + serviceType + " client", Run: func(cmd *cobra.Command, args []string) { - //check that a valid public key is provided - err := pubkey.Set(pk) + + rpcClient, err := clirpc.Client(cmd.Flags()) if err != nil { - if len(args) > 0 { - err := pubkey.Set(args[0]) + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("unable to create RPC client: %w", err)) + } + + if clientName != "" && pk != "" && addr != "" { + // add new app with -srv and -addr args, and if app was there just change -srv and -addr args and run it + err := pubkey.Set(pk) + if err != nil { + if len(args) > 0 { + err := pubkey.Set(args[0]) + if err != nil { + internal.PrintFatalError(cmd.Flags(), err) + } + } else { + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Invalid or missing public key")) + } + } + + arguments := map[string]string{} + arguments["srv"] = pubkey.String() + arguments["addr"] = addr + + _, err = rpcClient.App(clientName) + if err == nil { + err = rpcClient.DoCustomSetting(clientName, arguments) if err != nil { - internal.PrintFatalError(cmd.Flags(), err) + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Error occurs during set args to custom skysocks client")) } } else { - internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Invalid or missing public key")) + err = rpcClient.AddApp(clientName, "skysocks-client") + if err != nil { + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Error during add new app")) + } + err = rpcClient.DoCustomSetting(clientName, arguments) + if err != nil { + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Error occurs during set args to custom skysocks client")) + } } + internal.Catch(cmd.Flags(), rpcClient.StartApp(clientName)) + internal.PrintOutput(cmd.Flags(), nil, "Starting.") + } else if clientName != "" && pk == "" && addr == "" { + internal.Catch(cmd.Flags(), rpcClient.StartApp(clientName)) + internal.PrintOutput(cmd.Flags(), nil, "Starting.") + } else if pk != "" && clientName == "" && addr == "" { + err := pubkey.Set(pk) + if err != nil { + if len(args) > 0 { + err := pubkey.Set(args[0]) + if err != nil { + internal.PrintFatalError(cmd.Flags(), err) + } + } else { + internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Invalid or missing public key")) + } + } + internal.Catch(cmd.Flags(), rpcClient.StartSkysocksClient(pubkey.String())) + internal.PrintOutput(cmd.Flags(), nil, "Starting.") + // change defaul skysocks-proxy app -srv arg and run it + } else { + // error + return } - rpcClient, err := clirpc.Client(cmd.Flags()) - if err != nil { - internal.PrintFatalError(cmd.Flags(), fmt.Errorf("unable to create RPC client: %w", err)) - } - //TODO: implement operational timeout - internal.Catch(cmd.Flags(), rpcClient.StartSkysocksClient(pubkey.String())) - internal.PrintOutput(cmd.Flags(), nil, "Starting.") + startProcess := true for startProcess { time.Sleep(time.Second * 1) diff --git a/cmd/skywire-cli/commands/proxy/root.go b/cmd/skywire-cli/commands/proxy/root.go index 94ee37929c..d6d5a95a29 100644 --- a/cmd/skywire-cli/commands/proxy/root.go +++ b/cmd/skywire-cli/commands/proxy/root.go @@ -25,6 +25,7 @@ var ( servers []servicedisc.Service allClients bool clientName string + addr string ) // RootCmd contains commands that interact with the skywire-visor diff --git a/pkg/visor/api.go b/pkg/visor/api.go index 2713ad3d9a..c0b907480c 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -62,6 +62,7 @@ type API interface { App(appName string) (*appserver.AppState, error) Apps() ([]*appserver.AppState, error) StartApp(appName string) error + AddApp(appName, binaryName string) error RegisterApp(procConf appcommon.ProcConfig) (appcommon.ProcKey, error) DeregisterApp(procKey appcommon.ProcKey) error StopApp(appName string) error @@ -459,6 +460,15 @@ func (v *Visor) StartApp(appName string) error { return ErrProcNotAvailable } +// AddApp implement API. +func (v *Visor) AddApp(appName, binaryName string) error { + // check process manager and app launcher availability + if v.appL == nil { + return ErrAppLauncherNotAvailable + } + return v.conf.AddAppConfig(v.appL, appName, binaryName) +} + // RegisterApp implements API. func (v *Visor) RegisterApp(procConf appcommon.ProcConfig) (appcommon.ProcKey, error) { // check process manager and app launcher availability diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 8ca2b03824..8722d0fc16 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -248,6 +248,32 @@ func (r *RPC) StartApp(name *string, _ *struct{}) (err error) { return r.visor.StartApp(*name) } +// SetAppAddIn is input for SetAppAdd. +type SetAppAddIn struct { + AppName string + BinaryName string +} + +// AddApp add app to config +func (r *RPC) AddApp(in *SetAppAddIn, _ *struct{}) (err error) { + defer rpcutil.LogCall(r.log, "AddApp", in)(nil, &err) + + return r.visor.AddApp(in.AppName, in.BinaryName) +} + +// DoCustomSettingIn is input for DoCustomSetting +type DoCustomSettingIn struct { + AppName string + CustomSetting map[string]string +} + +// DoCustomSetting set custom setting to apps arguments +func (r *RPC) DoCustomSetting(in *DoCustomSettingIn, _ *struct{}) (err error) { + defer rpcutil.LogCall(r.log, "DoCustomSetting", in)(nil, &err) + + return r.visor.DoCustomSetting(in.AppName, in.CustomSetting) +} + // RegisterApp registers a App with provided proc config. func (r *RPC) RegisterApp(procConf *appcommon.ProcConfig, reply *appcommon.ProcKey) (err error) { defer rpcutil.LogCall(r.log, "RegisterApp", procConf)(reply, &err) diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index 0886226d9c..72c3b7a0ac 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -161,6 +161,14 @@ func (rc *rpcClient) StartApp(appName string) error { return rc.Call("StartApp", &appName, &struct{}{}) } +// AddApp calls AddApp. +func (rc *rpcClient) AddApp(appName, binaryName string) error { + return rc.Call("AddApp", &SetAppAddIn{ + AppName: appName, + BinaryName: binaryName, + }, &struct{}{}) +} + // RegisterApp calls RegisterApp. func (rc *rpcClient) RegisterApp(procConf appcommon.ProcConfig) (appcommon.ProcKey, error) { var procKey appcommon.ProcKey @@ -827,6 +835,11 @@ func (*mockRPCClient) StartApp(string) error { return nil } +// AddApp implement API. +func (*mockRPCClient) AddApp(string, string) error { + return nil +} + // RegisterApp implements API. func (*mockRPCClient) RegisterApp(appcommon.ProcConfig) (appcommon.ProcKey, error) { return appcommon.ProcKey{}, nil diff --git a/pkg/visor/visorconfig/v1.go b/pkg/visor/visorconfig/v1.go index fbc7da63af..adb7d6d9bb 100644 --- a/pkg/visor/visorconfig/v1.go +++ b/pkg/visor/visorconfig/v1.go @@ -3,6 +3,7 @@ package visorconfig import ( "fmt" + "math/rand" "strings" "sync" @@ -10,6 +11,7 @@ import ( "github.com/skycoin/skywire/pkg/app/appserver" "github.com/skycoin/skywire/pkg/app/launcher" "github.com/skycoin/skywire/pkg/dmsgc" + "github.com/skycoin/skywire/pkg/routing" "github.com/skycoin/skywire/pkg/transport" "github.com/skycoin/skywire/pkg/transport/network" ) @@ -237,6 +239,40 @@ func (v1 *V1) UpdatePublicAutoconnect(pAc bool) error { return v1.flush(v1) } +// AddAppConfig add new config to apps if name was not same +func (v1 *V1) AddAppConfig(launch *launcher.AppLauncher, appName, binaryName string) error { + v1.mu.Lock() + defer v1.mu.Unlock() + + conf := v1.Launcher + busyPorts := map[routing.Port]bool{} + for _, app := range conf.Apps { + busyPorts[app.Port] = true + if app.Name == appName { + return fmt.Errorf("the app exist") + } + } + var randomNumber int + for { + min := 10 + max := 99 + randomNumber := rand.Intn(max-min+1) + min //nolint + if _, ok := busyPorts[routing.Port(randomNumber)]; !ok { + break + } + } + + conf.Apps = append(conf.Apps, appserver.AppConfig{Name: appName, Binary: binaryName, Port: routing.Port(randomNumber)}) + + launch.ResetConfig(launcher.AppLauncherConfig{ + VisorPK: v1.PK, + Apps: conf.Apps, + ServerAddr: conf.ServerAddr, + DisplayNodeIP: conf.DisplayNodeIP, + }) + return v1.flush(v1) +} + // updateStringArg updates the cli non-boolean flag of the specified app config and also within the // It removes argName from app args if value is an empty string. // The updated config gets flushed to file if there are any changes. From 77de7e2ed9224e0dd5c747e55e07b039d6d50737 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Wed, 29 Nov 2023 01:03:32 +0000 Subject: [PATCH 07/10] fix rpc query on customsetting --- pkg/visor/rpc.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 8722d0fc16..168b24c58d 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -261,17 +261,10 @@ func (r *RPC) AddApp(in *SetAppAddIn, _ *struct{}) (err error) { return r.visor.AddApp(in.AppName, in.BinaryName) } -// DoCustomSettingIn is input for DoCustomSetting -type DoCustomSettingIn struct { - AppName string - CustomSetting map[string]string -} - // DoCustomSetting set custom setting to apps arguments -func (r *RPC) DoCustomSetting(in *DoCustomSettingIn, _ *struct{}) (err error) { +func (r *RPC) DoCustomSetting(in *SetAppMapIn, _ *struct{}) (err error) { defer rpcutil.LogCall(r.log, "DoCustomSetting", in)(nil, &err) - - return r.visor.DoCustomSetting(in.AppName, in.CustomSetting) + return r.visor.DoCustomSetting(in.AppName, in.Val) } // RegisterApp registers a App with provided proc config. From 5b710b8b59cba2e3a9dcfbf8b8ab3b043db100df Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Wed, 29 Nov 2023 01:07:52 +0000 Subject: [PATCH 08/10] fix random app port on adding new app --- pkg/visor/visorconfig/v1.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/visor/visorconfig/v1.go b/pkg/visor/visorconfig/v1.go index adb7d6d9bb..48a26f2611 100644 --- a/pkg/visor/visorconfig/v1.go +++ b/pkg/visor/visorconfig/v1.go @@ -256,7 +256,7 @@ func (v1 *V1) AddAppConfig(launch *launcher.AppLauncher, appName, binaryName str for { min := 10 max := 99 - randomNumber := rand.Intn(max-min+1) + min //nolint + randomNumber = rand.Intn(max-min+1) + min //nolint if _, ok := busyPorts[routing.Port(randomNumber)]; !ok { break } From 1089dddbb5c0aec1b51853bf45a186b19145a611 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Wed, 29 Nov 2023 01:44:58 +0000 Subject: [PATCH 09/10] add ctrl+c signal to command --- cmd/skywire-cli/commands/proxy/proxy.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmd/skywire-cli/commands/proxy/proxy.go b/cmd/skywire-cli/commands/proxy/proxy.go index bb01bce0ad..2c4ef5e821 100644 --- a/cmd/skywire-cli/commands/proxy/proxy.go +++ b/cmd/skywire-cli/commands/proxy/proxy.go @@ -3,6 +3,7 @@ package skysocksc import ( "bytes" + "context" "encoding/json" "fmt" "math/rand" @@ -12,10 +13,12 @@ import ( "text/tabwriter" "time" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/skycoin/skywire-utilities/pkg/buildinfo" + "github.com/skycoin/skywire-utilities/pkg/cmdutil" "github.com/skycoin/skywire-utilities/pkg/skyenv" clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc" "github.com/skycoin/skywire/cmd/skywire-cli/internal" @@ -114,12 +117,22 @@ var startCmd = &cobra.Command{ } internal.Catch(cmd.Flags(), rpcClient.StartSkysocksClient(pubkey.String())) internal.PrintOutput(cmd.Flags(), nil, "Starting.") + clientName = "skysocks-client" // change defaul skysocks-proxy app -srv arg and run it } else { // error return } + ctx, cancel := cmdutil.SignalContext(context.Background(), &logrus.Logger{}) + defer cancel() + go func() { + <-ctx.Done() + cancel() + rpcClient.StopApp(clientName) //nolint + os.Exit(1) + }() + startProcess := true for startProcess { time.Sleep(time.Second * 1) From cc98d8a05d07ed27b1e1850fd4f1d3e110582c95 Mon Sep 17 00:00:00 2001 From: MohammadReza Palide Date: Wed, 29 Nov 2023 01:47:17 +0000 Subject: [PATCH 10/10] add ctrl+c signal to command --- cmd/skywire-cli/commands/vpn/vvpn.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmd/skywire-cli/commands/vpn/vvpn.go b/cmd/skywire-cli/commands/vpn/vvpn.go index 3d0150ee8d..4ab463523e 100644 --- a/cmd/skywire-cli/commands/vpn/vvpn.go +++ b/cmd/skywire-cli/commands/vpn/vvpn.go @@ -3,6 +3,7 @@ package clivpn import ( "bytes" + "context" "encoding/json" "fmt" "math/rand" @@ -12,10 +13,12 @@ import ( "text/tabwriter" "time" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/skycoin/skywire-utilities/pkg/buildinfo" + "github.com/skycoin/skywire-utilities/pkg/cmdutil" "github.com/skycoin/skywire-utilities/pkg/skyenv" clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc" "github.com/skycoin/skywire/cmd/skywire-cli/internal" @@ -80,6 +83,14 @@ var startCmd = &cobra.Command{ } internal.Catch(cmd.Flags(), rpcClient.StartVPNClient(pubkey)) internal.PrintOutput(cmd.Flags(), nil, "Starting.") + ctx, cancel := cmdutil.SignalContext(context.Background(), &logrus.Logger{}) + defer cancel() + go func() { + <-ctx.Done() + cancel() + rpcClient.StopVPNClient("vpn-client") //nolint + os.Exit(1) + }() startProcess := true for startProcess { time.Sleep(time.Second * 1)