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
|
package client
import (
"context"
"sort"
"strings"
"sync"
"github.com/eduvpn/eduvpn-common/i18nerr"
"github.com/eduvpn/eduvpn-common/internal/discovery"
"github.com/eduvpn/eduvpn-common/internal/log"
"github.com/eduvpn/eduvpn-common/types/cookie"
discotypes "github.com/eduvpn/eduvpn-common/types/discovery"
)
func (c *Client) hasDiscovery() bool {
// see https://git.sr.ht/~fkooman/vpn-user-portal/tree/v3/item/src/OAuth/VpnClientDb.php
return strings.HasPrefix(c.Name, "org.eduvpn.app")
}
// DiscoOrganizations gets the organizations list from the discovery server with search string `search`
// 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(ck *cookie.Cookie, search string) (*discotypes.Organizations, error) {
// Not supported with Let's Connect! & govVPN
if !c.hasDiscovery() {
return nil, i18nerr.NewInternal("Server/organization discovery with this client ID is not supported")
}
disco, release := c.discoMan.Discovery(true)
defer release()
orgs, fresh, err := disco.Organizations(ck.Context())
if fresh {
defer c.TrySave()
}
if err != nil {
err = i18nerr.Wrap(err, "Failed to obtain the list of organizations")
}
if orgs == nil {
return nil, err
}
// convert to public subset
var retOrgs []discotypes.Organization
for _, v := range orgs.List {
if search == "" {
retOrgs = append(retOrgs, v.Organization)
continue
}
score := v.Score(search)
if score < 0 {
continue
}
v.Organization.Score = score
retOrgs = append(retOrgs, v.Organization)
}
if search != "" {
sort.Slice(retOrgs, func(i, j int) bool {
// lower score is better
return retOrgs[i].Score < retOrgs[j].Score
})
}
return &discotypes.Organizations{
List: retOrgs,
}, err
}
// DiscoServers gets the servers list from the discovery server with search string `search`
// 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(ck *cookie.Cookie, search string) (*discotypes.Servers, error) {
// Not supported with Let's Connect! & govVPN
if !c.hasDiscovery() {
return nil, i18nerr.NewInternal("Server/organization discovery with this client ID is not supported")
}
disco, release := c.discoMan.Discovery(true)
defer release()
servs, fresh, err := disco.Servers(ck.Context())
if fresh {
defer c.TrySave()
}
if err != nil {
err = i18nerr.Wrap(err, "Failed to obtain the list of servers")
}
if servs == nil {
return nil, err
}
// convert to public subset
var retServs []discotypes.Server
for _, v := range servs.List {
if search == "" {
retServs = append(retServs, v.Server)
continue
}
score := v.Score(search)
if score < 0 {
continue
}
v.Server.Score = score
retServs = append(retServs, v.Server)
}
if search != "" {
sort.Slice(retServs, func(i, j int) bool {
// lower score is better
return retServs[i].Score < retServs[j].Score
})
}
return &discotypes.Servers{
List: retServs,
}, err
}
type DiscoManager struct {
disco *discovery.Discovery
cancel context.CancelFunc
mu sync.RWMutex
wait sync.WaitGroup
}
func (m *DiscoManager) lock(write bool) {
if write {
m.mu.Lock()
return
}
m.mu.RLock()
}
func (m *DiscoManager) unlock(write bool) {
if write {
m.mu.Unlock()
return
}
m.mu.RUnlock()
}
func (m *DiscoManager) Discovery(write bool) (*discovery.Discovery, func()) {
if write {
m.wait.Wait()
}
m.lock(write)
return m.disco, func() {
m.unlock(write)
}
}
func (m *DiscoManager) Cancel() {
if m.cancel != nil {
m.cancel()
}
m.wait.Wait()
}
func (m *DiscoManager) Startup(ctx context.Context, cb func()) {
ctx, cancel := context.WithCancel(ctx)
m.cancel = cancel
m.wait.Add(1)
go func() {
m.lock(false)
discoCopy, err := m.disco.Copy()
if err != nil {
log.Logger.Warningf("internal error, failed to clone discovery, %v", err)
return
}
m.unlock(false)
// we already log the warning
discoCopy.Servers(ctx) //nolint:errcheck
m.lock(true)
m.disco.UpdateServers(discoCopy)
m.unlock(true)
m.wait.Done()
select {
case <-ctx.Done():
return
default:
if cb == nil {
return
}
cb()
}
}()
}
|