Skip to content

Commit

Permalink
Add -a flag to listen all devices quickly
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Nov 29, 2021
1 parent 8399d2c commit a3163cd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Examples:
$ sniffer -b tcp -d lo -d eth

Flags:
-a, --all-devices listen all devices if present
-b, --bpf string specify string pcap filter with the BPF syntax (default "tcp or udp")
-d, --devices-prefix stringArray prefixed devices to monitor (default [en,lo,eth,em,bond])
-h, --help help for sniffer
Expand Down
3 changes: 2 additions & 1 deletion 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.4.0"
const version = "v0.4.1"

func NewApp() *cobra.Command {
defaultOpts := DefaultOptions()
Expand Down Expand Up @@ -52,6 +52,7 @@ func NewApp() *cobra.Command {
}

app.Flags().BoolVarP(&list, "list", "l", false, "list all devices name")
app.Flags().BoolVarP(&opt.AllDevices, "all-devices", "a", false, "listen all devices if present")
app.Flags().StringVarP(&opt.BPFFilter, "bpf", "b", defaultOpts.BPFFilter, "specify string pcap filter with the BPF syntax")
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")
Expand Down
20 changes: 12 additions & 8 deletions pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type PcapClient struct {
bpfFilter string
devicesPrefix []string
disableDNSResolve bool
allDevices bool
ch chan []Segment
wg sync.WaitGroup
lookup Lookup
Expand All @@ -103,6 +104,7 @@ func NewPcapClient(lookup Lookup, opt Options) (*PcapClient, error) {
bpfFilter: opt.BPFFilter,
devicesPrefix: opt.DevicesPrefix,
disableDNSResolve: opt.DisableDNSResolve,
allDevices: opt.AllDevices,
}

if err := client.getAvailableDevices(); err != nil {
Expand All @@ -128,15 +130,17 @@ func (c *PcapClient) getAvailableDevices() error {
}

for _, device := range all {
var found bool
for _, prefix := range c.devicesPrefix {
if strings.HasPrefix(device.Name, prefix) {
found = true
break
if !c.allDevices {
var found bool
for _, prefix := range c.devicesPrefix {
if strings.HasPrefix(device.Name, prefix) {
found = true
break
}
}
if !found {
continue
}
}
if !found {
continue
}

handler, err := c.getHandler(device.Name, c.bpfFilter)
Expand Down
4 changes: 4 additions & 0 deletions sniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Options struct {

// 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 {
Expand All @@ -56,6 +59,7 @@ func DefaultOptions() Options {
Unit: UnitKB,
DevicesPrefix: []string{"en", "lo", "eth", "em", "bond"},
DisableDNSResolve: false,
AllDevices: false,
}
}

Expand Down

0 comments on commit a3163cd

Please sign in to comment.