From a3163cdd5f6e8f5985bca9151924dec2d2cc038a Mon Sep 17 00:00:00 2001 From: chenjiandongx Date: Mon, 29 Nov 2021 23:27:03 +0800 Subject: [PATCH] Add -a flag to listen all devices quickly --- README.md | 1 + cli.go | 3 ++- pcap.go | 20 ++++++++++++-------- sniffer.go | 4 ++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6801304..f63d6ce 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cli.go b/cli.go index 4a5b953..4fd7985 100644 --- a/cli.go +++ b/cli.go @@ -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() @@ -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") diff --git a/pcap.go b/pcap.go index c9f57f0..bff7a7e 100644 --- a/pcap.go +++ b/pcap.go @@ -85,6 +85,7 @@ type PcapClient struct { bpfFilter string devicesPrefix []string disableDNSResolve bool + allDevices bool ch chan []Segment wg sync.WaitGroup lookup Lookup @@ -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 { @@ -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) diff --git a/sniffer.go b/sniffer.go index 2c1d940..3fb2e3e 100644 --- a/sniffer.go +++ b/sniffer.go @@ -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 { @@ -56,6 +59,7 @@ func DefaultOptions() Options { Unit: UnitKB, DevicesPrefix: []string{"en", "lo", "eth", "em", "bond"}, DisableDNSResolve: false, + AllDevices: false, } }