Skip to content

Commit

Permalink
check app launcher availability (#1508)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpalide authored Mar 5, 2023
1 parent 87409b6 commit 7b89662
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
75 changes: 67 additions & 8 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,19 @@ func (v *Visor) DeleteRewardAddress() error {

// Apps implements API.
func (v *Visor) Apps() ([]*appserver.AppState, error) {
// check app launcher availability
if v.appL == nil {
return nil, ErrAppLauncherNotAvailable
}
return v.appL.AppStates(), nil
}

// App implements API.
func (v *Visor) App(appName string) (*appserver.AppState, error) {
// check app launcher availability
if v.appL == nil {
return nil, ErrAppLauncherNotAvailable
}
appState, ok := v.appL.AppState(appName)
if !ok {
return &appserver.AppState{}, ErrAppProcNotRunning
Expand All @@ -418,6 +426,10 @@ func (v *Visor) SkybianBuildVersion() string {

// StartApp implements API.
func (v *Visor) StartApp(appName string) error {
// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
var envs []string
var err error
if appName == visorconfig.VPNClientName {
Expand Down Expand Up @@ -447,7 +459,10 @@ func (v *Visor) StartApp(appName string) error {

// RegisterApp implements API.
func (v *Visor) RegisterApp(procConf appcommon.ProcConfig) (appcommon.ProcKey, error) {
// check process manager availability
// check process manager and app launcher availability
if v.appL == nil {
return appcommon.ProcKey{}, ErrAppLauncherNotAvailable
}
if v.procM != nil {
return v.appL.RegisterApp(procConf)
}
Expand All @@ -456,7 +471,10 @@ func (v *Visor) RegisterApp(procConf appcommon.ProcConfig) (appcommon.ProcKey, e

// DeregisterApp implements API.
func (v *Visor) DeregisterApp(procKey appcommon.ProcKey) error {
// check process manager availability
// check process manager and app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
if v.procM != nil {
return v.appL.DeregisterApp(procKey)
}
Expand All @@ -465,7 +483,10 @@ func (v *Visor) DeregisterApp(procKey appcommon.ProcKey) error {

// StopApp implements API.
func (v *Visor) StopApp(appName string) error {
// check process manager availability
// check process manager and app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
if v.procM != nil {
_, err := v.appL.StopApp(appName) //nolint:errcheck
return err
Expand All @@ -480,7 +501,10 @@ func (v *Visor) StartVPNClient(pk cipher.PubKey) error {
if v.tpM == nil {
return ErrTrpMangerNotAvailable
}

// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
if len(v.conf.Launcher.Apps) == 0 {
return errors.New("no vpn app configuration found")
}
Expand Down Expand Up @@ -513,7 +537,10 @@ func (v *Visor) StartVPNClient(pk cipher.PubKey) error {

// StopVPNClient implements API.
func (v *Visor) StopVPNClient(appName string) error {
// check process manager availability
// check process manager and app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
if v.procM != nil {
_, err := v.appL.StopApp(appName) //nolint:errcheck
return err
Expand All @@ -527,7 +554,10 @@ func (v *Visor) StartSkysocksClient(serverKey string) error {
if v.tpM == nil {
return ErrTrpMangerNotAvailable
}

// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
if len(v.conf.Launcher.Apps) == 0 {
return errors.New("no skysocks-client app configuration found")
}
Expand Down Expand Up @@ -568,7 +598,10 @@ func (v *Visor) StartSkysocksClient(serverKey string) error {

// StopSkysocksClient implements API.
func (v *Visor) StopSkysocksClient() error {
// check process manager availability
// 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
Expand Down Expand Up @@ -603,6 +636,11 @@ func (v *Visor) SetAppError(appName, appErr string) error {

// RestartApp implements API.
func (v *Visor) RestartApp(appName string) error {
// check app launcher availability
if v.appL == nil {
v.log.Warn("app launcher not ready yet")
return ErrAppLauncherNotAvailable
}
if _, ok := v.procM.ProcByName(appName); ok { //nolint:errcheck
v.log.Infof("Updated %v password, restarting it", appName)
return v.appL.RestartApp(appName, appName)
Expand All @@ -613,6 +651,11 @@ func (v *Visor) RestartApp(appName string) error {

// SetAutoStart implements API.
func (v *Visor) SetAutoStart(appName string, autoStart bool) error {
// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}

if _, ok := v.appL.AppState(appName); !ok {
return ErrAppProcNotRunning
}
Expand All @@ -623,6 +666,10 @@ func (v *Visor) SetAutoStart(appName string, autoStart bool) error {

// SetAppPassword implements API.
func (v *Visor) SetAppPassword(appName, password string) error {
// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}
allowedToChangePassword := func(appName string) bool {
allowedApps := map[string]struct{}{
visorconfig.SkysocksName: {},
Expand Down Expand Up @@ -654,6 +701,11 @@ func (v *Visor) SetAppPassword(appName, password string) error {

// SetAppNetworkInterface implements API.
func (v *Visor) SetAppNetworkInterface(appName, netifc string) error {
// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}

if visorconfig.VPNServerName != appName {
return fmt.Errorf("app %s is not allowed to set network interface", appName)
}
Expand All @@ -674,6 +726,11 @@ func (v *Visor) SetAppNetworkInterface(appName, netifc string) error {

// SetAppKillswitch implements API.
func (v *Visor) SetAppKillswitch(appName string, killswitch bool) error {
// check app launcher availability
if v.appL == nil {
return ErrAppLauncherNotAvailable
}

if appName != visorconfig.VPNClientName {
return fmt.Errorf("app %s is not allowed to set killswitch", appName)
}
Expand Down Expand Up @@ -775,7 +832,9 @@ func (v *Visor) SetAppDNS(appName string, dnsAddr string) error {
func (v *Visor) DoCustomSetting(appName string, customSetting map[string]string) error {

v.log.Infof("Changing %s Settings to %q", appName, customSetting)

if v.appL == nil {
return ErrAppLauncherNotAvailable
}
if err := v.conf.DeleteAppArg(v.appL, appName); err != nil {
v.log.Warn("An error occurs deleting old arguments.")
return err
Expand Down
2 changes: 2 additions & 0 deletions pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
ErrProcNotAvailable = errors.New("no process manager available")
// ErrTrpMangerNotAvailable represents error for unavailable transport manager
ErrTrpMangerNotAvailable = errors.New("no transport manager available")
// ErrAppLauncherNotAvailable represents error for unavailable app launcher
ErrAppLauncherNotAvailable = errors.New("no app launcher available")
)

const (
Expand Down

0 comments on commit 7b89662

Please sign in to comment.