forked from safing/spn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.go
268 lines (236 loc) · 6.65 KB
/
navigation.go
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
package captain
import (
"context"
"errors"
"fmt"
"time"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portbase/notifications"
"github.com/safing/portmaster/netenv"
"github.com/safing/portmaster/network/netutils"
"github.com/safing/spn/access"
"github.com/safing/spn/docks"
"github.com/safing/spn/hub"
"github.com/safing/spn/navigator"
"github.com/safing/spn/terminal"
)
func homeHubManager(ctx context.Context) (err error) {
defer ready.UnSet()
defer netenv.ConnectedToSPN.UnSet()
managing:
for {
// Check if we are online enough for connecting.
switch netenv.GetOnlineStatus() {
case netenv.StatusOffline,
netenv.StatusLimited:
select {
case <-ctx.Done():
return nil
case <-time.After(1 * time.Second):
continue managing
}
}
home, homeTerminal := navigator.Main.GetHome()
if home == nil || homeTerminal == nil || homeTerminal.IsAbandoned() {
if ready.SetToIf(true, false) {
netenv.ConnectedToSPN.UnSet()
log.Info("spn/captain: client not ready")
}
resetSPNStatus(StatusConnecting)
err = establishHomeHub(ctx)
if err != nil {
log.Warningf("failed to establish connection to home hub: %s", err)
notifications.NotifyWarn(
"spn:home-hub-failure",
"SPN Failed to Connect",
fmt.Sprintf("Failed to connect to a home hub: %s. The Portmaster will retry to connect automatically.", err),
spnTestPhaseStatusLinkButton,
spnSettingsButton,
).AttachToModule(module)
resetSPNStatus(StatusFailed)
select {
case <-ctx.Done():
return nil
case <-time.After(5 * time.Second):
}
continue managing
}
// success!
home, homeTerminal := navigator.Main.GetHome()
notifications.NotifyInfo(
"spn:connected-to-home-hub",
"Connected to SPN",
fmt.Sprintf("You are connected to the SPN at %s. This notification is persistent for awareness.", homeTerminal.RemoteAddr()),
spnTestPhaseStatusLinkButton,
spnSettingsButton,
).AttachToModule(module)
ready.Set()
netenv.ConnectedToSPN.Set()
// Update SPN Status with connection information.
func() {
// Lock for updating values.
spnStatus.Lock()
defer spnStatus.Unlock()
// Fill connection status data.
spnStatus.Status = StatusConnected
spnStatus.HomeHubID = home.Hub.ID
connectedIP, err := netutils.IPFromAddr(homeTerminal.RemoteAddr())
if err != nil {
spnStatus.ConnectedIP = homeTerminal.RemoteAddr().String()
} else {
spnStatus.ConnectedIP = connectedIP.String()
}
spnStatus.ConnectedTransport = homeTerminal.Transport().String()
now := time.Now()
spnStatus.ConnectedSince = &now
// Push new status.
pushSPNStatusUpdate()
}()
log.Infof("spn/captain: established new home %s", home.Hub)
log.Info("spn/captain: client is ready")
}
// Check again after a short break.
select {
case <-ctx.Done():
return nil
case <-time.After(1 * time.Second):
}
}
return nil
}
func establishHomeHub(ctx context.Context) error {
// Get own IP.
locations, ok := netenv.GetInternetLocation()
if !ok {
return errors.New("failed to locate own device")
}
log.Debugf(
"spn/captain: looking for new home hub near %s and %s",
locations.BestV4(),
locations.BestV6(),
)
// Find nearby hubs.
findCandidates:
candidates, err := navigator.Main.FindNearestHubs(
locations.BestV4().LocationOrNil(),
locations.BestV6().LocationOrNil(),
nil, navigator.HomeHub, 10,
)
if err != nil {
if errors.Is(err, navigator.ErrEmptyMap) {
// bootstrap to the network!
err := bootstrapWithUpdates()
if err != nil {
return err
}
goto findCandidates
}
return fmt.Errorf("failed to find nearby hubs: %s", err)
}
// Try connecting to a hub.
var tries int
var candidate *hub.Hub
for tries, candidate = range candidates {
err = connectToHomeHub(ctx, candidate)
if err != nil {
if errors.Is(err, terminal.ErrStopping) {
return err
}
log.Debugf("spn/captain: failed to connect to %s as new home: %s", candidate, err)
} else {
log.Infof("spn/captain: established connection to %s as new home with %d failed tries", candidate, tries)
return nil
}
}
if err != nil {
return fmt.Errorf("failed to connect to a new home hub - tried %d hubs: %s", tries+1, err)
}
return fmt.Errorf("no home hub candidates available")
}
func connectToHomeHub(ctx context.Context, dst *hub.Hub) error {
// Set and clean up exceptions.
setExceptions(dst.Info.IPv4, dst.Info.IPv6)
defer setExceptions(nil, nil)
// Connect to hub.
crane, err := EstablishCrane(dst)
if err != nil {
return err
}
// Cleanup connection in case of failure.
var success bool
defer func() {
if !success {
crane.Stop(nil)
}
}()
// Query all gossip msgs on first connection.
gossipQuery, tErr := NewGossipQueryOp(crane.Controller)
if tErr != nil {
log.Warningf("spn/captain: failed to start initial gossip query: %s", tErr)
}
// Wait for gossip query to complete.
select {
case <-gossipQuery.ctx.Done():
case <-ctx.Done():
}
// Create communication terminal.
homeTerminal, initData, tErr := docks.NewLocalCraneTerminal(crane, nil, &terminal.TerminalOpts{}, nil)
if tErr != nil {
return tErr.Wrap("failed to create home terminal")
}
tErr = crane.EstablishNewTerminal(homeTerminal, initData)
if tErr != nil {
return tErr.Wrap("failed to connect home terminal")
}
// Authenticate to home hub.
authOp, tErr := access.AuthorizeToTerminal(homeTerminal)
if tErr != nil {
return tErr.Wrap("failed to authorize")
}
select {
case tErr := <-authOp.Ended:
if !tErr.Is(terminal.ErrExplicitAck) {
return tErr.Wrap("failed to authenticate to")
}
case <-time.After(3 * time.Second):
return terminal.ErrTimeout.With("timed out waiting for auth to complete")
case <-ctx.Done():
return terminal.ErrStopping
}
// Set new home on map.
ok := navigator.Main.SetHome(dst.ID, homeTerminal)
if !ok {
return fmt.Errorf("failed to set home hub on map")
}
success = true
return nil
}
func optimizeNetwork(ctx context.Context, task *modules.Task) error {
if publicIdentity == nil {
return nil
}
optimize:
newDst, err := navigator.Main.Optimize(nil)
if err != nil {
if errors.Is(err, navigator.ErrEmptyMap) {
// bootstrap to the network!
err := bootstrapWithUpdates()
if err != nil {
return err
}
goto optimize
}
return err
}
if newDst != nil {
log.Infof("spn/captain: network optimization suggests new connection to %s", newDst)
_, err := EstablishPublicLane(newDst)
if err != nil {
log.Warningf("spn/captain: failed to establish public lane to %s: %s", newDst, err)
}
} else {
log.Info("spn/captain: network optimization suggests no further action")
}
return nil
}