-
Notifications
You must be signed in to change notification settings - Fork 67
/
cli.go
808 lines (642 loc) · 22.5 KB
/
cli.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
package main
import (
"context"
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/spf13/pflag"
)
var stdErr = os.Stderr // this is used to make stderr redirectable without side effects
// Config holds the configuration.
type Config struct {
RelayIPv4 net.IP
RelayIPv6 net.IP
SOAHostname string
Interface *net.Interface
TTL time.Duration
LeaseLifetime time.Duration
RouterLifetime time.Duration
LocalIPv6 net.IP
RAPeriod time.Duration
StatelessRA bool
DNSTimeout time.Duration
NoDHCPv6DNSTakeover bool
NoDHCPv6 bool
NoDNS bool
NoMDNS bool
NoNetBIOS bool
NoLLMNR bool
NoLocalNameResolution bool
NoIPv6LNR bool
NoRA bool
NoRADNS bool
Spoof []string
DontSpoof []string
SpoofFor []*hostMatcher
DontSpoofFor []*hostMatcher
SpoofTypes *spoofTypes
IgnoreDHCPv6NoFQDN bool
DelegateIgnoredTo string
DontSendEmptyReplies bool
DryMode bool
DryWithDHCPv6Mode bool
StopAfter time.Duration
Verbose bool
NoColor bool
NoTimestamps bool
LogFileName string
NoHostInfo bool
HideIgnored bool
RedirectStderr bool
ListInterfaces bool
spoofFor []string
dontSpoofFor []string
spoofTypes []string
}
// PrintSummary prints a summary of some important configuration parameters.
//
//nolint:forbidigo
func (c Config) PrintSummary() {
fmt.Printf("Listening on interface: %s\n", c.Interface.Name)
if c.LogFileName != "" {
fmt.Printf("Logging to file: %s\n", c.LogFileName)
}
if c.RelayIPv4 != nil {
fmt.Printf("IPv4 relayed to: %s\n", c.RelayIPv4)
}
if c.RelayIPv6 != nil {
fmt.Printf("IPv6 relayed to: %s\n", c.RelayIPv6)
}
if c.SOAHostname != "" {
fmt.Printf("SOA Requests answered with: %s\n", c.SOAHostname)
}
switch {
case c.DryMode:
raNotice := ""
if !c.NoRA && !c.NoDHCPv6DNSTakeover && !c.NoDHCPv6 {
raNotice = " (RA is still enabled)"
}
drySubject := "DHCPv6 and name resolution queries"
if c.DryWithDHCPv6Mode {
drySubject = "Name resolution queries"
}
fmt.Printf("Dry Mode: %s will not be answered%s\n", drySubject, raNotice)
default:
if len(c.Spoof) != 0 {
fmt.Println("Answering queries for: " + joinDomains(c.Spoof))
}
if len(c.DontSpoof) != 0 {
fmt.Println("Ignoring queries for: " + joinDomains(c.DontSpoof))
}
if len(c.SpoofFor) != 0 {
fmt.Println("Answering queries from: " + joinHosts(c.SpoofFor, ", "))
}
if len(c.DontSpoofFor) != 0 {
fmt.Println("Ignoring queries from: " + joinHosts(c.DontSpoofFor, ", "))
}
if len(c.spoofTypes) != 0 {
fmt.Println("Answering only queries of type: " + strings.Join(toUpper(c.spoofTypes), ", "))
}
}
if c.DelegateIgnoredTo != "" {
fmt.Println("Ignored DNS queries are delegated to DNS server:", c.DelegateIgnoredTo)
}
if c.StatelessRA {
dhcp := ""
if !c.NoDHCPv6 {
dhcp = " (DHCPv6 server is still active)"
}
fmt.Println("Stateless DNS configuration via Router Advertisement enabled" + dhcp)
if c.DelegateIgnoredTo == "" && (len(c.SpoofFor) > 0 || len(c.DontSpoofFor) > 0) {
fmt.Println(c.style(fgYellow, bold) + "Warning:" + c.style(reset) + c.style(fgYellow) +
" In stateless mode, the DNS server is sent to all neighbors regardless of --spoof-for/--dont-spoof-for" +
" setting, use --delegate-ignored-to to avoid affecting uninteded hosts" + c.style(reset))
}
}
if c.StopAfter > 0 {
fmt.Printf("Pretender will automatically terminate after: %s\n", formatStopAfter(c.StopAfter))
}
fmt.Println()
}
func (c *Config) style(attrs ...attribute) string {
if c.NoColor {
return ""
}
s := ""
for _, a := range attrs {
s += fmt.Sprintf("%s[%dm", escape, a)
}
return s
}
func (c *Config) setRedundantOptions() {
if c.DryWithDHCPv6Mode {
c.DryMode = true
}
if c.NoDNS && c.NoDHCPv6 {
c.NoDHCPv6DNSTakeover = true
}
if c.NoMDNS && c.NoLLMNR && c.NoNetBIOS {
c.NoLocalNameResolution = true
}
if c.NoLocalNameResolution {
c.NoNetBIOS = true
c.NoLLMNR = true
c.NoMDNS = true
}
if (c.NoDHCPv6DNSTakeover || c.NoDNS) || (c.NoDHCPv6 && !c.StatelessRA) {
c.NoRADNS = true
}
// don't advertize DNS server in RA when --spoof-for or --dont-spoof-for
// filters are present because it will set DNS for all hosts
// if --delegate-ignored-to is specified this is not really a problem
if !c.StatelessRA && c.DelegateIgnoredTo == "" && (len(c.spoofFor) != 0 || len(c.dontSpoofFor) != 0) {
c.NoRADNS = true
}
}
//nolint:forbidigo,maintidx
func configFromCLI() (config Config, logger *Logger, err error) {
var (
interfaceName string
printVersion bool
)
pflag.StringVarP(&interfaceName, "interface", "i", defaultInterface,
"Interface to bind on, supports auto-detection by IPv4 or IPv6")
pflag.IPVarP(&config.RelayIPv4, "ipv4", "4", defaultRelayIPv4,
"Relay IPv4 address with which queries are answered, supports\nauto-detection by interface")
pflag.IPVarP(&config.RelayIPv6, "ipv6", "6", defaultRelayIPv6,
"Relay IPv6 address with which queries are answered, supports\nauto-detection by interface")
pflag.StringVar(&config.SOAHostname, "soa-hostname", defaultSOAHostname,
"Hostname for the SOA record (useful for Kerberos relaying)")
pflag.BoolVar(&config.NoDHCPv6DNSTakeover, "no-dhcp-dns", defaultNoDHCPv6DNSTakeover,
"Disable DHCPv6 DNS takeover attack (DHCPv6 and DNS, mutually\nexlusive with --stateless-ra)")
pflag.BoolVar(&config.NoDHCPv6, "no-dhcp", defaultNoDHCPv6, "Disable DHCPv6 spoofing")
pflag.BoolVar(&config.NoDNS, "no-dns", defaultNoDNS, "Disable DNS spoofing")
pflag.BoolVar(&config.NoMDNS, "no-mdns", defaultNoMDNS, "Disable mDNS spoofing")
pflag.BoolVar(&config.NoNetBIOS, "no-netbios", defaultNoNetBIOS, "Disable NetBIOS-NS spoofing")
pflag.BoolVar(&config.NoLLMNR, "no-llmnr", defaultNoLLMNR, "Disable LLMNR spoofing")
pflag.BoolVar(&config.NoLocalNameResolution, "no-lnr", defaultNoLocalNameResolution,
"Disable local name resolution spoofing (mDNS, LLMNR, NetBIOS-NS)")
pflag.BoolVar(&config.NoIPv6LNR, "no-ipv6-lnr", defaultNoIPv6LNR,
"Disable mDNS and LLMNR via IPv6 (useful with allowlist or blocklist)")
pflag.BoolVar(&config.NoRA, "no-ra", defaultNoRA, "Disable router advertisements")
pflag.BoolVar(&config.NoRADNS, "no-ra-dns", defaultNoRADNS,
"Disable DNS server advertisement via RA (useful because\nRA is not affected by --spoof/--spoof-for filters)")
pflag.StringSliceVar(&config.Spoof, "spoof", defaultSpoof,
"Only spoof these domains, includes subdomain if it starts with\na dot, a single dot "+
"matches local hostnames (allowlist)")
pflag.StringSliceVar(&config.DontSpoof, "dont-spoof", defaultDontSpoof,
"Do not spoof these domains, includes subdomains if it starts\nwitha dot, a single dot "+
"matches local hostnames (blocklist)")
pflag.StringSliceVar(&config.spoofFor, "spoof-for", defaultSpoofFor,
"Only spoof DHCPv6 and name resolution for these `hosts`, it can\ncontain IPs or hostnames "+
"and subdomains are included when the hostname\nstarts with a dot (allowlist)")
pflag.StringSliceVar(&config.dontSpoofFor, "dont-spoof-for", defaultDontSpoofFor,
"Do not spoof DHCPv6 and name resolution for these `hosts`, it can\ncontain IPs or hostnames "+
"and subdomains are included when the hostname\nstarts with a dot (blocklist)")
pflag.StringSliceVar(&config.spoofTypes, "spoof-types", defaultSpoofTypes,
"Only spoof these query `types` (A, AAA, ANY, SOA, all types are spoofed\nif it is empty)")
pflag.BoolVar(&config.IgnoreDHCPv6NoFQDN, "ignore-nofqdn", defaultIgnoreDHCPv6NoFQDN,
"Ignore DHCPv6 messages where the client did not include its\nFQDN (useful with allowlist or blocklists)")
pflag.StringVar(&config.DelegateIgnoredTo, "delegate-ignored-to", defaultDelegateIgnoredTo,
"Delegate ignored DNS queries to an upstream `DNS server`")
pflag.BoolVar(&config.DontSendEmptyReplies, "dont-send-empty-replies", defaultDontSendEmptyReplies,
"Don't reply at all to ignored DNS queries or failed delegated\nqueries instead of sending an empty reply")
pflag.BoolVar(&config.DryMode, "dry", defaultDryMode,
"Do not answer DHCPv6 or any name resolution queries, only log them\n"+
"(does not disable RA but it can be combined with --no-ra/--no-ra-dns)")
pflag.BoolVar(&config.DryWithDHCPv6Mode, "dry-with-dhcp", defaultDryWithDHCPMode,
"Send RA and answer DHCPv6 queries but only log name resolution\n"+
"queries (can be combined with --delegate-ignored-to, takes\nprecedence over --dry)")
pflag.BoolVar(&config.StatelessRA, "stateless-ra", defaultStatelessRA,
"Do not advertize DHCPv6 server in router advertisement, only DNS\n"+
"server (useful with --no-dhcp, mutually exclusive with\n--no-ra/--no-ra-dns/--no-dhcp-dns)")
pflag.DurationVarP(&config.TTL, "ttl", "t", defaultTTL, "Time to live for name resolution responses")
pflag.DurationVar(&config.LeaseLifetime, "lease-lifetime", defaultLeaseLifetime, "DHCPv6 IP lease lifetime")
pflag.DurationVar(&config.RouterLifetime, "router-lifetime", defaultRARouterLifetime,
"Router lifetime specified in router advertisements")
pflag.DurationVar(&config.RAPeriod, "ra-period", defaultRAPeriod, "Time period between router advertisements")
pflag.DurationVar(&config.DNSTimeout, "dns-timeout", defaultDNSTimeout,
"Timeout for DNS queries performed by pretender")
pflag.DurationVar(&config.StopAfter, "stop-after", defaultStopAfter, "Stop running after this duration")
pflag.BoolVarP(&config.Verbose, "verbose", "v", defaultVerbose, "Print debug information")
pflag.BoolVar(&config.NoColor, "no-color", defaultNoColor, "Disables output styling")
pflag.BoolVar(&config.NoTimestamps, "no-timestamps", defaultNoTimestamps, "Disables timestamps in the output")
pflag.StringVarP(&config.LogFileName, "log", "l", defaultLogFileName, "Log `file` name")
pflag.BoolVar(&printVersion, "version", false, "Print version information")
pflag.BoolVar(&config.NoHostInfo, "no-host-info", defaultNoHostInfo, "Do not gather host information")
pflag.BoolVar(&config.HideIgnored, "hide-ignored", defaultHideIgnored, "Do not log ignored queries")
pflag.BoolVar(&config.RedirectStderr, "redirect-stderr", defaultRedirectStderr, "Redirect stderr to stdout")
pflag.BoolVar(&config.ListInterfaces, "interfaces", defaultListInterfaces,
"List interfaces and their addresses (the other options have no effect,\nexcept for --no-color)")
pflag.CommandLine.SortFlags = false
pflag.Parse()
if pflag.NArg() > 0 {
fmt.Printf("%s does not take positional arguments, only the following flags\n\n", binaryName())
pflag.PrintDefaults()
os.Exit(1)
}
if config.RedirectStderr {
stdErr = os.Stdout
}
config.setRedundantOptions()
if printVersion {
fmt.Println(longVersion())
} else {
fmt.Println(shortVersion())
}
if printVersion {
os.Exit(0)
}
if config.ListInterfaces {
err := listInterfaces(os.Stdout, config.NoColor)
if err != nil {
fmt.Fprintf(stdErr, "Error: %v", err)
os.Exit(1)
}
os.Exit(0)
}
logger = NewLogger().WithPrefix("Setup")
logger.Verbose = config.Verbose
logger.NoColor = config.NoColor
logger.PrintTimestamps = !config.NoTimestamps
logger.HideIgnored = config.HideIgnored
logger.NoHostInfo = config.NoHostInfo
if logger.HostInfoCache != nil {
logger.HostInfoCache.DNSTimeout = config.DNSTimeout
}
if config.LogFileName != "" {
f, err := os.OpenFile(config.LogFileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600)
if err != nil {
return config, logger, fmt.Errorf("log file: %w", err)
}
logger.LogFile = f
}
if !config.NoColor {
err := enableVirtualTerminalProcessing()
if err != nil {
config.NoColor = true
logger.NoColor = true
logger.Errorf("Warning: cannot enable virtual terminal processing: %v, disabling colored output", err)
logger.Flush()
}
}
if config.NoRADNS && config.StatelessRA {
return config, logger, fmt.Errorf("--no-ra-dns/--no-dhcp-dns and --stateless-ra are mutually exclusive")
} else if config.NoRA && config.StatelessRA {
return config, logger, fmt.Errorf("--no-ra and --stateless-ra are mutually exclusive")
}
config.Interface, err = chooseInterface(interfaceName, config.RelayIPv4, config.RelayIPv6)
if err != nil {
return config, logger, interfaceError{err}
}
relayIPv4, errIPv4 := autoDetectRelayIPv4(config.Interface, config.RelayIPv4, config.RelayIPv6)
relayIPv6, errIPv6 := autoDetectRelayIPv6(config.Interface, config.RelayIPv4, config.RelayIPv6)
config.RelayIPv4 = relayIPv4
config.RelayIPv6 = relayIPv6
if config.RelayIPv4 == nil && !config.NoNetBIOS {
logger.Errorf("no relay IPv4 configured (required for NetBIOS-NS): %v", errIPv4)
config.NoNetBIOS = true
}
if config.RelayIPv6 == nil && config.RelayIPv4 == nil {
return config, logger, fmt.Errorf("no relay IP configured: %s and %s", errIPv4, errIPv6) //nolint:errorlint
}
config.LocalIPv6, err = getLinkLocalIPv6Address(config.Interface)
if err != nil && !config.NoDHCPv6DNSTakeover {
logger.Errorf("cannot detect link local IPv6 (required for DHCPv6 DNS takeover): %v", err)
config.NoDHCPv6DNSTakeover = true
}
if config.DelegateIgnoredTo != "" {
upstreamDNSAddr, err := asDNSServerAddress(config.DelegateIgnoredTo)
if err != nil {
return config, logger, fmt.Errorf("invalid upstream DNS address: %w", err)
}
config.DelegateIgnoredTo = upstreamDNSAddr
}
stripSpaces(config.Spoof)
stripSpaces(config.DontSpoof)
config.SpoofFor = asHostMatchers(config.spoofFor, config.DNSTimeout)
config.DontSpoofFor = asHostMatchers(config.dontSpoofFor, config.DNSTimeout)
config.SpoofTypes, err = parseSpoofTypes(config.spoofTypes)
if err != nil {
return config, logger, fmt.Errorf("parsing --spoof-types: %w", err)
}
config.PrintSummary()
return config, logger, nil
}
func joinDomains(domains []string) string {
processed := make([]string, 0, len(domains))
for _, domain := range domains {
switch {
case domain == ".":
processed = append(processed, "<local hostnames>")
case strings.HasPrefix(domain, "."):
processed = append(processed, "*"+strings.TrimRight(domain, "."))
default:
processed = append(processed, strings.TrimRight(domain, "."))
}
}
return strings.Join(processed, ", ")
}
func joinHosts(hosts []*hostMatcher, sep string) string {
hostStrings := make([]string, 0, len(hosts))
for _, ip := range hosts {
hostStrings = append(hostStrings, ip.String())
}
return strings.Join(hostStrings, sep)
}
func chooseInterface(interfaceName string, ipv4, ipv6 net.IP) (*net.Interface, error) {
if interfaceName != "" {
return net.InterfaceByName(interfaceName)
}
var candidateByIPv4, candidateByIPv6 *net.Interface
var err error
if ipv4 != nil {
if ipv4.To4() == nil {
return nil, fmt.Errorf("expected IPv4 address but got IPv6 address %s", ipv4)
}
candidateByIPv4, err = getInterfaceByIP(ipv4)
if err != nil {
return nil, fmt.Errorf("choose interface by IP: %w", err)
}
}
if ipv6 != nil {
if ipv6.To4() != nil {
return nil, fmt.Errorf("expected IPv6 address but got IPv6 address %s", ipv6)
}
candidateByIPv6, err = getInterfaceByIP(ipv6)
if err != nil {
return nil, fmt.Errorf("choose interface by IP: %w", err)
}
}
if ipv4 == nil && ipv6 == nil {
return nil, fmt.Errorf("interface cannot be automatically detected when no relay addresses are provided")
}
if candidateByIPv4 != nil && candidateByIPv6 == nil {
return candidateByIPv4, nil
}
if candidateByIPv6 != nil && candidateByIPv4 == nil {
return candidateByIPv6, nil
}
if candidateByIPv4 == nil && candidateByIPv6 == nil {
ifaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("listing interfaces: %w", err)
}
ifaces = withoutLoopback(ifaces)
if len(ifaces) != 0 {
return nil, fmt.Errorf("no possible candidates to determine interface")
}
return &ifaces[0], nil
}
if candidateByIPv4.Name != candidateByIPv6.Name {
return nil, fmt.Errorf("cannot determine interface: conflict between %s (by IPv4) and %s (by IPv6)",
candidateByIPv4.Name, candidateByIPv6.Name)
}
return candidateByIPv4, nil
}
func withoutLoopback(ifaces []net.Interface) []net.Interface {
filtered := []net.Interface{}
for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback == 0 {
filtered = append(filtered, iface)
}
}
return filtered
}
func getInterfaceByIP(ip net.IP) (*net.Interface, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("listing interfaces: %w", err)
}
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
ifaceIP, ok := addr.(*net.IPNet)
if !ok {
return nil, fmt.Errorf("unexpected IP type: %T", addr)
}
if net.IP.Equal(ifaceIP.IP, ip) {
return &iface, nil
}
}
}
return nil, fmt.Errorf("cannot find interface with IP %s", ip)
}
func autoDetectRelayIPv4(iface *net.Interface, ipv4, ipv6 net.IP) (net.IP, error) {
if ipv4 == nil {
if ipv6 != nil {
return nil, fmt.Errorf("IPv4 auto-detection is disabled when an IPv6 relay address is specified")
}
ip, err := detectLocalIPv4(iface)
if err != nil {
return nil, fmt.Errorf("cannot auto-detect IPv4: %w", err)
}
return ip, nil
}
if ipv4.To4() == nil {
return nil, fmt.Errorf("expected IPv4 address but got IPv6 address %s", ipv4)
}
return ipv4, nil
}
func detectLocalIPv4(iface *net.Interface) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("listing addresses of interface %q: %w", iface.Name, err)
}
candidates := []net.IP{}
for _, addr := range addrs {
ip, ok := addr.(*net.IPNet)
if !ok {
return nil, fmt.Errorf("unexpected IP type: %T", addr)
}
if ip.IP.To4() == nil {
continue
}
candidates = append(candidates, ip.IP)
}
if len(candidates) > 1 {
return nil, fmt.Errorf("multiple possible IPv4 addresses on interface %q", iface.Name)
}
if len(candidates) == 0 {
return nil, fmt.Errorf("interface %q has no IPv4 addresses", iface.Name)
}
return candidates[0], nil
}
func autoDetectRelayIPv6(iface *net.Interface, ipv4, ipv6 net.IP) (net.IP, error) {
if ipv6 == nil {
if ipv4 != nil {
return nil, fmt.Errorf("IPv6 auto-detection is disabled when an IPv4 relay address is specified")
}
ip, err := detectLocalIPv6(iface)
if err != nil {
return nil, fmt.Errorf("cannot auto-detect IPv6: %w", err)
}
return ip, nil
}
if ipv6.To4() != nil {
return nil, fmt.Errorf("expected IPv6 address but got IPv4 address %s", ipv6)
}
return ipv6, nil
}
func detectLocalIPv6(iface *net.Interface) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("listing addresses of interface %q: %w", iface.Name, err)
}
candidates := []net.IP{}
for _, addr := range addrs {
ip, ok := addr.(*net.IPNet)
if !ok {
return nil, fmt.Errorf("unexpected IP type: %T", addr)
}
if ip.IP.To4() != nil || ip.IP.IsLinkLocalMulticast() {
continue
}
candidates = append(candidates, ip.IP)
}
if len(candidates) > 1 {
return nil, fmt.Errorf("multiple possible IPv6 addresses on interface %q", iface.Name)
}
if len(candidates) == 0 {
return nil, fmt.Errorf("interface %q has no IPv6 addresses", iface.Name)
}
return candidates[0], nil
}
func getLinkLocalIPv6Address(iface *net.Interface) (net.IP, error) {
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("gather addresses of interface %q: %w", iface.Name, err)
}
for _, addr := range addrs {
ip, ok := addr.(*net.IPNet)
if !ok {
return nil, fmt.Errorf("unexpected IP type: %T", addr)
}
if ip.IP.To4() == nil && ip.IP.IsLinkLocalUnicast() {
return ip.IP, nil
}
}
return nil, fmt.Errorf("interface %q has no link local IPv6 address", iface.Name)
}
func listInterfaces(w io.Writer, noColor bool) error {
ifaces, err := net.Interfaces()
if err != nil {
return fmt.Errorf("listing interfaces: %w", err)
}
indent := " "
for _, iface := range ifaces {
fmt.Fprintf(w, "\n%2d: %s %s:\n", iface.Index,
styled(iface.Name, noColor, bold, fgRed), styled("<"+iface.Flags.String()+">", noColor, faint))
if iface.HardwareAddr != nil {
fmt.Fprintf(w, "%sMAC : %s\n", indent, styled(iface.HardwareAddr.String(), noColor, bold))
}
addrs, err := iface.Addrs()
if err != nil {
return fmt.Errorf("gather addresses of interface %q: %w", iface.Name, err)
}
for _, addr := range addrs {
ip, ok := addr.(*net.IPNet)
if !ok {
return fmt.Errorf("unexpected IP type: %T", addr)
}
ipType := "IPv6"
if ip.IP.To4() != nil {
ipType = "IPv4"
}
fmt.Fprintf(w, "%s%s: %s %s\n", indent, ipType, styled(addr.String(), noColor, bold),
styled(ipProperties(ip.IP), noColor, faint))
}
}
return nil
}
func ipProperties(ip net.IP) string {
properties := []string{}
if ip.IsLoopback() {
properties = append(properties, "loopback")
}
if ip.IsGlobalUnicast() {
properties = append(properties, "global unicast")
}
if ip.IsLinkLocalUnicast() {
properties = append(properties, "link local unicast")
}
if ip.IsMulticast() {
properties = append(properties, "multicast")
}
return "<" + strings.Join(properties, "|") + ">"
}
type interfaceError struct {
error
}
func (ie interfaceError) Error() string {
return ie.error.Error()
}
func formatStopAfter(d time.Duration) string {
if d < time.Minute {
return d.String()
}
now := time.Now()
stopTime := now.Add(d)
dateFormatter := "03:04pm"
if stopTime.Day() != now.Day() {
dateFormatter += " 02-Jan-06"
}
return fmt.Sprintf("%s (%s)", d.String(), stopTime.Format(dateFormatter))
}
func toUpper(elements []string) []string {
upper := make([]string, 0, len(elements))
for _, el := range elements {
upper = append(upper, strings.ToUpper(el))
}
return upper
}
func asDNSServerAddress(addr string) (string, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
host = addr
port = "53"
}
ip, err := hostToIP(host)
if err != nil {
return "", err
}
return net.JoinHostPort(ip.String(), port), nil
}
func hostToIP(host string) (net.IP, error) {
ip := net.ParseIP(host)
if ip != nil {
return ip, nil
}
ctx, cancel := context.WithTimeout(context.Background(), defaultLookupTimeout)
defer cancel()
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", host)
if err != nil {
return nil, fmt.Errorf("lookup %s: %w", host, err)
}
if len(ips) == 0 {
return nil, fmt.Errorf("lookup %s: empty response", host)
}
return ips[0], nil
}
func binaryName() string {
binary, err := os.Executable()
if err == nil {
return filepath.Base(binary)
}
if len(os.Args) != 0 && !strings.HasPrefix(os.Args[0], "-") {
return filepath.Base(os.Args[0])
}
return "pretender"
}
func stripSpaces(elements []string) {
for i, el := range elements {
elements[i] = strings.TrimSpace(el)
}
}