diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-06-21 16:54:46 +0200 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2022-06-21 16:54:46 +0200 |
| commit | 24be04cf37dbbeaf7fa33fa027ea6b54823e21f2 (patch) | |
| tree | 5394f8b13e6713167c7e2fc0db133143ef4c84e6 | |
| parent | 1393ab1888e50cf623d22a8135daf42bda333f99 (diff) | |
Server: Add the ability to get the saved servers
| -rw-r--r-- | internal/server/server.go | 11 | ||||
| -rw-r--r-- | state.go | 27 |
2 files changed, 35 insertions, 3 deletions
diff --git a/internal/server/server.go b/internal/server/server.go index ce72400..807bd09 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,6 +1,7 @@ package server import ( + "encoding/json" "fmt" "github.com/jwijenbergh/eduvpn-common/internal/fsm" @@ -68,6 +69,16 @@ func (servers *Servers) GetCurrentServer() (Server, error) { return institute, nil } +func (servers *Servers) GetJSON() (string, error) { + bytes, bytesErr := json.Marshal(servers) + + if bytesErr != nil { + return "", bytesErr + } + + return string(bytes), nil +} + type Servers struct { InstituteServers InstituteServers `json:"institute_servers"` SecureInternetHomeServer SecureInternetHomeServer `json:"secure_internet_home"` @@ -32,6 +32,16 @@ type VPNState struct { Identifier string `json:"identifier"` } +func (state *VPNState) GetSavedServers() string { + serversJSON, serversJSONErr := state.Servers.GetJSON() + + if serversJSONErr != nil { + return "" + } + + return serversJSON +} + func (state *VPNState) Register(name string, directory string, stateCallback func(string, string, string), debug bool) error { errorMessage := "failed to register with the GO library" if !state.FSM.InState(fsm.DEREGISTERED) { @@ -64,7 +74,9 @@ func (state *VPNState) Register(name string, directory string, stateCallback fun // This error can be safely ignored, as when the config does not load, the struct will not be filled state.Logger.Log(log.LOG_INFO, "Previous configuration not found") } - state.FSM.GoTransition(fsm.NO_SERVER) + + // Go to the No Server state with the saved servers + state.FSM.GoTransitionWithData(fsm.NO_SERVER, state.GetSavedServers(), false) return nil } @@ -205,9 +217,18 @@ func (state *VPNState) SetIdentifier(identifier string) { state.Identifier = identifier } +func (state *VPNState) SetSearchServer() error { + if !state.FSM.HasTransition(fsm.SEARCH_SERVER) { + return &types.WrappedErrorMessage{Message: "failed to set search server", Err: fsm.WrongStateTransitionError{Got: state.FSM.Current, Want: fsm.CONNECTED}.CustomError()} + } + + state.FSM.GoTransition(fsm.SEARCH_SERVER) + return nil +} + func (state *VPNState) SetConnected() error { if !state.FSM.HasTransition(fsm.CONNECTED) { - return fsm.WrongStateTransitionError{Got: state.FSM.Current, Want: fsm.CONNECTED}.CustomError() + return &types.WrappedErrorMessage{Message: "failed to set connected", Err: fsm.WrongStateTransitionError{Got: state.FSM.Current, Want: fsm.CONNECTED}.CustomError()} } state.FSM.GoTransition(fsm.CONNECTED) @@ -216,7 +237,7 @@ func (state *VPNState) SetConnected() error { func (state *VPNState) SetDisconnected() error { if !state.FSM.HasTransition(fsm.HAS_CONFIG) { - return fsm.WrongStateTransitionError{Got: state.FSM.Current, Want: fsm.HAS_CONFIG}.CustomError() + return &types.WrappedErrorMessage{Message: "failed to set disconnected", Err: fsm.WrongStateTransitionError{Got: state.FSM.Current, Want: fsm.HAS_CONFIG}.CustomError()} } state.FSM.GoTransition(fsm.HAS_CONFIG) |
