summaryrefslogtreecommitdiff
path: root/internal/config/v2/v2.go
blob: 51f7a5f8d86e088c06e7af3f3e922eacf6d1cc6c (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
// Package v2 implements version 2 of the state file
package v2

import (
	"errors"
	"fmt"
	"net/url"
	"time"

	"github.com/eduvpn/eduvpn-common/internal/discovery"
	"github.com/eduvpn/eduvpn-common/types/server"
)

// Server is the struct for each server
type Server struct {
	// Profiles are the list of profiles
	Profiles server.Profiles `json:"profiles"`
	// LastAuthorizeTime is the time we last authorized
	// This is used for determining when to show e.g. the renew button
	LastAuthorizeTime time.Time `json:"last_authorize_time,omitempty"`
	// ExpireTime is the time at which the VPN expires
	ExpireTime time.Time `json:"expire_time,omitempty"`

	// CountryCode is the country code for the server in case of secure internet
	// Otherwise it is an empty string
	CountryCode string `json:"country_code"`
}

// ServerKey is the key type of the server map
type ServerKey struct {
	// T is the type of server, e.g. secure internet
	T server.Type
	// ID is the identifier for the server
	ID string
}

const keyFormat = "%d,%s"

func newServerType(key string) (*ServerKey, error) {
	var t server.Type
	var id string
	if _, err := fmt.Sscanf(key, keyFormat, &t, &id); err != nil {
		return nil, err
	}

	return &ServerKey{
		T:  t,
		ID: id,
	}, nil
}

// MarshalText convers the server key into one that can be used in a map
func (st ServerKey) MarshalText() ([]byte, error) {
	k := fmt.Sprintf(keyFormat, st.T, st.ID)
	return []byte(k), nil
}

// UnmarshalText converts the marshaled key into a ServerType struct
func (st *ServerKey) UnmarshalText(text []byte) error {
	k := string(text)
	g, err := newServerType(k)
	if err != nil {
		return err
	}
	*st = *g
	return nil
}

// V2 is the top-level struct for the state file
type V2 struct {
	// List is the list of servers
	List map[ServerKey]*Server `json:"server_list,omitempty"`
	// LastChosen represents the key of the last chosen server
	// A server is chosen if we got a config for it
	LastChosen *ServerKey `json:"last_chosen_id,omitempty"`
	// Discovery is the cached list of discovery JSON
	Discovery discovery.Discovery `json:"discovery"`
}

// RemoveServer removes a server with id `id` and type `t` from the V2 struct
// It returns an error if no such server exists
func (cfg *V2) RemoveServer(id string, t server.Type) error {
	k := ServerKey{
		ID: id,
		T:  t,
	}

	if _, ok := cfg.List[k]; ok {
		delete(cfg.List, k)

		// reset the last chosen
		if cfg.LastChosen != nil && *cfg.LastChosen == k {
			cfg.LastChosen = nil
		}
		return nil
	}
	return errors.New("server does not exist")
}

func (cfg *V2) getServerWithKey(k ServerKey) (*Server, error) {
	if v, ok := cfg.List[k]; ok {
		return v, nil
	}
	return nil, errors.New("server does not exist")
}

// GetServer gets a server with id `id` and type `t`
// If the server doesn't exist it returns nil and an error
func (cfg *V2) GetServer(id string, t server.Type) (*Server, error) {
	k := ServerKey{
		ID: id,
		T:  t,
	}
	return cfg.getServerWithKey(k)
}

// CurrentServer gets the last chosen server
// It returns the server, the server type and an error if it doesn't exist
func (cfg *V2) CurrentServer() (*Server, *ServerKey, error) {
	if cfg.LastChosen == nil {
		return nil, nil, errors.New("no server chosen before")
	}
	srv, err := cfg.getServerWithKey(*cfg.LastChosen)
	if err != nil {
		return nil, nil, err
	}
	return srv, cfg.LastChosen, nil
}

// HasSecureInternet returns true whether or not the state file
// has a secure internet server in it
func (cfg *V2) HasSecureInternet() bool {
	for k := range cfg.List {
		if k.T == server.TypeSecureInternet {
			return true
		}
	}
	return false
}

// AddServer adds a server with id `id`, type `t` and server `srv`
func (cfg *V2) AddServer(id string, t server.Type, srv Server) error {
	if cfg.HasSecureInternet() && t == server.TypeSecureInternet {
		return errors.New("a secure internet server already exists, remove the other secure internet server first")
	}
	k := ServerKey{
		ID: id,
		T:  t,
	}
	if cfg.List == nil {
		cfg.List = make(map[ServerKey]*Server)
	}
	cfg.List[k] = &srv
	return nil
}

// PublicCurrent gets the current server as a type that should be returned to the client
// It returns this server or nil and an error if it doesn't exist
func (cfg *V2) PublicCurrent(disco *discovery.Discovery) (*server.Current, error) {
	curr, _, err := cfg.CurrentServer()
	if err != nil {
		return nil, err
	}
	rcurr := &server.Current{}
	// SAFETY: LastChosen is guaranteed to be non-nil here
	switch cfg.LastChosen.T {
	case server.TypeInstituteAccess:
		g, err := convertInstitute(cfg.LastChosen.ID, disco)
		if err != nil {
			return nil, err
		}
		g.Profiles = curr.Profiles
		rcurr.Institute = g
	case server.TypeSecureInternet:
		g, err := convertSecure(cfg.LastChosen.ID, curr.CountryCode, disco)
		if err != nil {
			return nil, err
		}
		g.Profiles = curr.Profiles
		rcurr.SecureInternet = g
	case server.TypeCustom:
		g, err := convertCustom(cfg.LastChosen.ID)
		if err != nil {
			return nil, err
		}
		g.Profiles = curr.Profiles
		rcurr.Custom = g
	default:
		return nil, fmt.Errorf("unknown connected type: %d", cfg.LastChosen.T)
	}
	rcurr.Type = cfg.LastChosen.T
	return rcurr, nil
}

func convertInstitute(url string, disco *discovery.Discovery) (*server.Institute, error) {
	dsrv, err := disco.ServerByURL(url, "institute_access")
	if err != nil {
		return nil, err
	}

	return &server.Institute{
		Server: server.Server{
			DisplayName: dsrv.DisplayName,
			Identifier:  url,
		},
		SupportContacts: dsrv.SupportContact,
	}, nil
}

func convertCustom(u string) (*server.Server, error) {
	pu, err := url.Parse(u)
	if err != nil {
		return nil, err
	}
	return &server.Server{
		DisplayName: map[string]string{
			"en": pu.Hostname(),
		},
		Identifier: u,
	}, nil
}

func convertSecure(orgID string, countryCode string, disco *discovery.Discovery) (*server.SecureInternet, error) {
	dorg, _, err := disco.SecureHomeArgs(orgID)
	if err != nil {
		return nil, err
	}
	return &server.SecureInternet{
		Server: server.Server{
			DisplayName: dorg.DisplayName,
			Identifier:  dorg.OrgID,
		},
		CountryCode: countryCode,
		Locations:   disco.SecureLocationList(),
	}, nil
}

// PublicList gets all the servers in a format that is returned to the client
func (cfg *V2) PublicList(disco *discovery.Discovery) *server.List {
	ret := &server.List{}
	// TODO: profile information?
	for k, v := range cfg.List {
		switch k.T {
		case server.TypeInstituteAccess:
			g, err := convertInstitute(k.ID, disco)
			if err != nil || g == nil {
				// TODO: log/delisted?
				continue
			}
			g.Profiles = v.Profiles
			ret.Institutes = append(ret.Institutes, *g)
		case server.TypeSecureInternet:
			g, err := convertSecure(k.ID, v.CountryCode, disco)
			if err != nil || g == nil {
				// TODO: log/delisted?
				continue
			}
			g.Profiles = v.Profiles
			ret.SecureInternet = g
		case server.TypeCustom:
			g, err := convertCustom(k.ID)
			if err != nil || g == nil {
				// TODO: log/delisted?
				continue
			}
			g.Profiles = v.Profiles
			ret.Custom = append(ret.Custom, *g)
		default:
			// TODO: log
			continue
		}
	}
	return ret
}