1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
package client
import (
"github.com/eduvpn/eduvpn-common/internal/fsm"
"github.com/eduvpn/eduvpn-common/internal/server"
"github.com/go-errors/errors"
)
type (
FSMStateID = fsm.StateID
FSMStates = fsm.States
FSMState = fsm.State
FSMTransition = fsm.Transition
)
const (
// StateDeregistered means the app is not registered with the wrapper.
StateDeregistered FSMStateID = iota
// StateNoServer means the user has not chosen a server yet.
StateNoServer
// 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
// StateLoadingServer means we are loading the server details.
StateLoadingServer
// StateChosenServer means the user has chosen a server to connect to.
StateChosenServer
// StateOAuthStarted means the OAuth process has started.
StateOAuthStarted
// StateAuthorized means the OAuth process has finished and the user is now authorized with the server.
StateAuthorized
// StateRequestConfig means the user has requested a config for connecting.
StateRequestConfig
// 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
// 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
)
func GetStateName(s FSMStateID) string {
switch s {
case StateDeregistered:
return "Deregistered"
case StateNoServer:
return "No_Server"
case StateAskLocation:
return "Ask_Location"
case StateSearchServer:
return "Search_Server"
case StateLoadingServer:
return "Loading_Server"
case StateChosenServer:
return "Chosen_Server"
case StateOAuthStarted:
return "OAuth_Started"
case StateDisconnected:
return "Disconnected"
case StateRequestConfig:
return "Request_Config"
case StateAskProfile:
return "Ask_Profile"
case StateAuthorized:
return "Authorized"
case StateDisconnecting:
return "Disconnecting"
case StateConnecting:
return "Connecting"
case StateConnected:
return "Connected"
default:
panic("unknown conversion of state to string")
}
}
func newFSM(
callback func(FSMStateID, FSMStateID, interface{}) bool,
directory string,
debug bool,
) fsm.FSM {
states := FSMStates{
StateDeregistered: FSMState{
Transitions: []FSMTransition{{To: StateNoServer, Description: "Client registers"}},
},
StateNoServer: FSMState{
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{
Transitions: []FSMTransition{
{To: StateLoadingServer, Description: "User clicks a server in the UI"},
{To: StateNoServer, Description: "Cancel or Error"},
},
},
StateAskLocation: FSMState{
Transitions: []FSMTransition{
{To: StateChosenServer, Description: "Location chosen"},
{To: StateNoServer, Description: "Go back or Error"},
{To: StateSearchServer, Description: "Cancel or Error"},
},
},
StateLoadingServer: FSMState{
Transitions: []FSMTransition{
{To: StateChosenServer, Description: "Server info loaded"},
{
To: StateAskLocation,
Description: "User chooses a Secure Internet server but no location is configured",
},
{To: StateNoServer, Description: "Go back or Error"},
},
},
StateChosenServer: FSMState{
Transitions: []FSMTransition{
{To: StateAuthorized, Description: "Found tokens in config"},
{To: StateOAuthStarted, Description: "No tokens found in config"},
},
},
StateOAuthStarted: FSMState{
Transitions: []FSMTransition{
{To: StateAuthorized, Description: "User authorizes with browser"},
{To: StateNoServer, Description: "Go back or Error"},
{To: StateSearchServer, Description: "Cancel or Error"},
},
},
StateAuthorized: FSMState{
Transitions: []FSMTransition{
{To: StateOAuthStarted, Description: "Re-authorize with OAuth"},
{To: StateRequestConfig, Description: "Client requests a config"},
{To: StateNoServer, Description: "Client wants to go back to the main screen"},
},
},
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: 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"},
},
},
StateConnecting: FSMState{
Transitions: []FSMTransition{
{To: StateDisconnected, Description: "Cancel or Error"},
{To: StateConnected, Description: "Done connecting"},
},
},
StateConnected: FSMState{
Transitions: []FSMTransition{
{To: StateDisconnecting, Description: "App wants to disconnect"},
},
},
}
returnedFSM := fsm.FSM{}
returnedFSM.Init(StateDeregistered, states, callback, directory, GetStateName, debug)
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
c.Logger.Warningf("Already connected")
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
c.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
c.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
c.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 {
c.logError(err)
return err
}
c.FSM.GoTransitionWithData(StateDisconnected, srv)
return nil
}
// goBackInternal uses the public go back but logs an error if it happened.
func (c *Client) goBackInternal() {
err := c.GoBack()
if err != nil {
// TODO(jwijenbergh): Bit suspicious - logging level INFO, yet stacktrace logged.
c.Logger.Infof("failed going back: %s\nstacktrace:\n%s", err.Error(), err.(*errors.Error).ErrorStack())
}
}
// 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.
func (c *Client) CancelOAuth() error {
if !c.InFSMState(StateOAuthStarted) {
return errors.Errorf("fsm attempt cancelling OAuth while in '%v'", c.FSM.Current)
}
srv, err := c.Servers.GetCurrentServer()
if err != nil {
c.logError(err)
} else {
server.CancelOAuth(srv)
}
return err
}
// InFSMState is a helper to check if the FSM is in state `checkState`.
func (c *Client) InFSMState(checkState FSMStateID) bool {
return c.FSM.InState(checkState)
}
|