-
Notifications
You must be signed in to change notification settings - Fork 2
/
imup.go
172 lines (145 loc) · 4.2 KB
/
imup.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"sync"
"time"
"github.com/hashicorp/go-retryablehttp"
"github.com/imup-io/client/config"
log "golang.org/x/exp/slog"
)
type sendDataJob struct {
IMUPAddress string
IMUPData any
}
type imupData struct {
Downtime int `json:"downtime,omitempty"`
StatusChanged bool `json:"statusChanged"`
Email string `json:"email,omitempty"`
ID string `json:"hostId,omitempty"`
Key string `json:"apiKey,omitempty"`
GroupID string `json:"group_id,omitempty"`
IMUPData any `json:"data,omitempty"`
}
type authRequest struct {
Key string `json:"apiKey,omitempty"`
Email string `json:"email,omitempty"`
}
type pingStats struct {
PingAddress string `json:"pingAddress,omitempty"`
Success bool `json:"success,omitempty"`
PacketsRecv int `json:"packetsRecv,omitempty"`
PacketsSent int `json:"packetsSent,omitempty"`
PacketLoss float64 `json:"packetLoss,omitempty"`
MinRtt time.Duration `json:"minRtt,omitempty"`
MaxRtt time.Duration `json:"maxRtt,omitempty"`
AvgRtt time.Duration `json:"avgRtt,omitempty"`
StdDevRtt time.Duration `json:"stdDevRtt,omitempty"`
TimeStamp int64 `json:"timestamp,omitempty"`
ClientVersion string `json:"clientVersion,omitempty"`
OS string `json:"operatingSystem,omitempty"`
EndpointType string `json:"endpointType,omitempty"`
SuccessInternal bool `json:"successInternal,omitempty"`
}
type imup struct {
cfg config.Reloadable
SpeedTestLock sync.Mutex
ChannelImupData chan sendDataJob
PingAddressesAvoid map[string]bool
Errors *ErrMap
}
func newApp() *imup {
cfg, err := config.New()
if err != nil {
log.Error("error", err)
os.Exit(1)
}
imup := &imup{
PingAddressesAvoid: map[string]bool{},
cfg: cfg,
}
// make a channel with a capacity of 300.
imup.ChannelImupData = make(chan sendDataJob, 300)
// on startup get a clients public ip address
imup.cfg.RefreshPublicIP()
return imup
}
func sendImupData(ctx context.Context, job sendDataJob) {
b, err := json.Marshal(job.IMUPData)
if err != nil {
log.Error("error", err)
return
}
req, err := retryablehttp.NewRequest("POST", job.IMUPAddress, bytes.NewBuffer(b))
if err != nil {
log.Error("error", err)
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", "application/json")
client := retryablehttp.NewClient()
client.Backoff = exactJitterBackoff
// 50000 should be 15-30 days with this config
client.RetryMax = 50000
client.RetryWaitMin = time.Duration(30) * time.Second
client.RetryWaitMax = time.Duration(60) * time.Second
client.Logger = log.New(log.Default().Handler())
if _, err := client.Do(req); err != nil {
if err == context.Canceled {
// shutdown in progress
toUserCache(job)
}
log.Error("error", err)
}
}
func sendDataWorker(ctx context.Context, c chan sendDataJob) {
for {
select {
case <-ctx.Done():
if len(c) > 0 {
log.Info("shutdown detected, persisting queued data")
drain(c)
}
return
case job := <-c:
sendImupData(ctx, job)
}
}
}
func (i *imup) reloadConfig(data []byte) {
if cfg, err := config.Reload(data); err != nil {
log.Info("cannot reload config", "error", err)
} else {
i.cfg = cfg
}
}
func (i *imup) authorized(ctx context.Context, b *bytes.Buffer, addr string) error {
req, err := retryablehttp.NewRequest("POST", addr, b)
req = req.WithContext(ctx)
if err != nil {
return fmt.Errorf("NewRequest: %v", err)
}
req.Header.Set("Content-Type", "application/json")
client := retryablehttp.NewClient()
client.Backoff = exactJitterBackoff
client.RetryMax = 50_000
client.RetryWaitMin = time.Duration(30) * time.Second
client.RetryWaitMax = time.Duration(60) * time.Second
client.Logger = log.New(log.Default().Handler())
if resp, err := client.Do(req); err != nil {
if err == context.Canceled {
return nil
}
return fmt.Errorf("addr: %s, client.Do: %s", addr, err)
} else {
if resp.StatusCode == http.StatusOK {
i.cfg.EnableRealtime()
} else {
i.cfg.DisableRealtime()
}
}
return nil
}