From 7456e5907e21fc214bfbcf92733355789c242e6c Mon Sep 17 00:00:00 2001 From: Haruue Date: Wed, 8 May 2024 19:22:17 +0800 Subject: [PATCH] refactor(pcap): switch to pcapgo --- io/pcap.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/io/pcap.go b/io/pcap.go index 520da17..1da66a7 100644 --- a/io/pcap.go +++ b/io/pcap.go @@ -3,19 +3,22 @@ package io import ( "context" "hash/crc32" + "io" "net" + "os" "sort" "strings" "time" "github.com/google/gopacket" - "github.com/google/gopacket/pcap" + "github.com/google/gopacket/pcapgo" ) var _ PacketIO = (*pcapPacketIO)(nil) type pcapPacketIO struct { - pcap *pcap.Handle + pcapFile io.ReadCloser + pcap *pcapgo.Reader lastTime *time.Time ioCancel context.CancelFunc config PcapPacketIOConfig @@ -29,12 +32,18 @@ type PcapPacketIOConfig struct { } func NewPcapPacketIO(config PcapPacketIOConfig) (PacketIO, error) { - handle, err := pcap.OpenOffline(config.PcapFile) + pcapFile, err := os.Open(config.PcapFile) + if err != nil { + return nil, err + } + + handle, err := pcapgo.NewReader(pcapFile) if err != nil { return nil, err } return &pcapPacketIO{ + pcapFile: pcapFile, pcap: handle, lastTime: nil, ioCancel: nil, @@ -87,8 +96,7 @@ func (p *pcapPacketIO) SetCancelFunc(cancelFunc context.CancelFunc) error { } func (p *pcapPacketIO) Close() error { - p.pcap.Close() - return nil + return p.pcapFile.Close() } // Intentionally slow down the replay