diff --git a/README.md b/README.md index f8648a5..4315b71 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # sniffer +[![GoDoc](https://godoc.org/github.com/chenjiandongx/sniffer?status.svg)](https://godoc.org/github.com/chenjiandongx/sniffer) +[![Go Report Card](https://goreportcard.com/badge/github.com/chenjiandongx/sniffer)](https://goreportcard.com/report/github.com/chenjiandongx/sniffer) +[![License](https://img.shields.io/badge/License-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT) + > *A modern alternative network traffic sniffer inspired by [bandwhich](https://github.com/imsnif/bandwhich)(Rust) and [nethogs](https://github.com/raboof/nethogs)(C++).* https://user-images.githubusercontent.com/19553554/141692419-da8b63e1-e936-4d14-8ff5-5893e20a1f27.mov diff --git a/conn_darwin.go b/conn_darwin.go index 553ab9b..a08bac3 100644 --- a/conn_darwin.go +++ b/conn_darwin.go @@ -13,12 +13,13 @@ import ( ) type lsofConn struct { - invoker LsofInvoker + invoker lsofInvoker } -type LsofInvoker struct{} +type lsofInvoker struct{} -func (i LsofInvoker) Exec() ([]byte, error) { +// Exec executes the command and return the output bytes of it. +func (i lsofInvoker) Exec() ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() diff --git a/dns.go b/dns.go index 537e3f3..9998223 100644 --- a/dns.go +++ b/dns.go @@ -50,6 +50,7 @@ func (c *DNSResolver) Close() { c.wg.Wait() } +// Lookup resolve remote ip to the domains. func (c *DNSResolver) Lookup(ip string) string { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() @@ -62,6 +63,10 @@ func (c *DNSResolver) Lookup(ip string) string { return ip } + if len(addrs) == 1 { + return addrs[0] + } + sort.Strings(addrs) return addrs[0] } diff --git a/ui.go b/ui.go index 5f6c887..268e287 100644 --- a/ui.go +++ b/ui.go @@ -287,18 +287,12 @@ func (pv *PlotViewer) Shift() { pv.shiftIdx++ width, height := termui.TerminalDimensions() pv.grid = pv.newGrid(width, height) - if pv.count <= 1 { - return - } - termui.Render(pv.grid) + pv.render() } func (pv *PlotViewer) Resize(width, height int) { pv.grid = pv.newGrid(width, height) - if pv.count <= 1 { - return - } - termui.Render(pv.grid) + pv.render() } func (pv *PlotViewer) Render(stats interface{}) { @@ -313,6 +307,10 @@ func (pv *PlotViewer) Render(stats interface{}) { pv.updatePackets(data) pv.updateBytes(data) pv.updateConnections(data) + pv.render() +} + +func (pv *PlotViewer) render() { if pv.count <= 1 { return }