summaryrefslogtreecommitdiff
path: root/internal/server.go
blob: 489719ebd99d4413b3f152f252be66ce53774eaa (plain)
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
package internal

import (
	"encoding/json"
	"errors"
	"fmt"
)

type Server struct {
	BaseURL     string            `json:"base_url"`
	Endpoints   ServerEndpoints   `json:"endpoints"`
	OAuth       OAuth             `json:"oauth"`
	Profiles    ServerProfileInfo `json:"profiles"`
	ProfilesRaw string            `json:"profiles_raw"`
	Logger      *FileLogger       `json:"-"`
	FSM         *FSM              `json:"-"`
}

type Servers struct {
	List       map[string]*Server `json:"list"`
	Current    string             `json:"current"`
	SecureHome string             `json:"secure_home"`
}

func (servers *Servers) GetCurrentServer() (*Server, error) {
	if servers.List == nil {
		return nil, &ServerGetCurrentNoMapError{}
	}
	server, exists := servers.List[servers.Current]

	if !exists || server == nil {
		return nil, &ServerGetCurrentNotFoundError{}
	}
	return server, nil
}

func (server *Server) CancelOAuth() {
	server.OAuth.Cancel()
}

func (server *Server) Init(url string, fsm *FSM, logger *FileLogger) error {
	server.BaseURL = url
	server.FSM = fsm
	server.Logger = logger
	server.OAuth.Init(fsm, logger)
	endpointsErr := server.GetEndpoints()
	if endpointsErr != nil {
		return &ServerInitializeError{URL: url, Err: endpointsErr}
	}
	return nil
}

func (server *Server) EnsureTokens() error {
	if server.OAuth.NeedsRelogin() {
		server.Logger.Log(LOG_INFO, "OAuth: Tokens are invalid, relogging in")
		return server.Login()
	}
	return nil
}

func (servers *Servers) EnsureServer(url string, fsm *FSM, logger *FileLogger, makeCurrent bool) (*Server, error) {
	if url == "" {
		return nil, &ServerEnsureServerEmptyURLError{}
	}
	if servers.List == nil {
		servers.List = make(map[string]*Server)
	}

	server, exists := servers.List[url]

	if !exists || server == nil {
		server = &Server{}
	}
	serverInitErr := server.Init(url, fsm, logger)

	if serverInitErr != nil {
		return nil, &ServerEnsureServerError{Err: serverInitErr}
	}
	servers.List[url] = server

	if makeCurrent {
		servers.Current = url
	}
	return server, nil
}

func (servers *Servers) getSecureInternetHome() (*Server, error) {
	server, exists := servers.List[servers.SecureHome]

	if !exists || server == nil {
		return nil, &ServerGetSecureInternetHomeError{}
	}

	return server, nil
}

func (servers *Servers) EnsureSecureHome(server *Server) {
	if servers.SecureHome == "" {
		servers.SecureHome = server.BaseURL
	}
}

func (servers *Servers) CopySecureInternetOAuth(server *Server) error {
	secureHome, secureHomeErr := servers.getSecureInternetHome()

	if secureHomeErr != nil {
		return &ServerCopySecureInternetOAuthError{Err: secureHomeErr}
	}

	// Forward token properties
	server.OAuth = secureHome.OAuth
	return nil
}

type ServerProfile struct {
	ID             string   `json:"profile_id"`
	DisplayName    string   `json:"display_name"`
	VPNProtoList   []string `json:"vpn_proto_list"`
	DefaultGateway bool     `json:"default_gateway"`
}

type ServerProfileInfo struct {
	Current string `json:"current_profile"`
	Info    struct {
		ProfileList []ServerProfile `json:"profile_list"`
	} `json:"info"`
}

type ServerEndpointList struct {
	API           string `json:"api_endpoint"`
	Authorization string `json:"authorization_endpoint"`
	Token         string `json:"token_endpoint"`
}

// Struct that defines the json format for /.well-known/vpn-user-portal"
type ServerEndpoints struct {
	API struct {
		V2 ServerEndpointList `json:"http://eduvpn.org/api#2"`
		V3 ServerEndpointList `json:"http://eduvpn.org/api#3"`
	} `json:"api"`
	V string `json:"v"`
}

func (server *Server) Login() error {
	return server.OAuth.Login("org.eduvpn.app.linux", server.Endpoints.API.V3.Authorization, server.Endpoints.API.V3.Token)
}

func (server *Server) NeedsRelogin() bool {
	// Check if OAuth needs relogin
	return server.OAuth.NeedsRelogin()
}

