/
bkgcaster_client.go
509 lines (447 loc) · 13 KB
/
bkgcaster_client.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package ntrip
// ----------------------------------------------------------------------------
// Functions for the BKG NtripCaster only
// ----------------------------------------------------------------------------
import (
"bufio"
"fmt"
"io"
"net/http"
"net/http/httputil"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
var (
// Pattern for connection duration string, e.g. '12 days, 12 hours, 56 minutes and 5 seconds'
connectionTimePattern = regexp.MustCompile(`((\d+) days, )?((\d+) hours, )?((\d+) minutes)?( and )?((\d+) seconds)?`)
)
// CasterStats contains general statistics like number of clients, sources etc.
type CasterStats struct {
Admins int `json:"admins"`
Sources int `json:"sources"`
Listeners int `json:"listeners"`
Uptime time.Duration `json:"uptime"`
LastResync time.Time `json:"last_resync"`
// Following fields since last resync
KBytesRead int `json:"KBytes_recv"`
KBytesWritten int `json:"KBytes_sent"`
}
// CasterListener contains the information about an connected listener/client like IP, user agent etc.
type CasterListener struct {
Host string `json:"host"`
IP string `json:"ip"`
User string `json:"username"`
MP string `json:"mountpoint"`
ID int `json:"id"`
ConnectedFor time.Duration `json:"connected_for"`
BytesWritten int `json:"bytes_written"`
Errors int `json:"errors"`
UserAgent string `json:"user_agent"`
Type string `json:"type"`
}
// CasterSource contains information about an active data stream.
type CasterSource struct {
Host, IP string
MP string
ID int
Agent string
ConnectedFor time.Duration
ConnectionTime time.Time
Clients int
ClientConnections int
KBytesRead, KBytesWritten int
}
// CasterConn defines a clients connection to the caster.
type CasterConn struct {
MP string
Type string
ID int
UserAgent string
IP string
User string
ConnectedFor time.Duration
}
func (c *Client) sendRequest() (io.ReadCloser, error) {
req, err := http.NewRequest("GET", c.URL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.Useragent)
req.Header.Add("Ntrip-Version", "Ntrip/2.0")
req.SetBasicAuth(c.Username, c.Password)
//req.Header.Set("Accept-Encoding", `gzip;q=0,bzip2;q=0,compress;q=0,deflate;q=0`)
req.Header.Set("Accept", `text/html, text/plain, text/*`)
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Connection", "close")
// fmt.Println("The request header is:")
// re, _ := httputil.DumpRequest(req, false)
// fmt.Println(string(re))
resp, err := c.Do(req)
if err != nil {
return nil, err
}
//ntripcaster := resp.Header.Get("Server")
//fmt.Printf("Server: %s\n", ntripcaster)
if resp.StatusCode != http.StatusOK { // / if resp.Status != "200 OK"
respi, _ := httputil.DumpResponse(resp, false)
fmt.Print(string(respi))
resp.Body.Close()
return nil, fmt.Errorf("HTTP Request failed: %d (%s)", resp.StatusCode, resp.Status)
}
return resp.Body, nil
}
// GetListeners fetches the currently connected listeners.
func (c *Client) GetListeners() ([]CasterListener, error) { // pruefen []*listener
c.URL.Path = "admin"
c.URL.RawQuery = "mode=listeners"
reader, err := c.sendRequest()
if err != nil {
return nil, err
}
defer reader.Close()
listeners := make([]CasterListener, 0, 1000)
li := CasterListener{}
scanner := bufio.NewScanner(reader)
ln := ""
for scanner.Scan() {
ln = scanner.Text()
if strings.HasPrefix(ln, "<li>") {
ln = strings.TrimPrefix(ln, "<li>[")
ln = strings.TrimSuffix(ln, "]<br>")
li = CasterListener{}
fields := strings.Split(ln, "] [")
for _, v := range fields {
hlp := strings.Split(v, ":")
// fixed in caster version 2.0.3?
if len(hlp) == 1 {
if strings.HasPrefix(hlp[0], "Mountpoint") {
li.MP = strings.TrimPrefix(hlp[0], "Mountpoint /")
continue
}
}
key, val := hlp[0], strings.TrimSpace(hlp[1])
switch key {
case "Host":
li.Host = val
case "IP":
li.IP = val
case "User":
li.User = val
case "Mountpoint":
li.MP = strings.TrimPrefix(val, "/")
case "Id":
if i, err := strconv.Atoi(val); err == nil {
li.ID = i
} else {
return nil, err
}
case "Connected for":
if d, err := parseDuration(val); err == nil {
li.ConnectedFor = d
} else {
return nil, err
}
case "Bytes written":
if i, err := strconv.Atoi(val); err == nil {
li.BytesWritten = i
} else {
return nil, err
}
case "Errors":
if i, err := strconv.Atoi(val); err == nil {
li.Errors = i
} else {
return nil, err
}
case "User agent":
li.UserAgent = val
case "Type":
li.Type = val // strings.TrimSuffix(val, "]")
default:
return nil, fmt.Errorf("unknown field: %q", key)
}
}
listeners = append(listeners, li)
}
}
sort.Slice(listeners, func(i, j int) bool {
return listeners[i].User < listeners[j].User
})
return listeners, nil
}
// GetSources fetches the current sources.
func (c *Client) GetSources() ([]CasterSource, error) {
c.URL.Path = "admin"
c.URL.RawQuery = "mode=sources"
reader, err := c.sendRequest()
if err != nil {
return nil, err
}
defer reader.Close()
sources := make([]CasterSource, 0, 200)
headline := make([]string, 12)
src := CasterSource{}
scanner := bufio.NewScanner(reader)
ln := ""
line:
for scanner.Scan() {
ln = scanner.Text()
if strings.HasPrefix(ln, "<tr>") {
ln = strings.TrimPrefix(ln, "<tr><td>")
ln = strings.TrimSuffix(ln, "</td></tr>")
src = CasterSource{}
fields := strings.Split(ln, "</td><td>")
for i, val := range fields {
val = strings.TrimSpace(val)
if val == "Mountpoint" { // Headline
headline = fields
continue line
}
switch headline[i] {
case "Mountpoint":
src.MP = strings.TrimPrefix(val, "/")
case "Host":
src.Host = val
case "IP":
src.IP = val
case "Id":
if i, err := strconv.Atoi(val); err == nil {
src.ID = i
} else {
return nil, err
}
case "Connected for":
if d, err := parseDuration(val); err == nil {
src.ConnectedFor = d
} else {
return nil, err
}
case "Time of connect":
if t, err := time.Parse("02/Jan/2006:15:04:05", val); err == nil {
src.ConnectionTime = t
} else {
return nil, fmt.Errorf("could not parse time of connect: %v", err)
}
case "KBytes read":
if i, err := strconv.Atoi(val); err == nil {
src.KBytesRead = i
} else {
return nil, err
}
case "KBytes written":
if i, err := strconv.Atoi(val); err == nil {
src.KBytesWritten = i
} else {
return nil, err
}
case "Clients":
if i, err := strconv.Atoi(val); err == nil {
src.Clients = i
} else {
return nil, err
}
case "Client connections":
if i, err := strconv.Atoi(val); err == nil {
src.ClientConnections = i
} else {
return nil, err
}
case "Source Agent":
src.Agent = val
default:
return nil, fmt.Errorf("unknown field %q", headline[i])
}
}
sources = append(sources, src)
}
}
sort.Slice(sources, func(i, j int) bool {
return sources[i].MP < sources[j].MP
})
return sources, nil
}
// GetConnections fetches the current client connections.
func (c *Client) GetConnections() ([]CasterConn, error) {
c.URL.Path = "admin"
c.URL.RawQuery = "mode=connections"
reader, err := c.sendRequest()
if err != nil {
return nil, err
}
defer reader.Close()
conns := make([]CasterConn, 0, 1000)
header := [7]string{"Mountpoint", "Type", "Id", "Agent", "IP", "User", "Connected for"}
// <a href="/admin?mode=kick&argument=915">915</a>
idRegEx := regexp.MustCompile(`<a href="\/admin\?mode=kick&argument=\d+">(\d+)<\/a>`)
conn := CasterConn{}
scanner := bufio.NewScanner(reader)
ln := ""
line:
for scanner.Scan() {
ln = scanner.Text()
if strings.HasPrefix(ln, "<tr>") {
ln = strings.TrimPrefix(ln, "<tr><td>")
ln = strings.TrimSuffix(ln, "</td></tr>")
conn = CasterConn{} // reset
fields := strings.Split(ln, "</td><td>")
for i, val := range fields {
if val == "Mountpoint" { // Headline
continue line
}
switch header[i] {
case "Mountpoint":
conn.MP = strings.TrimPrefix(val, "/")
case "Type":
conn.Type = val
case "IP":
conn.IP = val
case "User":
conn.User = val
case "Id":
res := idRegEx.FindStringSubmatch(val)
if len(res) > 0 {
if i, err := strconv.Atoi(res[1]); err == nil {
conn.ID = i
} else {
return nil, err
}
} else {
return nil, fmt.Errorf("regex for \"Id\" did not match")
}
case "Connected for":
if d, err := parseDuration(val); err == nil {
conn.ConnectedFor = d
} else {
return nil, err
}
case "Agent":
conn.UserAgent = val
default:
return nil, fmt.Errorf("unknown key %q", header[i])
}
}
conns = append(conns, conn)
}
}
sort.Slice(conns, func(i, j int) bool {
return conns[i].MP < conns[j].MP
})
return conns, nil
}
// KickConnection stops an active client connection.
func (c *Client) KickConnection(id int) error {
if id < 1 {
return fmt.Errorf("invalid id: %d", id) // kann das ueberhaupt vorkommen?
}
c.URL.Path = "admin"
c.URL.RawQuery = "mode=kick&argument=" + strconv.Itoa(id)
reader, err := c.sendRequest()
if err != nil {
return err
}
defer reader.Close()
return nil
}
// GetStats requests some general statistics from the caster like number of clients, connections etc.
func (c *Client) GetStats() (*CasterStats, error) {
c.URL.Path = "admin"
c.URL.RawQuery = "mode=stats"
stats := &CasterStats{}
reader, err := c.sendRequest()
if err != nil {
return stats, err
}
defer reader.Close()
scanner := bufio.NewScanner(reader)
ln := ""
isBody := false
i := 0
if err != nil {
return nil, fmt.Errorf("could not parse the number of sources in line: %s", ln)
}
for scanner.Scan() {
ln = scanner.Text()
if !isBody && strings.HasPrefix(ln, "<body>") {
isBody = true
continue
}
if !isBody {
continue
}
if strings.HasPrefix(ln, "<div") {
continue
}
ln = strings.TrimSuffix(ln, "<br>")
fields := strings.Split(ln, ":")
if len(fields) < 2 {
//fmt.Printf(">>>>>>>>>>>>>>>>>> %s\n", ln)
continue
}
key, val := fields[0], strings.TrimSpace(fields[1])
if key == "Admins" {
i, err = strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("could not parse the number of Admins in line: %s", ln)
}
stats.Admins = i
} else if key == "Sources" {
i, err = strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("could not parse the number of Sources in line: %s", ln)
}
stats.Sources = i
} else if key == "Listeners" {
i, err = strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("could not parse the number of Listeners in line: %s", ln)
}
stats.Listeners = i
} else if strings.Contains(key, "uptime") {
if d, err := parseDuration(val); err == nil {
stats.Uptime = d
} else {
return nil, err
}
} else if strings.Contains(key, "last resync") {
if t, err := time.Parse("02/Jan/2006:150405", val+":"+fields[2]+fields[3]+fields[4]); err == nil {
stats.LastResync = t
} else {
return nil, fmt.Errorf("parsing time of last resync: %v", err)
}
} else if strings.Contains(key, "KBytes read") {
if i, err := strconv.Atoi(val); err == nil {
stats.KBytesRead = i
} else {
return nil, err
}
} else if strings.Contains(key, "KBytes written") {
if i, err := strconv.Atoi(val); err == nil {
stats.KBytesWritten = i
} else {
return nil, err
}
}
}
return stats, nil
}
func parseDuration(dur string) (time.Duration, error) {
res := connectionTimePattern.FindStringSubmatch(dur)
for k, v := range res {
//fmt.Printf("%d. %s\n", k, v)
if v == "" {
res[k] = "0"
}
}
if len(res) == 10 {
days, _ := strconv.Atoi(res[2])
hours, _ := strconv.Atoi(res[4])
min, _ := strconv.Atoi(res[6])
secs, _ := strconv.Atoi(res[9])
return time.Duration(days)*time.Hour*24 + time.Duration(hours)*time.Hour +
time.Duration(min)*time.Minute + time.Duration(secs)*time.Second, nil
}
return 0, fmt.Errorf("could not parse duration from: %s (%+v)", dur, res)
}