Skip to content

Commit

Permalink
Release: v0.6.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mandochen committed Jul 27, 2022
1 parent e87a1ad commit 686f001
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 69 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
Expand Down Expand Up @@ -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)
9 changes: 4 additions & 5 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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`,
Expand All @@ -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()
Expand Down
13 changes: 1 addition & 12 deletions conn_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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] {
Expand Down
11 changes: 4 additions & 7 deletions conn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
21 changes: 4 additions & 17 deletions conn_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type (
)

type SocketFetcher interface {
GetOpenSockets(pid ...int32) (OpenSockets, error)
GetOpenSockets() (OpenSockets, error)
}

type Protocol string
Expand Down
5 changes: 1 addition & 4 deletions sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 1 addition & 15 deletions ui.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"fmt"
"strconv"
"time"
Expand Down Expand Up @@ -138,7 +137,6 @@ func NewUIComponent(opt Options) *UIComponent {
packetsPlot: newPlot("Packets: Blue Up / Green Down", 2),
bytesPlot: newPlot(fmt.Sprintf("Bytes: <Unit %sps> Blue Up / Green Down", opt.Unit.String()), 2),
connsPlot: newPlot("Connections", 1),
pids: opt.Pids,
unit: opt.Unit,
}
}
Expand Down Expand Up @@ -208,7 +206,6 @@ type PlotViewer struct {
shiftIdx int
count int
unit Unit
pids []int32
}

func (pv *PlotViewer) Setup() {
Expand All @@ -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 </ %s />", now, buf.String())
return fmt.Sprintf("[Plot Mode] Now: %s", time.Now().Format(timeFormat))
}

func (pv *PlotViewer) updatePackets(data *NetworkData) {
Expand Down

0 comments on commit 686f001

Please sign in to comment.