-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
396 lines (352 loc) · 10.7 KB
/
run.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"sync"
"time"
"github.com/imup-io/client/connectivity"
"github.com/imup-io/client/speedtesting"
"github.com/imup-io/client/util"
log "golang.org/x/exp/slog"
)
func run(ctx context.Context, shutdown chan os.Signal) error {
imup := newApp()
log.Info("Starting Client", "Version", ClientVersion)
imup.Errors = NewErrMap(imup.cfg.HostID())
log.Info("imup setup", "client", fmt.Sprintf("imup: %+v", imup))
log.Info("imup config", "config", fmt.Sprintf("config: %+v", imup.cfg))
// define a context with cancel to coordinate shutdown behavior
cctx, cancel := context.WithCancel(ctx)
// sendDataWorker listens for imup data
go sendDataWorker(cctx, imup.ChannelImupData)
// check for and send data from local user cache
if cachedJobs, ok := fromCacheDir(); ok {
for _, job := range cachedJobs {
imup.ChannelImupData <- job
}
clearCache()
}
// ======================================================================
// Refresh Public IP Address
//
// refresh public ip address every 1 minute if client has a defined allow or block list
go func() {
ticker := time.NewTicker((1 * time.Minute))
defer ticker.Stop()
for {
// only refresh a clients public ip address if configured to allow/block specific ips
if len(imup.cfg.AllowedIPs()) > 0 || len(imup.cfg.BlockedIPs()) > 0 {
imup.cfg.RefreshPublicIP()
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
return
}
}
}()
// ======================================================================
// Authorization
//
// check to see if client is authorized for realtime features
go func() {
ticker := time.NewTicker(30 * time.Minute)
defer ticker.Stop()
for {
ar := &authRequest{Key: imup.cfg.APIKey(), Email: imup.cfg.EmailAddress()}
b, err := json.Marshal(ar)
if err != nil {
log.Error("failed to marshal auth request", "error", err)
} else if err := imup.authorized(cctx, bytes.NewBuffer(b), imup.cfg.RealtimeAuth()); err != nil {
log.Error("failed to check client authorization", "error", err)
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
return
}
}
}()
// ======================================================================
// Realtime
//
// These functions should run on their own goroutines so
// as not to block each other
// remote configuration reload
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for {
ticker := time.NewTicker(time.Duration(1 * time.Hour))
defer ticker.Stop()
for {
if imup.cfg.Realtime() {
// when api sends a new config, reload it
if err := imup.remoteConfigReload(cctx); err != nil {
log.Error("failed to reload config", "error", err)
imup.Errors.write("RemoteConfigReload", err)
} else {
imup.Errors.reportErrors("RemoteConfigReload")
}
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
return
}
}
}
}()
// liveness checkin
wg.Add(1)
go func() {
defer wg.Done()
for {
ticker := time.NewTicker(time.Duration(10 * time.Second))
defer ticker.Stop()
for {
if imup.cfg.Realtime() {
// liveness checkin
if err := imup.sendClientHealthy(cctx); err != nil {
log.Error("failed liveness checkin", "error", err)
imup.Errors.write("SendClientHealthy", err)
} else {
imup.Errors.reportErrors("SendClientHealthy")
}
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
return
}
}
}
}()
// on demand speed tests
wg.Add(1)
go func() {
defer wg.Done()
for {
ticker := time.NewTicker(time.Duration(10 * time.Second))
defer ticker.Stop()
for {
if imup.cfg.Realtime() {
if ok, err := imup.shouldRunSpeedtest(cctx); err != nil {
log.Error("failed on-demand speed test check", "error", err)
imup.Errors.write("ShouldRunSpeedtest", err)
} else if ok {
// post on demand speed test status
if err := imup.postSpeedTestRealtimeStatus(cctx, "running"); err != nil {
log.Error("failed to post realtime speedtest", "error", err)
imup.Errors.write("PostSpeedTestStatus", err)
}
// run an on demand speed test
opts := speedtesting.Options{
Insecure: imup.cfg.InsecureSpeedTests(),
OnDemand: true,
ClientVersion: ClientVersion,
}
if result, err := speedtesting.Run(cctx, opts); err != nil {
// async post on demand speed test status
if err := imup.postSpeedTestRealtimeStatus(ctx, "error"); err != nil {
log.Error("failed to update on-demand speed test status", "error", err)
}
log.Error("failed to run on-demand speed test", "error", err)
imup.Errors.write("RunSpeedTestOnce", err)
} else {
// async post on demand speed test result
go func() {
if err := imup.postSpeedTestRealtimeResults(ctx, "complete", result); err != nil {
log.Error("failed to update on-demand speed test status", "error", err)
}
}()
imup.Errors.reportErrors("ShouldRunSpeedtest")
imup.Errors.reportErrors("PostSpeedTestStatus")
imup.Errors.reportErrors("RunSpeedTestOnce")
// enqueue a job
imup.ChannelImupData <- sendDataJob{
IMUPAddress: imup.cfg.PostSpeedTestData(),
IMUPData: &imupData{
Email: imup.cfg.EmailAddress(),
ID: imup.cfg.HostID(),
Key: imup.cfg.APIKey(),
GroupID: imup.cfg.GroupID(),
IMUPData: result,
},
}
}
}
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
return
}
}
}
}()
// ======================================================================
// Random Speed Testing
//
// collects speed test data using the ndt7 protocol
// data is collected pseudo randomly, every 4 hours
go func() {
ticker := time.NewTicker(speedTestInterval())
defer ticker.Stop()
for {
if imup.cfg.SpeedTests() {
monitoring := util.IPMonitored(imup.cfg.PublicIP(), imup.cfg.AllowedIPs(), imup.cfg.BlockedIPs())
// extra check if ip based speed testing is configured
if monitoring {
opts := speedtesting.Options{
Insecure: imup.cfg.InsecureSpeedTests(),
OnDemand: false,
ClientVersion: ClientVersion,
}
if result, err := speedtesting.Run(cctx, opts); err != nil {
log.Error("failed to run speed test", "error", err)
imup.Errors.write("CollectSpeedTestData", err)
} else {
go imup.Errors.reportErrors("CollectSpeedTestData")
// enqueue a job
imup.ChannelImupData <- sendDataJob{
IMUPAddress: imup.cfg.PostSpeedTestData(),
IMUPData: &imupData{
Email: imup.cfg.EmailAddress(),
ID: imup.cfg.HostID(),
Key: imup.cfg.APIKey(),
GroupID: imup.cfg.GroupID(),
IMUPData: result,
},
}
}
}
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
return
}
}
}()
// ======================================================================
// Connectivity Testing
//
// using either ICMP or TCP setup run connectivity tests
// on regular intervals, the default is continuous polling
// with statistics calculated for each minute
// ensure that the client sends its first point of data to the api
// after it runs a single connectivity test
firstTest := true
wg.Add(1)
data := make([]connectivity.Statistics, 0, 30)
var collector connectivity.StatCollector
go func() {
defer wg.Done()
// initialize a collector
if imup.cfg.PingTests() {
collector = connectivity.NewPingCollector(connectivity.Options{
AddressInternal: imup.cfg.InternalPingAddress(),
ClientVersion: ClientVersion,
Count: imup.cfg.PingRequestsCount(),
Debug: imup.cfg.Verbosity() == log.LevelDebug,
Delay: time.Duration(imup.cfg.PingDelayMilli()) * time.Millisecond,
Interval: time.Duration(imup.cfg.PingIntervalSeconds()) * time.Second,
Timeout: time.Duration(imup.cfg.PingIntervalSeconds()) * time.Second,
})
} else {
collector = connectivity.NewDialerCollector(connectivity.Options{
ClientVersion: ClientVersion,
Count: imup.cfg.ConnRequestsCount(),
Debug: imup.cfg.Verbosity() == log.LevelDebug,
Delay: time.Duration(imup.cfg.ConnDelayMilli()) * time.Millisecond,
Interval: time.Duration(imup.cfg.ConnIntervalSeconds()) * time.Second,
Timeout: time.Duration(imup.cfg.ConnIntervalSeconds()) * time.Second,
})
}
ticker := time.NewTicker(collector.Interval())
defer ticker.Stop()
for {
monitoring := util.IPMonitored(imup.cfg.PublicIP(), imup.cfg.AllowedIPs(), imup.cfg.BlockedIPs())
if monitoring {
collected := collector.Collect(cctx, imup.cfg.PingAddresses())
data = append(data, collected...)
log.Debug("data points collected", "count", len(data))
if imup.cfg.StoreJobsOnDisk() {
sc, dt := collector.DetectDowntime(data)
toUserCache(sendDataJob{
IMUPAddress: imup.cfg.PostConnectionData(),
IMUPData: imupData{
Downtime: dt,
StatusChanged: sc,
Email: imup.cfg.EmailAddress(),
ID: imup.cfg.HostID(),
Key: imup.cfg.APIKey(),
GroupID: imup.cfg.GroupID(),
IMUPData: collected,
}})
}
}
if len(data) >= imup.cfg.IMUPDataLen() || firstTest {
firstTest = false
sc, dt := collector.DetectDowntime(data)
// enqueue a job
imup.ChannelImupData <- sendDataJob{
IMUPAddress: imup.cfg.PostConnectionData(),
IMUPData: imupData{
Downtime: dt,
StatusChanged: sc,
Email: imup.cfg.EmailAddress(),
ID: imup.cfg.HostID(),
Key: imup.cfg.APIKey(),
GroupID: imup.cfg.GroupID(),
IMUPData: data,
},
}
// reset connData slice
data = nil
if imup.cfg.StoreJobsOnDisk() {
clearCache()
}
}
select {
case <-ticker.C:
continue
case <-cctx.Done():
log.Debug("data points to persist?", "data > 0", len(data) > 0)
if len(data) > 0 {
sc, dt := collector.DetectDowntime(data)
log.Debug("persisting pending conn data")
toUserCache(sendDataJob{
IMUPAddress: imup.cfg.PostConnectionData(),
IMUPData: imupData{
Downtime: dt,
StatusChanged: sc,
Email: imup.cfg.EmailAddress(),
ID: imup.cfg.HostID(),
Key: imup.cfg.APIKey(),
GroupID: imup.cfg.GroupID(),
IMUPData: data,
}})
}
}
return
}
}()
sig := <-shutdown
log.Info("shutdown started", "signal", sig)
cancel()
wg.Wait()
defer log.Info("shutdown completed", "signal", sig)
return nil
}