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
|
// Package client implements the public interface for creating eduVPN/Let's Connect! clients
package client
import (
"fmt"
"strings"
"github.com/eduvpn/eduvpn-common/internal/config"
"github.com/eduvpn/eduvpn-common/internal/discovery"
"github.com/eduvpn/eduvpn-common/internal/failover"
"github.com/eduvpn/eduvpn-common/internal/fsm"
"github.com/eduvpn/eduvpn-common/internal/http"
"github.com/eduvpn/eduvpn-common/internal/log"
"github.com/eduvpn/eduvpn-common/internal/oauth"
"github.com/eduvpn/eduvpn-common/internal/server"
"github.com/eduvpn/eduvpn-common/internal/util"
"github.com/eduvpn/eduvpn-common/types"
"github.com/go-errors/errors"
)
type (
// ServerBase is an alias to the internal ServerBase
// This contains the details for each server.
ServerBase = server.Base
)
func (c *Client) logError(err error) {
// Logs the error with the same level/verbosity as the error
if c.Debug {
log.Logger.Inherit(err, fmt.Sprintf("\nwith stacktrace: %s\n", err.(*errors.Error).ErrorStack()))
} else {
log.Logger.Inherit(err, "")
}
}
func (c *Client) isLetsConnect() bool {
// see https://git.sr.ht/~fkooman/vpn-user-portal/tree/v3/item/src/OAuth/VpnClientDb.php
return strings.HasPrefix(c.Name, "org.letsconnect-vpn.app")
}
// isAllowedClientID checks if the 'clientID' is in the list of allowed client IDs
func isAllowedClientID(clientID string) bool {
allowList := []string{
// eduVPN
"org.eduvpn.app.windows",
"org.eduvpn.app.android",
"org.eduvpn.app.ios",
"org.eduvpn.app.macos",
"org.eduvpn.app.linux",
// Let's Connect!
"org.letsconnect-vpn.app.windows",
"org.letsconnect-vpn.app.android",
"org.letsconnect-vpn.app.ios",
"org.letsconnect-vpn.app.macos",
"org.letsconnect-vpn.app.linux",
}
for _, x := range allowList {
if x == clientID {
return true
}
}
return false
}
func userAgentName(clientID string) string {
switch clientID {
case "org.eduvpn.app.windows":
return "eduVPN for Windows"
case "org.eduvpn.app.android":
return "eduVPN for Android"
case "org.eduvpn.app.ios":
return "eduVPN for iOS"
case "org.eduvpn.app.macos":
return "eduVPN for macOS"
case "org.eduvpn.app.linux":
return "eduVPN for Linux"
case "org.letsconnect-vpn.app.windows":
return "Let's Connect! for Windows"
case "org.letsconnect-vpn.app.android":
return "Let's Connect! for Android"
case "org.letsconnect-vpn.app.ios":
return "Let's Connect! for iOS"
case "org.letsconnect-vpn.app.macos":
return "Let's Connect! for macOS"
case "org.letsconnect-vpn.app.linux":
return "Let's Connect! for Linux"
default:
return "unknown"
}
}
// Client is the main struct for the VPN client.
type Client struct {
// The name of the client
Name string `json:"-"`
// The language used for language matching
Language string `json:"-"` // language should not be saved
// The chosen server
Servers server.Servers `json:"servers"`
// The list of servers and organizations from disco
Discovery discovery.Discovery `json:"discovery"`
// The fsm
FSM fsm.FSM `json:"-"`
// The config
Config config.Config `json:"-"`
// Whether or not this client supports WireGuard
SupportsWireguard bool `json:"-"`
// Whether to enable debugging
Debug bool `json:"-"`
// The Failover monitor for the current VPN connection
Failover *failover.DroppedConMon `json:"-"`
// tokenCB is the token callback
tokenCB func(srv server.Server, tok oauth.Token)
}
func (c *Client) ForwardTokenUpdate(srv server.Server) {
if c.tokenCB == nil {
log.Logger.Debugf("No token update callback available")
return
}
t := oauth.Token{}
o := srv.OAuth()
if o != nil {
t = o.Token()
} else {
log.Logger.Debugf("OAuth was nil when forwarding token callback")
}
log.Logger.Debugf("Running token callback")
c.tokenCB(srv, t)
}
// SetTokenUpdater sets the token updater callback
func (c *Client) SetTokenUpdater(updater func(srv server.Server, tok oauth.Token)) {
c.tokenCB = updater
}
// Register initializes the clientwith the following parameters:
// - name: the name of the client
// - directory: the directory where the config files are stored. Absolute or relative
// - stateCallback: the callback function for the FSM that takes two states (old and new) and the data as an interface
// - debug: whether or not we want to enable debugging
//
// It returns an error if initialization failed, for example when discovery cannot be obtained and when there are no servers.
func (c *Client) Register(
name string,
version string,
directory string,
language string,
stateCallback func(FSMStateID, FSMStateID, interface{}) bool,
debug bool,
) (err error) {
defer func() {
if err != nil {
c.logError(err)
}
}()
if !c.InFSMState(StateDeregistered) {
return errors.Errorf("fsm attempt to register while in '%v'", c.FSM.Current)
}
if !isAllowedClientID(name) {
return errors.Errorf("client ID is not allowed: '%v', see https://git.sr.ht/~fkooman/vpn-user-portal/tree/v3/item/src/OAuth/VpnClientDb.php for a list of allowed IDs", name)
}
if len([]rune(version)) > 20 {
return errors.Errorf("version is not allowed: '%s', must be max 20 characters", version)
}
http.RegisterAgent(userAgentName(name), version)
c.Name = name
// TODO: Verify language setting?
c.Language = language
// Initialize the logger
lvl := log.LevelWarning
if debug {
lvl = log.LevelDebug
}
if err = log.Logger.Init(lvl, directory); err != nil {
return err
}
// Initialize the FSM
c.FSM = newFSM(stateCallback, directory, debug)
// By default we support wireguard
c.SupportsWireguard = true
// Debug only if given
c.Debug = debug
// Initialize the Config
c.Config.Init(directory, "state")
// Try to load the previous configuration
if c.Config.Load(&c) != nil {
// This error can be safely ignored, as when the config does not load, the struct will not be filled
log.Logger.Infof("Previous configuration not found")
}
// Go to the No Server state with the saved servers after we're done
defer c.FSM.GoTransitionWithData(StateNoServer, c.Servers)
// Let's Connect! doesn't care about discovery
if c.isLetsConnect() {
return nil
}
// Check if we are able to fetch discovery, and log if something went wrong
if _, err := c.DiscoServers(); err != nil {
log.Logger.Warningf("Failed to get discovery servers: %v", err)
}
if _, err := c.DiscoOrganizations(); err != nil {
log.Logger.Warningf("Failed to get discovery organizations: %v", err)
}
return nil
}
// Deregister 'deregisters' the client, meaning saving the log file and the config and emptying out the client struct.
func (c *Client) Deregister() {
// Close the log file
_ = log.Logger.Close()
// Save the config
if err := c.Config.Save(&c); err != nil {
log.Logger.Infof("c.Config.Save failed: %s\nstacktrace:\n%s", err.Error(), err.(*errors.Error).ErrorStack())
}
// Empty out the state
*c = Client{}
}
// askProfile asks the user for a profile by moving the FSM to the ASK_PROFILE state.
func (c *Client) askProfile(srv server.Server) error {
ps, err := server.ValidProfiles(srv, c.SupportsWireguard)
if err != nil {
return err
}
if err = c.FSM.GoTransitionRequired(StateAskProfile, ps); err != nil {
return err
}
return nil
}
// DiscoOrganizations gets the organizations list from the discovery server
// If the list cannot be retrieved an error is returned.
// If this is the case then a previous version of the list is returned if there is any.
// This takes into account the frequency of updates, see: https://github.com/eduvpn/documentation/blob/v3/SERVER_DISCOVERY.md#organization-list.
func (c *Client) DiscoOrganizations() (orgs *types.DiscoveryOrganizations, err error) {
defer func() {
if err != nil {
c.logError(err)
}
}()
// Not supported with Let's Connect!
if c.isLetsConnect() {
return nil, errors.Errorf("discovery with Let's Connect is not supported")
}
// Mark organizations as expired if we have not set an organization yet
if !c.Servers.HasSecureInternet() {
c.Discovery.MarkOrganizationsExpired()
}
return c.Discovery.Organizations()
}
// DiscoServers gets the servers list from the discovery server
// If the list cannot be retrieved an error is returned.
// If this is the case then a previous version of the list is returned if there is any.
// This takes into account the frequency of updates, see: https://github.com/eduvpn/documentation/blob/v3/SERVER_DISCOVERY.md#server-list.
func (c *Client) DiscoServers() (dss *types.DiscoveryServers, err error) {
defer func() {
if err != nil {
c.logError(err)
}
}()
// Not supported with Let's Connect!
if c.isLetsConnect() {
return nil, errors.Errorf("discovery with Let's Connect is not supported")
}
return c.Discovery.Servers()
}
// GetTranslated gets the translation for `languages` using the current state language.
func (c *Client) GetTranslated(languages map[string]string) string {
return util.GetLanguageMatched(languages, c.Language)
}
|