-
Notifications
You must be signed in to change notification settings - Fork 1
/
peers.go
334 lines (307 loc) · 8.06 KB
/
peers.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
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"sync"
"time"
netutil "github.com/shirou/gopsutil/v3/net"
"github.com/blinklabs-io/nview/internal/config"
)
var peersFiltered []string
var checkPeers bool = true
var scrollPeers bool = false
func filterPeers(ctx context.Context) error {
var peers []string
if len(peerStats.RTTresultsSlice) != 0 &&
len(peerStats.RTTresultsSlice) == len(peersFiltered) {
return nil
}
if processMetrics == nil {
return nil // TODO: what to do here
}
cfg := config.GetConfig()
// Get process in/out connections
connections, err := netutil.ConnectionsPidWithContext(
ctx,
"tcp",
processMetrics.Pid,
)
if err != nil {
return err
}
var peersIn []string
var peersOut []string
// Loops each connection, looking for ESTABLISHED
for _, c := range connections {
if c.Status == "ESTABLISHED" {
// If local port == node port, it's incoming (except P2P)
if c.Laddr.Port == cfg.Node.Port {
peersIn = append(
peersIn,
fmt.Sprintf("%s:%d", c.Raddr.IP, c.Raddr.Port),
)
}
// If local port != node port, ekg port, or prometheus port, it's outgoing
if c.Laddr.Port != cfg.Node.Port && c.Laddr.Port != uint32(12788) &&
c.Laddr.Port != cfg.Prometheus.Port {
peersOut = append(
peersOut,
fmt.Sprintf("%s:%d", c.Raddr.IP, c.Raddr.Port),
)
}
}
}
// Skip everything if we have no peers
if len(peersIn) == 0 && len(peersOut) == 0 {
failCount = 0
return nil
}
// Process peersIn
for _, peer := range peersIn {
p := strings.Split(peer, ":")
peerIP := p[0]
peerPORT := p[1]
if strings.HasPrefix(peerIP, "[") { // IPv6
peerIP = strings.TrimPrefix(strings.TrimSuffix(peerIP, "]"), "[")
}
if peerIP == "127.0.0.1" ||
(publicIP != nil && peerIP == publicIP.String() && peerPORT == strconv.FormatUint(uint64(cfg.Node.Port), 10)) {
// Do nothing
continue
} else {
added := false
for i, toCheck := range peers {
checkIP := strings.Split(toCheck, ":")[0]
if checkIP == peerIP {
if p[2] != "i" {
// Remove and re-add as duplex (i+o)
peers = append(peers[:i], peers[i+1:]...)
peers = append(peers, fmt.Sprintf("%s;%s;i+o", peerIP, peerPORT))
added = true
break
}
}
}
if !added {
peers = append(peers, fmt.Sprintf("%s;%s;i", peerIP, peerPORT))
}
}
}
// Process peersOut
for _, peer := range peersOut {
p := strings.Split(peer, ":")
peerIP := p[0]
peerPORT := p[1]
if strings.HasPrefix(peerIP, "[") { // IPv6
peerIP = strings.TrimPrefix(strings.TrimSuffix(peerIP, "]"), "[")
}
if peerIP == "127.0.0.1" ||
(publicIP != nil && peerIP == publicIP.String() && peerPORT == strconv.FormatUint(uint64(cfg.Node.Port), 10)) {
// Do nothing
continue
} else {
added := false
for i, toCheck := range peers {
checkIP := strings.Split(toCheck, ":")[0]
if checkIP == peerIP {
if p[2] != "o" {
// Remove and re-add as duplex (i+o)
peers = append(peers[:i], peers[i+1:]...)
peers = append(peers, fmt.Sprintf("%s;%s;i+o", peerIP, peerPORT))
added = true
break
}
}
}
if !added {
peers = append(peers, fmt.Sprintf("%s;%s;o", peerIP, peerPORT))
}
}
}
// TODO: do this better than just a length check
if len(peers) != len(peersFiltered) {
peersFiltered = peers
}
return nil
}
func pingPeers(ctx context.Context) error {
scrollPeers = false
var granularity int = 68
granularitySmall := granularity / 2
if checkPeers {
// counters, etc.
peerCount := len(peersFiltered)
var peerRTT int
var wg sync.WaitGroup
for _, v := range peersFiltered {
// increment waitgroup counter
wg.Add(1)
// Avoid re-use of v in all go-routines
// https://go.dev/doc/faq#closures_and_goroutines
v := v
go func() {
defer wg.Done()
peerArr := strings.Split(v, ";")
peerIP := peerArr[0]
peerPORT := peerArr[1]
peerDIR := peerArr[2]
// Return early if we've been checked recently
now := time.Now()
expire := now.Add(-600 * time.Second)
existing, ok := peerStats.RTTresultsMap[peerIP]
if ok {
if existing.UpdatedAt.After(expire) && existing.RTT != 0 {
return
}
if existing.Location != "---" {
return
}
}
// Start RTT loop
// for tool in ... return peerRTT
peerRTT = tcpinfoRtt(fmt.Sprintf("%s:%s", peerIP, peerPORT))
if peerRTT != 99999 {
peerStats.RTTSUM = peerStats.RTTSUM + peerRTT
}
// Update counters
if peerRTT < 50 {
peerStats.CNT1 = peerStats.CNT1 + 1
} else if peerRTT < 100 {
peerStats.CNT2 = peerStats.CNT2 + 1
} else if peerRTT < 200 {
peerStats.CNT3 = peerStats.CNT3 + 1
} else if peerRTT < 99999 {
peerStats.CNT4 = peerStats.CNT4 + 1
} else {
peerStats.CNT0 = peerStats.CNT0 + 1
}
peerPort, err := strconv.Atoi(peerPORT)
if err != nil {
peerPort = 0
}
peerLocation := getGeoIP(ctx, peerIP)
peer := &Peer{
IP: peerIP,
Port: peerPort,
Direction: peerDIR,
RTT: peerRTT,
Location: peerLocation,
UpdatedAt: time.Now(),
}
peerStats.RTTresultsMap[peerIP] = peer
peerStats.RTTresultsSlice = append(
peerStats.RTTresultsSlice,
peer,
)
sort.Sort(peerStats.RTTresultsSlice)
}()
wg.Wait()
}
peerCNTreachable := peerCount - peerStats.CNT0
if peerCNTreachable > 0 {
peerStats.RTTAVG = peerStats.RTTSUM / peerCNTreachable
peerStats.PCT1 = float32(
peerStats.CNT1,
) / float32(
peerCNTreachable,
) * 100
peerStats.PCT1items = int(peerStats.PCT1) * granularitySmall / 100
peerStats.PCT2 = float32(
peerStats.CNT2,
) / float32(
peerCNTreachable,
) * 100
peerStats.PCT2items = int(peerStats.PCT2) * granularitySmall / 100
peerStats.PCT3 = float32(
peerStats.CNT3,
) / float32(
peerCNTreachable,
) * 100
peerStats.PCT3items = int(peerStats.PCT3) * granularitySmall / 100
peerStats.PCT4 = float32(
peerStats.CNT4,
) / float32(
peerCNTreachable,
) * 100
peerStats.PCT4items = int(peerStats.PCT4) * granularitySmall / 100
}
if len(peerStats.RTTresultsSlice) != 0 &&
len(peerStats.RTTresultsSlice) >= peerCount {
checkPeers = false
scrollPeers = true
}
}
failCount = 0
return nil
}
func resetPeers() {
peerStats.CNT0 = 0
peerStats.CNT1 = 0
peerStats.CNT2 = 0
peerStats.CNT3 = 0
peerStats.CNT4 = 0
peerStats.RTTSUM = 0
peerStats.RTTresultsSlice = []*Peer{}
for _, peerIP := range peerStats.RTTresultsMap {
peerIP.RTT = 0
}
peersFiltered = []string{}
}
var peerStats PeerStats
type PeerStats struct {
RTTSUM int
RTTAVG int
CNT0 int
CNT1 int
CNT2 int
CNT3 int
CNT4 int
PCT1 float32
PCT2 float32
PCT3 float32
PCT4 float32
PCT1items int
PCT2items int
PCT3items int
PCT4items int
RTTresultsMap peerRTTresultsMap
RTTresultsSlice peerRTTresultsSlice
}
type Peer struct {
Direction string
IP string
RTT int
Port int
Location string
UpdatedAt time.Time
}
type peerRTTresultsMap map[string]*Peer
type peerRTTresultsSlice []*Peer
// Len is part of sort.Interface
func (p peerRTTresultsSlice) Len() int {
return len(p)
}
// Swap is part of sort.Interface
func (p peerRTTresultsSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
// Less is part of sort.Interface and we use RTT as the value to sort by
func (p peerRTTresultsSlice) Less(i, j int) bool {
return p[i].RTT < p[j].RTT
}