-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrouter_types.go
667 lines (542 loc) · 15.5 KB
/
router_types.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
package hitron
import (
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"time"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// RouterSysInfo -
type RouterSysInfo struct {
SystemTime time.Time // current time
PrivLanNet *net.IPNet //
CMVersion
Error
LANName string // :"brlan0",
WanName string // :"erouter0",
RouterMode string // :"Dualstack"
PrivLanIP net.IP // :"192.168.0.1\/24",
SecDNS net.IP // :"",
DNS []net.IP // :["127.0.0.1","2607:f2c0::2"],
WanIP []net.IP // :["23.233.27.226","2607:f2c0:f200:a03:59e0:7e1e:f96b:923b"],
RFMac net.HardwareAddr // :"74:9B:DE:AD:BE:EF",
SystemLanUptime time.Duration // : "468117",
SystemWanUptime time.Duration // :"468083",
LanRx uint64 // :"19601748772",
LanTx uint64 // :"141585555187",
WanRx uint64 // :"139788502458",
WanRxPkts uint64 // :"175946286",
WanTx uint64 // :"18787516468",
WanTxPkts uint64 // :"52845543",
}
//nolint:funlen
func (s RouterSysInfo) String() string {
if s.Error != NoError && s.Error.Message != "" {
return s.Error.String()
}
sb := strings.Builder{}
sb.WriteString("CMVersion:\n\tDeviceID: ")
sb.WriteString(s.CMVersion.DeviceID)
sb.WriteString("\n\tModelName: ")
sb.WriteString(s.CMVersion.ModelName)
sb.WriteString("\n\tVendorName: ")
sb.WriteString(s.CMVersion.VendorName)
sb.WriteString("\n\tSerialNum: ")
sb.WriteString(s.CMVersion.SerialNum)
sb.WriteString("\n\tHwVersion: ")
sb.WriteString(s.CMVersion.HwVersion)
sb.WriteString("\n\tAPIVersion: ")
sb.WriteString(s.CMVersion.APIVersion)
sb.WriteString("\n\tSoftwareVersion: ")
sb.WriteString(s.CMVersion.SoftwareVersion)
sb.WriteString("\n")
sb.WriteString("SystemTime: ")
sb.WriteString(s.SystemTime.String())
sb.WriteString("\n")
sb.WriteString("LAN: ")
sb.WriteString(s.LANName)
sb.WriteString(" (IP ")
sb.WriteString(s.PrivLanIP.String())
sb.WriteString(") (Net ")
sb.WriteString(s.PrivLanNet.String())
sb.WriteString(")\n")
sb.WriteString(" Rx/Tx: ")
sb.WriteString(byteSize(s.LanRx))
sb.WriteString("/")
sb.WriteString(byteSize(s.LanTx))
sb.WriteString("\n")
sb.WriteString("WAN: ")
sb.WriteString(s.WanName)
sb.WriteString(" (")
for i, ip := range s.WanIP {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(ip.String())
}
sb.WriteString(")\n")
sb.WriteString(" Rx/Tx: ")
sb.WriteString(byteSize(s.WanRx))
sb.WriteString("/")
sb.WriteString(byteSize(s.WanTx))
sb.WriteString("\n")
sb.WriteString(" Rx/Tx Packets: ")
p := message.NewPrinter(language.English)
sb.WriteString(p.Sprintf("%d/%d", s.WanRxPkts, s.WanTxPkts))
sb.WriteString("\n")
sb.WriteString("DNS: ")
for i, ip := range s.DNS {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(ip.String())
}
sb.WriteString("\n")
if s.SecDNS != nil {
sb.WriteString("SecDNS: ")
sb.WriteString(s.SecDNS.String())
sb.WriteString("\n")
}
sb.WriteString("RFMac: ")
sb.WriteString(s.RFMac.String())
sb.WriteString("\n")
sb.WriteString("System Uptime: LAN ")
sb.WriteString(s.SystemLanUptime.String())
sb.WriteString(", WAN ")
sb.WriteString(s.SystemWanUptime.String())
sb.WriteString("\n")
sb.WriteString("RouterMode: ")
sb.WriteString(s.RouterMode)
sb.WriteString("\n")
return sb.String()
}
// UnmarshalJSON - implements json.Unmarshaler
//
//nolint:funlen,gocyclo
func (s *RouterSysInfo) UnmarshalJSON(b []byte) error {
raw := struct {
Error
CMVersion
SysTime string `json:"sysTime"` // :"2020-11-17 02:12:33",
TZ string `json:"tz"` // :"13_2_1",
LANName string `json:"lanName"` // :"brlan0",
PrivLanIP string `json:"privLanIP"` // :"192.168.0.1\/24",
LanRx string `json:"lanRx"` // :"19601748772",
LanTx string `json:"lanTx"` // :"141585555187",
WanName string `json:"wanName"` // :"erouter0",
WanRx string `json:"wanRx"` // :"139788502458",
WanRxPkts string `json:"wanRxPkts"` // :"175946286",
WanTx string `json:"wanTx"` // :"18787516468",
WanTxPkts string `json:"wanTxPkts"` // :"52845543",
RFMac string `json:"rfMac"` // :"74:9B:DE:AD:BE:EF",
SystemLanUptime string `json:"systemLanUptime"` // : "468117",
SystemWanUptime string `json:"systemWanUptime"` // :"468083",
RouterMode string `json:"routerMode"` // :"Dualstack"
SecDNS string `json:"secDNS"` // :"",
WanIP []string `json:"wanIP"` // :["23.233.27.226","2607:f2c0:f200:a03:59e0:7e1e:f96b:923b"],
DNS []string `json:"dns"` // :["127.0.0.1","2607:f2c0::2"],
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal RouterSysInfo %q: %w", string(b), err)
}
s.Error = raw.Error
s.CMVersion = raw.CMVersion
s.LANName = raw.LANName
s.WanName = raw.WanName
s.RouterMode = raw.RouterMode
// secDNS can be empty, or set to a value like "none", so we ignore
// non-parseable IPs here
secDNS := net.ParseIP(raw.SecDNS)
if len(secDNS) > 0 {
s.SecDNS = secDNS
}
wanIP, err := parseIPList(raw.WanIP)
if err != nil {
return fmt.Errorf("failed to parse WanIP %q: %w", raw.WanIP, err)
}
s.WanIP = wanIP
dnsIPs, err := parseIPList(raw.DNS)
if err != nil {
return fmt.Errorf("failed to parse DNS %q: %w", raw.DNS, err)
}
s.DNS = dnsIPs
// sometimes the CM gets confused and stops returning the MAC Address...
if raw.RFMac != "" {
s.RFMac, err = net.ParseMAC(raw.RFMac)
if err != nil {
return fmt.Errorf("failed to parse RF MAC Address %q: %w", raw.RFMac, err)
}
}
s.PrivLanIP, s.PrivLanNet, err = net.ParseCIDR(raw.PrivLanIP)
if err != nil {
return fmt.Errorf("failed to parse CIDR %q: %w", raw.PrivLanIP, err)
}
s.LanRx = formattedBytesToUint64(raw.LanRx)
s.LanTx = formattedBytesToUint64(raw.LanTx)
s.WanRx = formattedBytesToUint64(raw.WanRx)
s.WanRxPkts = atoui64(raw.WanRxPkts)
s.WanTx = formattedBytesToUint64(raw.WanTx)
s.WanTxPkts = atoui64(raw.WanTxPkts)
lanUp, err := parseUptime(raw.SystemLanUptime)
if err != nil {
return fmt.Errorf("failed to parse LAN uptime %q: %w", raw.SystemLanUptime, err)
}
s.SystemLanUptime = lanUp
wanUp, err := parseUptime(raw.SystemWanUptime)
if err != nil {
return fmt.Errorf("failed to parse WAN uptime %q: %w", raw.SystemWanUptime, err)
}
s.SystemWanUptime = wanUp
l, err := tzToLocation(raw.TZ)
if err != nil {
return err
}
// Date format is MM/DD/YYYY HH:MM:SS - no timezone
t, err := time.ParseInLocation("2006-01-02 15:04:05", raw.SysTime, l)
if err != nil {
return fmt.Errorf("failed to parse timestamp %q/%s: %w", raw.SysTime, raw.TZ, err)
}
s.SystemTime = t
return nil
}
//nolint:gomnd
func parseUptime(s string) (time.Duration, error) {
i, err := strconv.ParseInt(strings.TrimSpace(s), 10, 64)
if err == nil {
return time.Duration(i) * time.Second, nil
}
if s == "" {
return time.Duration(0), nil
}
// a format like '000 days 01h:02m:03s' shows up on some models
var days, hours, minutes, seconds int64
_, err = fmt.Fscanf(strings.NewReader(s), "%d days %dh:%dm:%ds", &days, &hours, &minutes, &seconds)
if err != nil {
return time.Duration(0), fmt.Errorf("failed to parse uptime %q: %w", s, err)
}
result := time.Duration(days*24)*time.Hour +
time.Duration(hours)*time.Hour +
time.Duration(minutes)*time.Minute +
time.Duration(seconds)*time.Second
return result, nil
}
func parseIPList(s []string) ([]net.IP, error) {
ips := []net.IP{}
for _, ip := range s {
// sometimes spaces or newlines are included in the value...
ip = strings.TrimSpace(ip)
// just ignore empty strings
if ip == "" {
continue
}
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
return nil, fmt.Errorf("failed to parse IP %q", ip)
}
ips = append(ips, ipAddr)
}
return ips, nil
}
func tzToLocation(tz string) (*time.Location, error) {
tzmap := map[string]string{
"0": "Etc/UTC",
"0_1_0": "Pacific/Kwajalein",
"1_1_0": "Pacific/Pago_Pago",
"2_1_0": "Pacific/Honolulu",
"3_1_1": "America/Anchorage",
"4_1_1": "America/Los_Angeles",
"5_1_0": "America/Phoenix",
"5_2_1": "America/Denver",
"6_1_1": "America/Mexico_City",
"6_2_1": "America/Chicago",
"7_1_0": "America/Indiana/Indianapolis",
"7_2_1": "America/New_York",
"8_1_0": "America/Caracas",
"8_2_1": "America/Halifax",
"9_1_1": "America/St_Johns",
"10_1_1": "America/Sao_Paulo",
"11_1_1": "Atlantic/South_Georgia",
"12_1_1": "Atlantic/Azores",
"13_1_0": "Africa/Monrovia",
"13_2_1": "Etc/UTC",
"14_1_0": "Africa/Tunis",
"14_2_1": "Europe/Rome",
"15_1_0": "Africa/Johannesburg",
"16_1_0": "Europe/Athens",
"17_1_0": "Europe/Samara",
"18_1_0": "Asia/Yekaterinburg",
"19_1_0": "Asia/Kolkata",
"20_1_0": "Asia/Omsk",
"21_1_0": "Asia/Bangkok",
"22_1_0": "Asia/Shanghai",
"22_2_0": "Asia/Taipei",
"23_1_0": "Asia/Tokyo",
"24_1_0": "Pacific/Guam",
"24_2_1": "Australia/Sydney",
"25_1_0": "Pacific/Bougainville",
"26_1_1": "Pacific/Auckland",
}
l, err := time.LoadLocation(tzmap[tz])
if err != nil {
return time.UTC, fmt.Errorf("failed to load location %q: %w", tzmap[tz], err)
}
return l, nil
}
// RouterCapability -
type RouterCapability struct {
Error
RouterMode string
Gateway bool
UPnP bool
HNAP bool
USB bool
SIPAlg bool
}
func (s RouterCapability) String() string {
if s.Error != NoError {
return s.Error.String()
}
sb := strings.Builder{}
sb.WriteString("RouterMode: ")
sb.WriteString(s.RouterMode)
if s.Gateway {
sb.WriteString("/Gateway")
}
if s.UPnP {
sb.WriteString("/UPnP")
}
if s.HNAP {
sb.WriteString("/HNAP")
}
if s.USB {
sb.WriteString("/USB")
}
if s.SIPAlg {
sb.WriteString("/SIPAlg")
}
return sb.String()
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *RouterCapability) UnmarshalJSON(b []byte) error {
raw := struct {
Error
RouterMode string
GatewayOnOff string
UPnpOnOff string
HnapOnOff string
UsbOnOff string
SIPAlgOnOff string
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal RouterCapability %q: %w", string(b), err)
}
s.Error = raw.Error
s.RouterMode = raw.RouterMode
s.Gateway = raw.GatewayOnOff == on
s.UPnP = raw.UPnpOnOff == on
s.HNAP = raw.HnapOnOff == on
s.USB = raw.UsbOnOff == on
s.SIPAlg = raw.SIPAlgOnOff == on
return nil
}
// RouterLocation -
type RouterLocation struct {
Error
LocationText string
}
func (r RouterLocation) String() string {
if r.Error != NoError {
return r.Error.String()
}
return r.LocationText
}
// RouterDMZ -
type RouterDMZ struct {
Error
Host net.IP
PrivateLan net.IP
Mask net.IP
Enable bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *RouterDMZ) UnmarshalJSON(b []byte) error {
raw := struct {
Error
Enable string
Host net.IP
PrivateLan net.IP
SubMask net.IP
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal RouterDMZ %q: %w", string(b), err)
}
s.Error = raw.Error
s.Enable = raw.Enable == on
s.Host = raw.Host
s.PrivateLan = raw.PrivateLan
s.Mask = raw.SubMask
return nil
}
// RouterPortForwardStatus -
type RouterPortForwardStatus struct {
Error
PrivateLan net.IP
Mask net.IP
Enable bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *RouterPortForwardStatus) UnmarshalJSON(b []byte) error {
raw := struct {
Error
AllRulesOnOff string
PrivateLan net.IP
SubMask net.IP
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal RouterPortForwardStatus %q: %w", string(b), err)
}
s.Error = raw.Error
s.Enable = raw.AllRulesOnOff == on
s.PrivateLan = raw.PrivateLan
s.Mask = raw.SubMask
return nil
}
// RouterPortForwardall -
type RouterPortForwardall struct {
Error
Rules []PortForwardRule `json:"Rules_List"`
Total int
}
// PortForwardRule -
type PortForwardRule struct {
AppName string
Protocol string
RemoteIPs IPRange
LocalIP net.IP
PublicPorts PortRange
PrivatePorts PortRange
ID int
Origin int
Enable bool
}
// PortRange -
type PortRange struct {
Start, End int
}
func parsePort(in string) int {
p, _ := strconv.Atoi(in)
return p
}
// IPRange -
type IPRange struct {
Start, End net.IP
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *PortForwardRule) UnmarshalJSON(b []byte) error {
raw := struct {
ID string
Origin string
AppName string
PubStart, PubEnd string
PriStart, PriEnd string
RuleOnOff string
Protocol string
LocalIPAddr net.IP
RemoteIPStar net.IP
RemoteIPEnd net.IP
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal PortForwardRule %q: %w", string(b), err)
}
s.ID, _ = strconv.Atoi(raw.ID)
s.Origin, _ = strconv.Atoi(raw.Origin)
s.AppName = raw.AppName
s.Protocol = raw.Protocol
s.Enable = raw.RuleOnOff == on
s.PublicPorts = PortRange{parsePort(raw.PubStart), parsePort(raw.PubEnd)}
s.PrivatePorts = PortRange{parsePort(raw.PriStart), parsePort(raw.PriEnd)}
s.LocalIP = raw.LocalIPAddr
s.RemoteIPs = IPRange{raw.RemoteIPStar, raw.RemoteIPEnd}
return nil
}
// RouterPortTriggerStatus -
// Port triggering is a means of automating port forwarding. The CODA-4x8x scans
// outgoing traffic (from the LAN to the WAN) to see if any of the traffic's
// destination ports match those specified in the port triggering rules you
// configure. If any of the ports match, the CODA-4x8x automatically opens the
// incoming ports specified in the rule, in anticipation of incoming traffic.
type RouterPortTriggerStatus struct {
Error
Enable bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *RouterPortTriggerStatus) UnmarshalJSON(b []byte) error {
raw := struct {
Error
AllRulesOnOff string
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal RouterPortTriggerStatus %q: %w", string(b), err)
}
s.Error = raw.Error
s.Enable = raw.AllRulesOnOff == on
return nil
}
// RouterPortTriggerall -
type RouterPortTriggerall struct {
Error
Rules []PortTriggerRule `json:"Rules_List"`
Total int
}
// PortTriggerRule -
type PortTriggerRule struct {
AppName string
Protocol string // TCP, UDP, BOTH
TriggerPorts PortRange // outgoing/public
TargetPorts PortRange // incoming/private
ID int
Timeout time.Duration
Enable bool
TwoWay bool
}
// UnmarshalJSON - implements json.Unmarshaler
func (s *PortTriggerRule) UnmarshalJSON(b []byte) error {
raw := struct {
RuleOnOff string
ID string
AppName string
PubStart, PubEnd string
PriStart, PriEnd string
Protocol string
Timeout string
TwoWayOnOff string
}{}
err := json.Unmarshal(b, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal PortTriggerRule %q: %w", string(b), err)
}
s.ID, _ = strconv.Atoi(raw.ID)
s.Enable = raw.RuleOnOff == on
s.TwoWay = raw.TwoWayOnOff == on
s.AppName = raw.AppName
s.Protocol = raw.Protocol
s.TriggerPorts = PortRange{parsePort(raw.PubStart), parsePort(raw.PubEnd)}
s.TargetPorts = PortRange{parsePort(raw.PriStart), parsePort(raw.PriEnd)}
to, _ := strconv.Atoi(raw.Timeout)
s.Timeout = time.Duration(to) * time.Millisecond
return nil
}
// RouterTR069 -
type RouterTR069 struct {
Error
TR069URL string
}