summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2023-03-20 12:58:42 +0100
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2023-09-25 09:43:37 +0200
commit495fe67cd9f7e9ee9af1022cc8d4859b365f1cf5 (patch)
treed2a0036d910ff527ed3857cb7d1bd3c13026df7f /client
parentde4ec8a1cd9aa8ed7afe3a634946a6586f897f5e (diff)
Client FSM: Remove states that are client specific/not in the lib
Diffstat (limited to 'client')
-rw-r--r--client/fsm.go209
1 files changed, 32 insertions, 177 deletions
diff --git a/client/fsm.go b/client/fsm.go
index b2e846a..d1d2d9e 100644
--- a/client/fsm.go
+++ b/client/fsm.go
@@ -24,13 +24,13 @@ const (
// StateAskLocation means the user selected a Secure Internet server but needs to choose a location.
StateAskLocation
- // StateSearchServer means the user is currently selecting a server in the UI.
- StateSearchServer
+ // StateChosenLocation means the user has selected a Secure Internet location
+ StateChosenLocation
// StateLoadingServer means we are loading the server details.
StateLoadingServer
- // StateChosenServer means the user has chosen a server to connect to.
+ // StateChosenServer means the user has chosen a server to connect to and the server is initialized.
StateChosenServer
// StateOAuthStarted means the OAuth process has started.
@@ -45,17 +45,11 @@ const (
// StateAskProfile means the go code is asking for a profile selection from the UI.
StateAskProfile
- // StateDisconnected means the user has gotten a config for a server but is not connected yet.
- StateDisconnected
+ // StateChosenProfile means a profile has been chosen
+ StateChosenProfile
- // StateDisconnecting means the OS is disconnecting and the Go code is doing the /disconnect.
- StateDisconnecting
-
- // StateConnecting means the OS is establishing a connection to the server.
- StateConnecting
-
- // StateConnected means the user has been connected to the server.
- StateConnected
+ // StateGotConfig means a VPN configuration has been obtained
+ StateGotConfig
)
func GetStateName(s FSMStateID) string {
@@ -66,28 +60,24 @@ func GetStateName(s FSMStateID) string {
return "No_Server"
case StateAskLocation:
return "Ask_Location"
- case StateSearchServer:
- return "Search_Server"
case StateLoadingServer:
return "Loading_Server"
case StateChosenServer:
return "Chosen_Server"
+ case StateChosenLocation:
+ return "Chosen_Location"
case StateOAuthStarted:
return "OAuth_Started"
- case StateDisconnected:
- return "Disconnected"
case StateRequestConfig:
return "Request_Config"
case StateAskProfile:
return "Ask_Profile"
+ case StateChosenProfile:
+ return "Chosen_Profile"
case StateAuthorized:
return "Authorized"
- case StateDisconnecting:
- return "Disconnecting"
- case StateConnecting:
- return "Connecting"
- case StateConnected:
- return "Connected"
+ case StateGotConfig:
+ return "Got_Config"
default:
panic("unknown conversion of state to string")
}
@@ -106,26 +96,18 @@ func newFSM(
Transitions: []FSMTransition{
{To: StateNoServer, Description: "Reload list"},
{To: StateLoadingServer, Description: "User clicks a server in the UI"},
- {To: StateChosenServer, Description: "The server has been chosen"},
- {
- To: StateSearchServer,
- Description: "The user is trying to choose a new server in the UI",
- },
- {To: StateConnected, Description: "The user is already connected"},
- {To: StateAskLocation, Description: "Change the location in the main screen"},
},
},
- StateSearchServer: FSMState{
+ StateAskLocation: FSMState{
Transitions: []FSMTransition{
- {To: StateLoadingServer, Description: "User clicks a server in the UI"},
- {To: StateNoServer, Description: "Cancel or Error"},
+ {To: StateChosenLocation, Description: "Location chosen"},
+ {To: StateNoServer, Description: "Go back or Error"},
},
},
- StateAskLocation: FSMState{
+ StateChosenLocation: FSMState{
Transitions: []FSMTransition{
- {To: StateChosenServer, Description: "Location chosen"},
+ {To: StateChosenServer, Description: "Server has been chosen"},
{To: StateNoServer, Description: "Go back or Error"},
- {To: StateSearchServer, Description: "Cancel or Error"},
},
},
StateLoadingServer: FSMState{
@@ -148,7 +130,6 @@ func newFSM(
Transitions: []FSMTransition{
{To: StateAuthorized, Description: "User authorizes with browser"},
{To: StateNoServer, Description: "Go back or Error"},
- {To: StateSearchServer, Description: "Cancel or Error"},
},
},
StateAuthorized: FSMState{
@@ -161,41 +142,27 @@ func newFSM(
StateRequestConfig: FSMState{
Transitions: []FSMTransition{
{To: StateAskProfile, Description: "Multiple profiles found and no profile chosen"},
- {To: StateDisconnected, Description: "Only one profile or profile already chosen"},
+ {To: StateChosenProfile, Description: "Only one profile or profile already chosen"},
{To: StateNoServer, Description: "Cancel or Error"},
{To: StateOAuthStarted, Description: "Re-authorize"},
},
},
StateAskProfile: FSMState{
Transitions: []FSMTransition{
- {To: StateDisconnected, Description: "User chooses profile"},
{To: StateNoServer, Description: "Cancel or Error"},
- {To: StateSearchServer, Description: "Cancel or Error"},
- },
- },
- StateDisconnected: FSMState{
- Transitions: []FSMTransition{
- {To: StateConnecting, Description: "OS reports it is trying to connect"},
- {To: StateRequestConfig, Description: "User reconnects"},
- {To: StateNoServer, Description: "User wants to choose a new server"},
- {To: StateOAuthStarted, Description: "Re-authorize with OAuth"},
- },
- },
- StateDisconnecting: FSMState{
- Transitions: []FSMTransition{
- {To: StateDisconnected, Description: "Cancel or Error"},
- {To: StateDisconnected, Description: "Done disconnecting"},
+ {To: StateChosenProfile, Description: "Profile has been chosen"},
},
},
- StateConnecting: FSMState{
+ StateChosenProfile: FSMState{
Transitions: []FSMTransition{
- {To: StateDisconnected, Description: "Cancel or Error"},
- {To: StateConnected, Description: "Done connecting"},
+ {To: StateNoServer, Description: "Cancel or Error"},
+ {To: StateGotConfig, Description: "Config has been obtained"},
},
},
- StateConnected: FSMState{
+ StateGotConfig: FSMState{
Transitions: []FSMTransition{
- {To: StateDisconnecting, Description: "App wants to disconnect"},
+ {To: StateNoServer, Description: "Choose a new server"},
+ {To: StateLoadingServer, Description: "Get a new configuration"},
},
},
}
@@ -204,115 +171,16 @@ func newFSM(
return returnedFSM
}
-// SetSearchServer sets the FSM to the SEARCH_SERVER state.
-// This indicates that the user wants to search for a new server.
-// Returns an error if this state transition is not possible.
-func (c *Client) SetSearchServer() error {
- if err := c.FSM.CheckTransition(StateSearchServer); err != nil {
- c.logError(err)
- return err
- }
-
- // TODO(jwijenbergh): Should we handle `false` returned value here?
- c.FSM.GoTransition(StateSearchServer)
- return nil
-}
-
-// SetConnected sets the FSM to the CONNECTED state.
-// This indicates that the VPN is connected to the server.
-// Returns an error if this state transition is not possible.
-func (c *Client) SetConnected() error {
- if c.InFSMState(StateConnected) {
- // already connected, show no error
- return nil
- }
-
- if err := c.FSM.CheckTransition(StateConnected); err != nil {
- c.logError(err)
- return err
- }
-
- srv, err := c.Servers.GetCurrentServer()
- if err != nil {
- c.logError(err)
- return err
- }
-
- c.FSM.GoTransitionWithData(StateConnected, srv)
- return nil
-}
-
-// SetConnecting sets the FSM to the CONNECTING state.
-// This indicates that the VPN is currently connecting to the server.
-// Returns an error if this state transition is not possible.
-func (c *Client) SetConnecting() error {
- if c.InFSMState(StateConnecting) {
- // already loading connection, show no error
- log.Logger.Warningf("Already connecting")
- return nil
- }
- if err := c.FSM.CheckTransition(StateConnecting); err != nil {
- c.logError(err)
- return err
- }
-
- srv, err := c.Servers.GetCurrentServer()
- if err != nil {
- c.logError(err)
- return err
- }
-
- c.FSM.GoTransitionWithData(StateConnecting, srv)
- return nil
-}
-
-// SetDisconnecting sets the FSM to the DISCONNECTING state.
-// This indicates that the VPN is currently disconnecting from the server.
-// Returns an error if this state transition is not possible.
-func (c *Client) SetDisconnecting() error {
- if c.InFSMState(StateDisconnecting) {
- // already disconnecting, show no error
- log.Logger.Warningf("Already disconnecting")
- return nil
- }
- if err := c.FSM.CheckTransition(StateDisconnecting); err != nil {
- c.logError(err)
- return err
- }
-
- srv, err := c.Servers.GetCurrentServer()
- if err != nil {
- c.logError(err)
- return err
- }
-
- c.FSM.GoTransitionWithData(StateDisconnecting, srv)
- return nil
-}
-
-// SetDisconnected sets the FSM to the DISCONNECTED state.
-// This indicates that the VPN is currently disconnected from the server.
-// This also sends the /disconnect API call to the server.
-// Returns an error if this state transition is not possible.
-func (c *Client) SetDisconnected() error {
- if c.InFSMState(StateDisconnected) {
- // already disconnected, show no error
- log.Logger.Warningf("Already disconnected")
- return nil
- }
- if err := c.FSM.CheckTransition(StateDisconnected); err != nil {
- c.logError(err)
- return err
- }
-
- srv, err := c.Servers.GetCurrentServer()
- if err != nil {
+// GoBack transitions the FSM back to the previous UI state, for now this is always the NO_SERVER state.
+func (c *Client) GoBack() error {
+ if c.InFSMState(StateDeregistered) {
+ err := errors.Errorf("fsm attempt going back from 'StateDeregistered'")
c.logError(err)
return err
}
- c.FSM.GoTransitionWithData(StateDisconnected, srv)
-
+ // FIXME: Arbitrary back transitions don't work because we need the appropriate data
+ c.FSM.GoTransitionWithData(StateNoServer, c.Servers)
return nil
}
@@ -325,19 +193,6 @@ func (c *Client) goBackInternal() {
}
}
-// GoBack transitions the FSM back to the previous UI state, for now this is always the NO_SERVER state.
-func (c *Client) GoBack() error {
- if c.InFSMState(StateDeregistered) {
- err := errors.Errorf("fsm attempt going back from 'StateDeregistered'")
- c.logError(err)
- return err
- }
-
- // FIXME: Arbitrary back transitions don't work because we need the appropriate data
- c.FSM.GoTransitionWithData(StateNoServer, c.Servers)
- return nil
-}
-
// CancelOAuth cancels OAuth if one is in progress.
// If OAuth is not in progress, it returns an error.
// An error is also returned if OAuth is in progress, but it fails to cancel it.