diff options
| -rw-r--r-- | cmd/cli/main.go | 2 | ||||
| -rw-r--r-- | fsm.go | 76 | ||||
| -rw-r--r-- | internal/oauth/oauth.go | 12 |
3 files changed, 47 insertions, 43 deletions
diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 4037a72..fe6b40e 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -51,7 +51,7 @@ func sendProfile(state *eduvpn.VPNState, data interface{}) { serverProfiles, ok := data.(*server.ServerProfileInfo) if !ok { - fmt.Errorf("Invalid data type") + fmt.Println("Invalid data type") return } @@ -102,100 +102,100 @@ func newFSM( ) fsm.FSM { states := FSMStates{ STATE_DEREGISTERED: FSMState{ - Transitions: []FSMTransition{{STATE_NO_SERVER, "Client registers"}}, + Transitions: []FSMTransition{{To: STATE_NO_SERVER, Description: "Client registers"}}, }, STATE_NO_SERVER: FSMState{ Transitions: []FSMTransition{ - {STATE_NO_SERVER, "Reload list"}, - {STATE_CHOSEN_SERVER, "User chooses a server"}, - {STATE_SEARCH_SERVER, "The user is trying to choose a Server in the UI"}, - {STATE_CONNECTED, "The user is already connected"}, - {STATE_ASK_LOCATION, "Change the location in the main screen"}, + {To: STATE_NO_SERVER, Description: "Reload list"}, + {To: STATE_CHOSEN_SERVER, Description: "User chooses a server"}, + {To: STATE_SEARCH_SERVER, Description: "The user is trying to choose a Server in the UI"}, + {To: STATE_CONNECTED, Description: "The user is already connected"}, + {To: STATE_ASK_LOCATION, Description: "Change the location in the main screen"}, }, }, STATE_SEARCH_SERVER: FSMState{ Transitions: []FSMTransition{ - {STATE_LOADING_SERVER, "User clicks a server in the UI"}, - {STATE_NO_SERVER, "Cancel or Error"}, + {To: STATE_LOADING_SERVER, Description: "User clicks a server in the UI"}, + {To: STATE_NO_SERVER, Description: "Cancel or Error"}, }, BackState: STATE_NO_SERVER, }, STATE_ASK_LOCATION: FSMState{ Transitions: []FSMTransition{ - {STATE_CHOSEN_SERVER, "Location chosen"}, - {STATE_NO_SERVER, "Go back or Error"}, - {STATE_SEARCH_SERVER, "Cancel or Error"}, + {To: STATE_CHOSEN_SERVER, Description: "Location chosen"}, + {To: STATE_NO_SERVER, Description: "Go back or Error"}, + {To: STATE_SEARCH_SERVER, Description: "Cancel or Error"}, }, }, STATE_LOADING_SERVER: FSMState{ Transitions: []FSMTransition{ - {STATE_CHOSEN_SERVER, "Server info loaded"}, + {To: STATE_CHOSEN_SERVER, Description: "Server info loaded"}, { - STATE_ASK_LOCATION, - "User chooses a Secure Internet server but no location is configured", + To: STATE_ASK_LOCATION, + Description: "User chooses a Secure Internet server but no location is configured", }, - {STATE_NO_SERVER, "Go back or Error"}, + {To: STATE_NO_SERVER, Description: "Go back or Error"}, }, BackState: STATE_NO_SERVER, }, STATE_CHOSEN_SERVER: FSMState{ Transitions: []FSMTransition{ - {STATE_AUTHORIZED, "Found tokens in config"}, - {STATE_OAUTH_STARTED, "No tokens found in config"}, + {To: STATE_AUTHORIZED, Description: "Found tokens in config"}, + {To: STATE_OAUTH_STARTED, Description: "No tokens found in config"}, }, }, STATE_OAUTH_STARTED: FSMState{ Transitions: []FSMTransition{ - {STATE_AUTHORIZED, "User authorizes with browser"}, - {STATE_NO_SERVER, "Cancel or Error"}, - {STATE_SEARCH_SERVER, "Cancel or Error"}, + {To: STATE_AUTHORIZED, Description: "User authorizes with browser"}, + {To: STATE_NO_SERVER, Description: "Cancel or Error"}, + {To: STATE_SEARCH_SERVER, Description: "Cancel or Error"}, }, BackState: STATE_NO_SERVER, }, STATE_AUTHORIZED: FSMState{ Transitions: []FSMTransition{ - {STATE_OAUTH_STARTED, "Re-authorize with OAuth"}, - {STATE_REQUEST_CONFIG, "Client requests a config"}, + {To: STATE_OAUTH_STARTED, Description: "Re-authorize with OAuth"}, + {To: STATE_REQUEST_CONFIG, Description: "Client requests a config"}, }, }, STATE_REQUEST_CONFIG: FSMState{ Transitions: []FSMTransition{ - {STATE_ASK_PROFILE, "Multiple profiles found and no profile chosen"}, - {STATE_DISCONNECTED, "Only one profile or profile already chosen"}, - {STATE_NO_SERVER, "Cancel or Error"}, - {STATE_OAUTH_STARTED, "Re-authorize"}, + {To: STATE_ASK_PROFILE, Description: "Multiple profiles found and no profile chosen"}, + {To: STATE_DISCONNECTED, Description: "Only one profile or profile already chosen"}, + {To: STATE_NO_SERVER, Description: "Cancel or Error"}, + {To: STATE_OAUTH_STARTED, Description: "Re-authorize"}, }, }, STATE_ASK_PROFILE: FSMState{ Transitions: []FSMTransition{ - {STATE_DISCONNECTED, "User chooses profile"}, - {STATE_NO_SERVER, "Cancel or Error"}, - {STATE_SEARCH_SERVER, "Cancel or Error"}, + {To: STATE_DISCONNECTED, Description: "User chooses profile"}, + {To: STATE_NO_SERVER, Description: "Cancel or Error"}, + {To: STATE_SEARCH_SERVER, Description: "Cancel or Error"}, }, }, STATE_DISCONNECTED: FSMState{ Transitions: []FSMTransition{ - {STATE_CONNECTING, "OS reports it is trying to connect"}, - {STATE_REQUEST_CONFIG, "User reconnects"}, - {STATE_NO_SERVER, "User wants to choose a new server"}, - {STATE_OAUTH_STARTED, "Re-authorize with OAuth"}, + {To: STATE_CONNECTING, Description: "OS reports it is trying to connect"}, + {To: STATE_REQUEST_CONFIG, Description: "User reconnects"}, + {To: STATE_NO_SERVER, Description: "User wants to choose a new server"}, + {To: STATE_OAUTH_STARTED, Description: "Re-authorize with OAuth"}, }, BackState: STATE_NO_SERVER, }, STATE_DISCONNECTING: FSMState{ Transitions: []FSMTransition{ - {STATE_DISCONNECTED, "Cancel or Error"}, - {STATE_DISCONNECTED, "Done disconnecting"}, + {To: STATE_DISCONNECTED, Description: "Cancel or Error"}, + {To: STATE_DISCONNECTED, Description: "Done disconnecting"}, }, }, STATE_CONNECTING: FSMState{ Transitions: []FSMTransition{ - {STATE_DISCONNECTED, "Cancel or Error"}, - {STATE_CONNECTED, "Done connecting"}, + {To: STATE_DISCONNECTED, Description: "Cancel or Error"}, + {To: STATE_CONNECTED, Description: "Done connecting"}, }, }, STATE_CONNECTED: FSMState{ - Transitions: []FSMTransition{{STATE_DISCONNECTING, "App wants to disconnect"}}, + Transitions: []FSMTransition{{To: STATE_DISCONNECTING, Description: "App wants to disconnect"}}, }, } returnedFSM := fsm.FSM{} diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index 10abf4a..e99b715 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -78,7 +78,7 @@ type OAuthExchangeSession struct { // filled in when constructing the callback Context context.Context - Server http.Server + Server *http.Server } // Struct that defines the json format for /.well-known/vpn-user-portal" @@ -95,7 +95,7 @@ func (oauth *OAuth) getTokensWithCallback() error { oauth.Session.Context = context.Background() mux := http.NewServeMux() addr := "127.0.0.1:8000" - oauth.Session.Server = http.Server{ + oauth.Session.Server = &http.Server{ Addr: addr, Handler: mux, } @@ -200,7 +200,9 @@ func (oauth *OAuth) Callback(w http.ResponseWriter, req *http.Request) { code, success := req.URL.Query()["code"] // Shutdown after we're done defer func() { - go oauth.Session.Server.Shutdown(oauth.Session.Context) + if oauth.Session.Server != nil { + go oauth.Session.Server.Shutdown(oauth.Session.Context) + } }() if !success { oauth.Session.CallbackError = &types.WrappedErrorMessage{ @@ -308,7 +310,9 @@ func (oauth *OAuth) Cancel() { Message: "cancelled OAuth", Err: &OAuthCancelledCallbackError{}, } - oauth.Session.Server.Shutdown(oauth.Session.Context) + if oauth.Session.Server != nil { + oauth.Session.Server.Shutdown(oauth.Session.Context) + } } func (oauth *OAuth) EnsureTokens() error { |
