-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathmain.go
105 lines (81 loc) · 2.35 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
/****************************************
* *
* RedTeam Pentesting GmbH *
* https://www.redteam-pentesting.de/ *
* *
****************************************/
package main
import (
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
)
func main() {
config, logger, err := configFromCLI()
if err != nil {
logger.Errorf("Error: " + err.Error())
logger.Flush()
if errors.As(err, &interfaceError{}) {
logger.Errorf("Try specifying one of the following interfaces:")
logger.Flush()
_ = listInterfaces(stdErr, config.NoColor)
}
logger.Close()
os.Exit(1)
}
runListeners(config, logger)
logger.Close()
}
func runListeners(config Config, logger *Logger) {
ctx, cancel := signal.NotifyContext(context.Background(),
os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGABRT)
defer cancel()
if config.StopAfter > 0 {
ctx, cancel = context.WithTimeout(ctx, config.StopAfter)
defer cancel()
}
wg := newServiceWaitGroup(ctx)
if !config.NoNetBIOS && !config.NoLocalNameResolution {
wg.Run(RunNetBIOSResponder, logger.WithPrefix("NetBIOS"), config)
}
if !config.NoLLMNR && !config.NoLocalNameResolution {
wg.Run(RunLLMNRResponder, logger.WithPrefix("LLMNR"), config)
}
if !config.NoMDNS && !config.NoLocalNameResolution {
wg.Run(RunMDNSResponder, logger.WithPrefix("mDNS"), config)
}
if !config.NoDHCPv6DNSTakeover && !config.NoDNS {
wg.Run(RunDNSResponder, logger.WithPrefix("DNS"), config)
}
if !config.NoDHCPv6DNSTakeover && !config.NoDHCPv6 {
wg.Run(RunDHCPv6Server, logger.WithPrefix("DHCPv6"), config)
}
if !config.NoRA {
wg.Run(SendRouterAdvertisements, logger.WithPrefix("RA"), config)
}
wg.Wait()
}
type serviceFunc func(context.Context, *Logger, Config) error
type serviceWaitGroup struct {
ctx context.Context //nolint:containedctx
sync.WaitGroup
}
func newServiceWaitGroup(ctx context.Context) *serviceWaitGroup {
return &serviceWaitGroup{ctx: ctx}
}
func (wg *serviceWaitGroup) Run(service serviceFunc, logger *Logger, config Config) {
wg.WaitGroup.Add(1)
go func() {
err := service(wg.ctx, logger, config)
if err != nil {
logger.Errorf(escapeFormatString(err.Error()))
} else if wg.ctx.Err() != nil {
logger.Debugf("shutdown")
}
wg.WaitGroup.Done()
}()
}