diff --git a/README.md b/README.md index 0c4c69b..59ac307 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,8 @@ Usage: sniffer [flags] Examples: - # processes mode for pid 1024,2048 in MB unit - $ sniffer -p 1024 -p 2048 -m 2 -u MB + # bytes mode in MB unit + $ sniffer -u MB # only capture the TCP protocol packets with lo,eth prefixed devices $ sniffer -b tcp -d lo -d eth @@ -77,9 +77,8 @@ Flags: -h, --help help for sniffer -i, --interval int interval for refresh rate in seconds (default 1) -l, --list list all devices name - -m, --mode int view mode of sniffer (0: bytes 1: packets 2: processes) + -m, --mode int view mode of sniffer (0: bytes 1: packets 2: plot) -n, --no-dns-resolve disable the DNS resolution - -p, --pids int32Slice pids to watch, empty stands for all pids (default []) -u, --unit string unit of traffic stats, optional: B, Kb, KB, Mb, MB, Gb, GB (default "KB") -v, --version version for sniffer ``` @@ -128,10 +127,6 @@ See what stats they show, sniffer and bandwhich output are very approximate(~ 2. ![](https://user-images.githubusercontent.com/19553554/147360686-5600d65b-9685-486b-b7cf-42c341364009.jpg) -***Processes Mode:*** display traffic stats groups by process using Plot widget. - -![](https://user-images.githubusercontent.com/19553554/147360725-c9541fdd-3203-4ead-8f29-042478717abb.jpg) - ## License MIT [©chenjiandongx](https://github.com/chenjiandongx) diff --git a/cli.go b/cli.go index 671567f..78f6dd4 100644 --- a/cli.go +++ b/cli.go @@ -6,7 +6,7 @@ import ( "github.com/spf13/cobra" ) -const version = "v0.6.0" +const version = "v0.6.1" func NewApp() *cobra.Command { defaultOpts := DefaultOptions() @@ -44,8 +44,8 @@ func NewApp() *cobra.Command { defer sniffer.Close() sniffer.Start() }, - Example: ` # processes mode for pid 1024,2048 in MB unit - $ sniffer -p 1024 -p 2048 -m 2 -u MB + Example: ` # bytes mode in MB unit + $ sniffer -u MB # only capture the TCP protocol packets with lo,eth prefixed devices $ sniffer -b tcp -d lo -d eth`, @@ -57,8 +57,7 @@ func NewApp() *cobra.Command { app.Flags().IntVarP(&opt.Interval, "interval", "i", defaultOpts.Interval, "interval for refresh rate in seconds") app.Flags().StringArrayVarP(&opt.DevicesPrefix, "devices-prefix", "d", defaultOpts.DevicesPrefix, "prefixed devices to monitor") app.Flags().BoolVarP(&opt.DisableDNSResolve, "no-dns-resolve", "n", defaultOpts.DisableDNSResolve, "disable the DNS resolution") - app.Flags().Int32SliceVarP(&opt.Pids, "pids", "p", defaultOpts.Pids, "pids to watch, empty stands for all pids") - app.Flags().IntVarP(&mode, "mode", "m", int(defaultOpts.ViewMode), "view mode of sniffer (0: bytes 1: packets 2: processes)") + app.Flags().IntVarP(&mode, "mode", "m", int(defaultOpts.ViewMode), "view mode of sniffer (0: bytes 1: packets 2: plot)") app.Flags().StringVarP(&unit, "unit", "u", defaultOpts.Unit.String(), "unit of traffic stats, optional: B, Kb, KB, Mb, MB, Gb, GB") app.Flags().PrintDefaults() diff --git a/conn_darwin.go b/conn_darwin.go index 47602c9..0a580f3 100644 --- a/conn_darwin.go +++ b/conn_darwin.go @@ -44,18 +44,13 @@ func (i lsofInvoker) Exec() ([]byte, error) { return buf.Bytes(), nil } -func (lc *lsofConn) GetOpenSockets(pids ...int32) (OpenSockets, error) { +func (lc *lsofConn) GetOpenSockets() (OpenSockets, error) { sockets := make(OpenSockets) output, err := lc.invoker.Exec() if err != nil { return sockets, err } - set := make(map[int32]bool) - for _, pid := range pids { - set[pid] = true - } - lines := strings.Split(string(output), "\n") for _, line := range lines { fields := strings.Fields(line) @@ -65,12 +60,6 @@ func (lc *lsofConn) GetOpenSockets(pids ...int32) (OpenSockets, error) { procName := strings.ReplaceAll(fields[0], "\\x20", " ") pid, _ := strconv.Atoi(fields[1]) - if len(pids) > 0 { - if !set[int32(pid)] { - continue - } - } - procInfo := ProcessInfo{Pid: pid, Name: procName} switch fields[8] { diff --git a/conn_linux.go b/conn_linux.go index 6b1c1e6..c2b5708 100644 --- a/conn_linux.go +++ b/conn_linux.go @@ -381,13 +381,10 @@ func (nl *netlinkConn) listPids() ([]int32, error) { return pids, nil } -func (nl *netlinkConn) GetOpenSockets(pids ...int32) (OpenSockets, error) { - var err error - if len(pids) == 0 { - pids, err = nl.listPids() - if err != nil { - return nil, err - } +func (nl *netlinkConn) GetOpenSockets() (OpenSockets, error) { + pids, err := nl.listPids() + if err != nil { + return nil, err } inodeMap := nl.getAllProcsInodes(pids...) diff --git a/conn_windows.go b/conn_windows.go index 862df3b..2fb89fb 100644 --- a/conn_windows.go +++ b/conn_windows.go @@ -12,16 +12,12 @@ import ( type psutilConn struct{} -func (ps *psutilConn) GetOpenSockets(pids ...int32) (OpenSockets, error) { - return ps.getOpenSockets(pids...) -} - -func (ps *psutilConn) getOpenSockets(pids ...int32) (OpenSockets, error) { +func (ps *psutilConn) GetOpenSockets() (OpenSockets, error) { openSockets := make(OpenSockets) - if err := ps.getConnections(ProtoTCP, openSockets, pids...); err != nil { + if err := ps.getConnections(ProtoTCP, openSockets); err != nil { return nil, err } - if err := ps.getConnections(ProtoUDP, openSockets, pids...); err != nil { + if err := ps.getConnections(ProtoUDP, openSockets); err != nil { return nil, err } @@ -45,26 +41,17 @@ func (ps *psutilConn) getProcName(pid int32) ProcessInfo { return procInfo } -func (ps *psutilConn) getConnections(proto Protocol, openSockets OpenSockets, pids ...int32) error { +func (ps *psutilConn) getConnections(proto Protocol, openSockets OpenSockets) error { connections, err := net.Connections(string(proto)) if err != nil { return err } - set := make(map[int32]bool) - for _, pid := range pids { - set[pid] = true - } - for _, conn := range connections { if proto == ProtoTCP && conn.Status != "ESTABLISHED" { continue } - if len(pids) > 0 && !set[conn.Pid] { - continue - } - localSocket := LocalSocket{ IP: conn.Laddr.IP, Port: uint16(conn.Laddr.Port), diff --git a/pcap.go b/pcap.go index b52cc67..7feee93 100644 --- a/pcap.go +++ b/pcap.go @@ -40,7 +40,7 @@ type ( ) type SocketFetcher interface { - GetOpenSockets(pid ...int32) (OpenSockets, error) + GetOpenSockets() (OpenSockets, error) } type Protocol string diff --git a/sniffer.go b/sniffer.go index 915840d..ddd58e1 100644 --- a/sniffer.go +++ b/sniffer.go @@ -28,9 +28,6 @@ type Options struct { // DevicesPrefix represents prefixed devices to monitor DevicesPrefix []string - // Pids to watch in processes mode - Pids []int32 - // Unit of stats in processes mode, optional: B, Kb, KB, Mb, MB, Gb, GB Unit Unit @@ -136,7 +133,7 @@ func (s *Sniffer) Close() { func (s *Sniffer) Refresh() { utilization := s.pcapClient.sinker.GetUtilization() - openSockets, err := s.socketFetcher.GetOpenSockets(s.opts.Pids...) + openSockets, err := s.socketFetcher.GetOpenSockets() if err != nil { return } diff --git a/ui.go b/ui.go index fc15aee..2809659 100644 --- a/ui.go +++ b/ui.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "fmt" "strconv" "time" @@ -138,7 +137,6 @@ func NewUIComponent(opt Options) *UIComponent { packetsPlot: newPlot("Packets: Blue Up / Green Down", 2), bytesPlot: newPlot(fmt.Sprintf("Bytes: Blue Up / Green Down", opt.Unit.String()), 2), connsPlot: newPlot("Connections", 1), - pids: opt.Pids, unit: opt.Unit, } } @@ -208,7 +206,6 @@ type PlotViewer struct { shiftIdx int count int unit Unit - pids []int32 } func (pv *PlotViewer) Setup() { @@ -232,18 +229,7 @@ func (pv *PlotViewer) newQueue(size int) *queue { } func (pv *PlotViewer) getHeaderText() string { - now := time.Now().Format(timeFormat) - if len(pv.pids) <= 0 { - return fmt.Sprintf("[Processes Mode] Now: %s Pids All", now) - } - buf := &bytes.Buffer{} - for i, pid := range pv.pids { - buf.WriteString(strconv.Itoa(int(pid))) - if i+1 != len(pv.pids) { - buf.WriteString(" ") - } - } - return fmt.Sprintf("[Processes Mode] Now: %s Pids ", now, buf.String()) + return fmt.Sprintf("[Plot Mode] Now: %s", time.Now().Format(timeFormat)) } func (pv *PlotViewer) updatePackets(data *NetworkData) {