-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
370 lines (311 loc) · 7.62 KB
/
main.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
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"flag"
"log"
"math/big"
"net"
"os"
"runtime"
"strings"
"time"
"github.com/influxdata/influxdb-client-go"
)
const (
network = "udp"
disconnectTimeout = time.Second * 15
)
var (
listen = flag.String("listen", ":4590", "UDP address to listen on")
)
type ClientKey struct {
IP [16]byte
Port int
Zone string // IPv6 scoped addressing zone
}
type Client struct {
net.UDPAddr
IsAlive bool
Group string
ClientKey ClientKey
Index uint16
Sector uint64
LastSeen time.Time
}
//type ClientGroup map[ClientKey]*Client
type ClientGroup struct {
Group string
AnonymizedName string
Clients []Client
ActiveCount int
}
var (
udpAddr *net.UDPAddr
conn *net.UDPConn
clientGroups map[string]*ClientGroup
)
var (
networkMetrics NetworkMetrics = &nullNetworkMetrics{}
clientMetrics ClientMetrics = &nullClientMetrics{}
groupMetrics GroupMetrics = &nullGroupMetrics{}
)
func readTinyString(buf *bytes.Buffer) (value string, err error) {
var valueLength uint8
if err = binary.Read(buf, binary.LittleEndian, &valueLength); err != nil {
return
}
valueBytes := make([]byte, valueLength)
var n int
n, err = buf.Read(valueBytes)
if err != nil {
return
}
if n < int(valueLength) {
return
}
value = string(valueBytes)
return
}
type UDPMessage struct {
Envelope []byte
ReceivedFrom *net.UDPAddr
}
func reportGroupClients(group *ClientGroup) {
groupMetrics.GroupClients(group, group.ActiveCount)
}
func reportTotalGroups() {
groupMetrics.TotalGroups(len(clientGroups))
}
func reportTotalClients() {
totalClients := 0
for _, cg := range clientGroups {
totalClients += cg.ActiveCount
}
clientMetrics.TotalClients(totalClients)
}
func getPackets(conn *net.UDPConn, messages chan<- UDPMessage) {
// we only need a single receive buffer:
b := make([]byte, 1500)
for {
// wait for a packet from UDP socket:
var n, addr, err = conn.ReadFromUDP(b)
if err != nil {
log.Print(err)
close(messages)
return
}
// copy the envelope:
envelope := make([]byte, n)
copy(envelope, b[:n])
messages <- UDPMessage{
Envelope: envelope,
ReceivedFrom: addr,
}
}
}
func main() {
flag.Parse()
var err error
var ifxClient influxdb2.Client = nil
ifxUrl := os.Getenv("INFLUX_URL")
if ifxUrl != "" {
ifxToken := os.Getenv("INFLUX_TOKEN")
ifxClient = influxdb2.NewClientWithOptions(
ifxUrl,
ifxToken,
influxdb2.DefaultOptions().
SetBatchSize(2400).
SetPrecision(time.Nanosecond).
SetFlushInterval(1000))
defer ifxClient.Close()
ifxOrg := os.Getenv("INFLUX_ORG")
ifxBucket := os.Getenv("INFLUX_BUCKET")
// create influx metrics reporters:
writer := ifxClient.WriteAPI(ifxOrg, ifxBucket)
defer writer.Close()
networkMetrics = &influxNetworkMetrics{w: writer}
clientMetrics = &influxClientMetrics{w: writer}
groupMetrics = &influxGroupMetrics{w: writer}
}
udpAddr, err = net.ResolveUDPAddr(network, *listen)
if err != nil {
log.Fatal(err)
}
log.Printf("getting ready to listen on %s %s; pass the -listen flag to change", network, udpAddr)
conn, err = net.ListenUDP(network, udpAddr)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
log.Printf("listening on %s %s", network, udpAddr)
udpMessages := make(chan UDPMessage)
for i := 0; i < runtime.NumCPU(); i++ {
go getPackets(conn, udpMessages)
}
clientGroups = make(map[string]*ClientGroup)
secondsTicker := time.Tick(time.Second * 5)
eventloop:
for {
select {
case udpMessage, more := <-udpMessages:
// exit loop if udpMessages channel is closed:
if !more {
log.Print("channel closed\n")
break eventloop
}
// process incoming message:
err = processMessage(udpMessage)
if err != nil {
log.Fatal(err)
}
case seconds := <-secondsTicker:
// expunge any expired clients every 5 seconds:
expireClients(seconds)
}
}
log.Print("main loop done\n")
}
func processMessage(message UDPMessage) (fatalErr error) {
//log.Printf("(%v) received %v bytes\n", addr, len(envelope))
buf := bytes.NewBuffer(message.Envelope)
var header uint16
if err := binary.Read(buf, binary.LittleEndian, &header); err != nil {
log.Print(err)
return
}
// 0x651F = 25887 = "ALTTP" on dialpad (T9)
if header != 25887 {
log.Printf("bad header %04x\n", header)
return
}
var protocol uint8
if err := binary.Read(buf, binary.LittleEndian, &protocol); err != nil {
log.Print(err)
return
}
switch protocol {
case 0x01:
return processProtocol01(message, buf)
case 0x02:
return processProtocol02(message, buf)
case 0x03:
return processProtocol03(message, buf)
default:
log.Printf("unknown protocol 0x%02x\n", protocol)
}
return
}
func expireClients(seconds time.Time) {
// find all client groups with clients to expire in them:
for groupKey, clientGroup := range clientGroups {
// find all clients to be expired:
for i := range clientGroup.Clients {
c := &clientGroup.Clients[i]
if !c.IsAlive {
continue
}
// expunge expired clients:
if c.LastSeen.Add(disconnectTimeout).Before(seconds) {
c.IsAlive = false
clientGroup.ActiveCount--
log.Printf("[group %s] (%v) forget client, clients=%d\n", groupKey, c, clientGroup.ActiveCount)
reportTotalClients()
}
}
// remove the client group if no more clients left within:
if clientGroup.ActiveCount == 0 {
delete(clientGroups, groupKey)
log.Printf("[group %s] forget group\n", groupKey)
reportTotalGroups()
}
reportGroupClients(clientGroup)
}
}
func calcGroupKey(group string) string {
groupKey := strings.Trim(group, " \t\r\n+='\",.<>[]{}()*&^%$#@!~`?|\\;:/")
groupKey = strings.ToLower(groupKey)
return groupKey
}
func generateAnonymizedName() string {
max36 := big.NewInt(36)
b := make([]byte, 20)
for i := 0; i < 20; i++ {
n, _ := rand.Int(rand.Reader, max36)
r := n.Int64()
if r < 10 {
b[i] = byte('0') + byte(r)
} else {
b[i] = byte('a') + byte(r - 10)
}
}
return string(b)
}
func findGroupOrCreate(groupKey string) *ClientGroup {
clientGroup, ok := clientGroups[groupKey]
if !ok {
clientGroup = &ClientGroup{
Group: groupKey,
AnonymizedName: generateAnonymizedName(),
Clients: make([]Client, 0, 8),
}
clientGroups[groupKey] = clientGroup
log.Printf("[group %s] new group\n", groupKey)
reportGroupClients(clientGroup)
reportTotalGroups()
}
return clientGroup
}
func findClientOrCreate(clientGroup *ClientGroup, clientKey ClientKey, addr *net.UDPAddr, group string, groupKey string) (client *Client, ci int) {
// Find client in Clients array by ClientKey
// Find first free slot to reuse
// Find total count of active clients
ci = -1
free := -1
var i int
activeCount := 0
for i = range clientGroup.Clients {
c := &clientGroup.Clients[i]
if !c.IsAlive {
if free == -1 {
free = i
}
continue
}
activeCount++
if c.ClientKey == clientKey {
client = c
ci = i
}
}
// update ActiveCount:
clientGroup.ActiveCount = activeCount
if client != nil {
// update time last seen:
client.LastSeen = time.Now()
return
}
// No free slot?
if free == -1 {
// Extend Clients array:
clientGroup.Clients = append(clientGroup.Clients, Client{})
free = len(clientGroup.Clients) - 1
}
ci = free
client = &clientGroup.Clients[free]
// add this client to set of clients:
*client = Client{
UDPAddr: *addr,
IsAlive: true,
Group: group,
ClientKey: clientKey,
Index: uint16(free),
LastSeen: time.Now(),
}
clientGroup.ActiveCount++
log.Printf("[group %s] (%v) new client, clients=%d\n", groupKey, client, clientGroup.ActiveCount)
reportGroupClients(clientGroup)
reportTotalClients()
return
}