|
|
@ -1,7 +1,6 @@
|
|
|
|
package libpcap
|
|
|
|
package libpcap
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket/pcap"
|
|
|
|
"github.com/google/gopacket/pcap"
|
|
|
@ -13,22 +12,12 @@ type NetCatch struct {
|
|
|
|
sentence string
|
|
|
|
sentence string
|
|
|
|
fn func(gopacket.Packet)
|
|
|
|
fn func(gopacket.Packet)
|
|
|
|
*pcap.Handle
|
|
|
|
*pcap.Handle
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
stopFn context.CancelFunc
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (n *NetCatch) Recall() func(gopacket.Packet) {
|
|
|
|
|
|
|
|
return n.fn
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (n *NetCatch) SetRecall(fn func(p gopacket.Packet)) {
|
|
|
|
func (n *NetCatch) SetRecall(fn func(p gopacket.Packet)) {
|
|
|
|
n.fn = fn
|
|
|
|
n.fn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (n *NetCatch) Stop() {
|
|
|
|
|
|
|
|
n.stopFn()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func FindAllDevs() ([]pcap.Interface, error) {
|
|
|
|
func FindAllDevs() ([]pcap.Interface, error) {
|
|
|
|
return pcap.FindAllDevs()
|
|
|
|
return pcap.FindAllDevs()
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -56,7 +45,6 @@ func NewCatch(host string, sentence string) (*NetCatch, error) {
|
|
|
|
nc.host = host
|
|
|
|
nc.host = host
|
|
|
|
nc.eth = eth
|
|
|
|
nc.eth = eth
|
|
|
|
nc.sentence = sentence
|
|
|
|
nc.sentence = sentence
|
|
|
|
nc.ctx, nc.stopFn = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
return nc, nil
|
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -64,7 +52,6 @@ func NewCatchEth(eth string, sentence string) (*NetCatch, error) {
|
|
|
|
nc := new(NetCatch)
|
|
|
|
nc := new(NetCatch)
|
|
|
|
nc.eth = eth
|
|
|
|
nc.eth = eth
|
|
|
|
nc.sentence = sentence
|
|
|
|
nc.sentence = sentence
|
|
|
|
nc.ctx, nc.stopFn = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
return nc, nil
|
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -81,14 +68,10 @@ func (n *NetCatch) Run() error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pks := gopacket.NewPacketSource(handle, handle.LinkType())
|
|
|
|
pks := gopacket.NewPacketSource(handle, handle.LinkType())
|
|
|
|
for {
|
|
|
|
for packet := range pks.Packets() {
|
|
|
|
select {
|
|
|
|
if n.fn != nil {
|
|
|
|
case packet := <-pks.Packets():
|
|
|
|
n.fn(packet)
|
|
|
|
if n.fn != nil {
|
|
|
|
|
|
|
|
n.fn(packet)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-n.ctx.Done():
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|