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
|
package v2
import (
"errors"
"fmt"
"net/url"
"time"
"github.com/eduvpn/eduvpn-common/internal/discovery"
"github.com/eduvpn/eduvpn-common/types/server"
)
type Server struct {
Profiles server.Profiles `json:"profiles"`
LastAuthorizeTime time.Time `json:"last_authorize_time,omitempty"`
ExpireTime time.Time `json:"expire_time,omitempty"`
// In case of secure internet:
CountryCode string `json:"country_code"`
}
type ServerType struct {
T server.Type
ID string
}
const keyFormat = "%d,%s"
func newServerType(key string) (*ServerType, error) {
var t server.Type
var id string
if _, err := fmt.Sscanf(key, keyFormat, &t, &id); err != nil {
return nil, err
}
return &ServerType{
T: t,
ID: id,
}, nil
}
func (st ServerType) MarshalText() ([]byte, error) {
k := fmt.Sprintf(keyFormat, st.T, st.ID)
return []byte(k), nil
}
func (st *ServerType) UnmarshalText(text []byte) error {
k := string(text)
g, err := newServerType(k)
if err != nil {
return err
}
*st = *g
return nil
}
type V2 struct {
List map[ServerType]*Server `json:"server_list,omitempty"`
LastChosen *ServerType `json:"last_chosen_id,omitempty"`
Discovery discovery.Discovery `json:"discovery"`
}
func (cfg *V2) RemoveServer(id string, t server.Type) error {
k := ServerType{
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 ServerType) (*Server, error) {
if v, ok := cfg.List[k]; ok {
return v, nil
}
return nil, errors.New("server does not exist")
}
func (cfg *V2) GetServer(id string, t server.Type) (*Server, error) {
k := ServerType{
ID: id,
T: t,
}
return cfg.getServerWithKey(k)
}
func (cfg *V2) CurrentServer() (*Server, *ServerType, 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
}
func (cfg *V2) HasSecureInternet() bool {
for k := range cfg.List {
if k.T == server.TypeSecureInternet {
return true
}
}
return false
}
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 := ServerType{
ID: id,
T: t,
}
if cfg.List == nil {
cfg.List = make(map[ServerType]*Server)
}
cfg.List[k] = &srv
return nil
}
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
}
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
}
|