func (server *Server) GetEndpoints() error {
	url := server.BaseURL + "/.well-known/vpn-user-portal"
	_, body, bodyErr := HTTPGet(url)

	if bodyErr != nil {
		return &ServerGetEndpointsError{Err: bodyErr}
	}

	endpoints := ServerEndpoints{}
	jsonErr := json.Unmarshal(body, &endpoints)

	if jsonErr != nil {
		return &ServerGetEndpointsError{Err: jsonErr}
	}

	server.Endpoints = endpoints

	return nil
}

func (profile *ServerProfile) supportsWireguard() bool {
	for _, proto := range profile.VPNProtoList {
		if proto == "wireguard" {
			return true
		}
	}
	return false
}

func (server *Server) getCurrentProfile() (*ServerProfile, error) {
	profileID := server.Profiles.Current
	for _, profile := range server.Profiles.Info.ProfileList {
		if profile.ID == profileID {
			return &profile, nil
		}
	}
	return nil, &ServerGetCurrentProfileNotFoundError{ProfileID: profileID}
}

func (server *Server) getConfigWithProfile() (string, error) {
	if !server.FSM.HasTransition(HAS_CONFIG) {
		return "", &FSMWrongStateTransitionError{Got: server.FSM.Current, Want: HAS_CONFIG}
	}
	profile, profileErr := server.getCurrentProfile()

	if profileErr != nil {
		return "", &ServerGetConfigWithProfileError{Err: profileErr}
	}

	if profile.supportsWireguard() {
		return server.WireguardGetConfig()
	}
	return server.OpenVPNGetConfig()
}

func (server *Server) askForProfileID() error {
	if !server.FSM.HasTransition(ASK_PROFILE) {
		return &FSMWrongStateTransitionError{Got: server.FSM.Current, Want: ASK_PROFILE}
	}
	server.FSM.GoTransitionWithData(ASK_PROFILE, server.ProfilesRaw, false)
	return nil
}

func (server *Server) GetConfig() (string, error) {
	if !server.FSM.InState(REQUEST_CONFIG) {
		return "", errors.New(fmt.Sprintf("cannot get a config, invalid state %s", server.FSM.Current.String()))
	}
	infoErr := server.APIInfo()

	if infoErr != nil {
		return "", &ServerGetConfigError{Err: infoErr}
	}

	// Set the current profile if there is only one profile
	if len(server.Profiles.Info.ProfileList) == 1 {
		server.Profiles.Current = server.Profiles.Info.ProfileList[0].ID
		return server.getConfigWithProfile()
	}

	profileErr := server.askForProfileID()

	if profileErr != nil {
		return "", &ServerGetConfigError{Err: profileErr}
	}

	return server.getConfigWithProfile()
}

type ServerGetCurrentProfileNotFoundError struct {
	ProfileID string
}

func (e *ServerGetCurrentProfileNotFoundError) Error() string {
	return fmt.Sprintf("failed to get current profile, profile with ID: %s not found", e.ProfileID)
}

type ServerGetConfigWithProfileError struct {
	Err error
}

func (e *ServerGetConfigWithProfileError) Error() string {
	return fmt.Sprintf("failed to get config including profile with error %v", e.Err)
}

type ServerGetEndpointsError struct {
	Err error
}

func (e *ServerGetEndpointsError) Error() string {
	return fmt.Sprintf("failed to get server endpoint with error %v", e.Err)
}

type ServerGetSecureInternetHomeError struct{}

func (e *ServerGetSecureInternetHomeError) Error() string {
	return "failed to get secure internet home server, not found"
}

type ServerCopySecureInternetOAuthError struct {
	Err error
}

func (e *ServerCopySecureInternetOAuthError) Error() string {
	return fmt.Sprintf("failed to copy oauth tokens from home server with error %v", e.Err)
}

type ServerEnsureServerEmptyURLError struct{}

func (e *ServerEnsureServerEmptyURLError) Error() string {
	return "failed ensuring server, empty url provided"
}

type ServerEnsureServerError struct {
	Err error
}

func (e *ServerEnsureServerError) Error() string {
	return fmt.Sprintf("failed ensuring server with error %v", e.Err)
}

type ServerGetCurrentNoMapError struct{}

func (e *ServerGetCurrentNoMapError) Error() string {
	return "failed getting current server, no servers available"
}

type ServerGetCurrentNotFoundError struct{}

func (e *ServerGetCurrentNotFoundError) Error() string {
	return "failed getting current server, not found"
}

type ServerGetConfigError struct {
	Err error
}

func (e *ServerGetConfigError) Error() string {
	return fmt.Sprintf("failed getting server config with error %v", e.Err)
}

type ServerInitializeError struct {
	URL string
	Err error
}

func (e *ServerInitializeError) Error() string {
	return fmt.Sprintf("failed initializing server with url %s and error %v", e.URL, e.Err)
}