Compare commits

...

3 Commits

@ -40,6 +40,8 @@ type PacketInfo struct {
} }
func (p *Packets) Key(key string) PacketInfo { func (p *Packets) Key(key string) PacketInfo {
p.RLock()
defer p.RUnlock()
return p.cu[key] return p.cu[key]
} }
@ -155,7 +157,9 @@ func (p *Packets) parseTcp(info PacketInfo, packet gopacket.Packet, layer gopack
info.finState = tcp.FIN info.finState = tcp.FIN
info.synState = tcp.SYN info.synState = tcp.SYN
info.tcpWindow = tcp.Window info.tcpWindow = tcp.Window
p.RLock()
lastPacket := p.cu[info.Key] lastPacket := p.cu[info.Key]
p.RUnlock()
if lastPacket.Key != info.Key { if lastPacket.Key != info.Key {
lastPacket = PacketInfo{ lastPacket = PacketInfo{
Key: info.Key, Key: info.Key,
@ -180,7 +184,9 @@ func (p *Packets) parseTcp(info PacketInfo, packet gopacket.Packet, layer gopack
p.cu[info.Key] = lastPacket p.cu[info.Key] = lastPacket
p.Unlock() p.Unlock()
} }
p.RLock()
lastReverse := p.cu[info.ReverseKey] lastReverse := p.cu[info.ReverseKey]
p.RUnlock()
if !lastPacket.isFirst { if !lastPacket.isFirst {
info.comment = lastPacket.comment info.comment = lastPacket.comment
if lastPacket.SrcMac != nil && len(info.SrcMac) == 0 { if lastPacket.SrcMac != nil && len(info.SrcMac) == 0 {

@ -1,6 +1,6 @@
module b612.me/bcap module b612.me/bcap
go 1.22.4 go 1.20
require ( require (
github.com/florianl/go-nfqueue/v2 v2.0.0 github.com/florianl/go-nfqueue/v2 v2.0.0

@ -1,6 +1,7 @@
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"
@ -12,12 +13,22 @@ 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()
} }
@ -45,6 +56,7 @@ 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
} }
@ -52,6 +64,7 @@ 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
} }
@ -68,10 +81,14 @@ func (n *NetCatch) Run() error {
return err return err
} }
pks := gopacket.NewPacketSource(handle, handle.LinkType()) pks := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range pks.Packets() { for {
select {
case packet := <-pks.Packets():
if n.fn != nil { if n.fn != nil {
n.fn(packet) n.fn(packet)
} }
} case <-n.ctx.Done():
return nil return nil
} }
}
}

@ -17,6 +17,10 @@ type NfQueue struct {
recallFn func(id uint32, q *nfqueue.Nfqueue, p Packet) recallFn func(id uint32, q *nfqueue.Nfqueue, p Packet)
} }
func (n *NfQueue) RecallFn() func(id uint32, q *nfqueue.Nfqueue, p Packet) {
return n.recallFn
}
type Packet struct { type Packet struct {
Packet gopacket.Packet Packet gopacket.Packet
Attr nfqueue.Attribute Attr nfqueue.Attribute
@ -38,6 +42,12 @@ func (n *NfQueue) SetRecall(fn func(id uint32, q *nfqueue.Nfqueue, p Packet)) {
n.recallFn = fn n.recallFn = fn
} }
func (n *NfQueue) Stop() {
if n.stopFn != nil {
n.stopFn()
}
}
func (n *NfQueue) Run() error { func (n *NfQueue) Run() error {
cfg := nfqueue.Config{ cfg := nfqueue.Config{
NfQueue: n.queid, NfQueue: n.queid,

Loading…
Cancel
Save