-
Notifications
You must be signed in to change notification settings - Fork 63
/
sniffer.go
145 lines (121 loc) · 3.01 KB
/
sniffer.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
package main
import (
"fmt"
"os"
"time"
"github.com/gizak/termui/v3"
)
func exit(s string) {
fmt.Fprintln(os.Stderr, "Start sniffer failed:", s)
os.Exit(1)
}
// Options is the options set for the sniffer instance.
type Options struct {
// BPFFilter is the string pcap filter with the BPF syntax
// eg. "tcp and port 80"
BPFFilter string
// Interval is the interval for refresh rate in seconds
Interval int
// ViewMode represents the sniffer view mode, optional: bytes, packets, processes
ViewMode ViewMode
// DevicesPrefix represents prefixed devices to monitor
DevicesPrefix []string
// Unit of stats in processes mode, optional: B, Kb, KB, Mb, MB, Gb, GB
Unit Unit
// DisableDNSResolve decides whether if disable the DNS resolution
DisableDNSResolve bool
// AllDevices specifies whether to listen all devices or not
AllDevices bool
}
func (o Options) Validate() error {
if err := o.ViewMode.Validate(); err != nil {
return err
}
if err := o.Unit.Validate(); err != nil {
return err
}
return nil
}
func DefaultOptions() Options {
return Options{
BPFFilter: "tcp or udp",
Interval: 2,
ViewMode: ModeTableBytes,
Unit: UnitKB,
DevicesPrefix: []string{"en", "lo", "eth", "em", "bond"},
DisableDNSResolve: false,
AllDevices: false,
}
}
type Sniffer struct {
opts Options
dnsResolver *DNSResolver
pcapClient *PcapClient
statsManager *StatsManager
ui *UIComponent
socketFetcher SocketFetcher
}
func NewSniffer(opts Options) (*Sniffer, error) {
dnsResolver := NewDnsResolver()
pcapClient, err := NewPcapClient(dnsResolver.Lookup, opts)
if err != nil {
return nil, err
}
return &Sniffer{
opts: opts,
dnsResolver: dnsResolver,
pcapClient: pcapClient,
statsManager: NewStatsManager(opts),
ui: NewUIComponent(opts),
socketFetcher: GetSocketFetcher(),
}, nil
}
func (s *Sniffer) SwitchViewMode() {
s.opts.ViewMode = (s.opts.ViewMode + 1) % 3
s.statsManager = NewStatsManager(s.opts)
s.ui.Close()
s.ui = NewUIComponent(s.opts)
}
func (s *Sniffer) Start() {
events := termui.PollEvents()
s.Refresh()
var paused bool
ticker := time.NewTicker(time.Duration(s.opts.Interval) * time.Second)
defer ticker.Stop()
for {
select {
case e := <-events:
switch e.ID {
case "<Tab>":
s.ui.viewer.Shift()
case "<Space>":
paused = !paused
case "<Resize>":
payload := e.Payload.(termui.Resize)
s.ui.viewer.Resize(payload.Width, payload.Height)
case "s", "S":
s.SwitchViewMode()
case "q", "Q", "<C-c>":
return
}
case <-ticker.C:
if !paused {
s.Refresh()
}
}
}
}
func (s *Sniffer) Close() {
s.ui.Close()
s.pcapClient.Close()
s.dnsResolver.Close()
}
func (s *Sniffer) Refresh() {
utilization := s.pcapClient.sinker.GetUtilization()
openSockets, err := s.socketFetcher.GetOpenSockets()
if err != nil {
return
}
s.statsManager.Put(Stat{OpenSockets: openSockets, Utilization: utilization})
s.ui.viewer.Render(s.statsManager.GetStats())
}