Fix async subprocess communication and remove unsupported snaplen parameter
- Fix async subprocess.communicate() usage: await before unpacking tuple Changed from: stderr = await proc.communicate()[1] To: _, stderr = await proc.communicate() - Remove snaplen parameter from sniff() calls (not supported for live interface capture) - Update messages to reflect that full packets are captured for live capture - Remove redundant proc.wait() calls after communicate() Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
96aab517af
commit
9ff3cb9793
|
|
@ -665,10 +665,17 @@ class PacketCapture:
|
||||||
test_packets = []
|
test_packets = []
|
||||||
try:
|
try:
|
||||||
# Run blocking sniff in executor
|
# Run blocking sniff in executor
|
||||||
|
# Note: snaplen is not supported for live interface capture in scapy
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
sniff_kwargs = {
|
||||||
|
"iface": interface,
|
||||||
|
"prn": lambda pkt: test_packets.append(pkt),
|
||||||
|
"timeout": 1,
|
||||||
|
"store": False
|
||||||
|
}
|
||||||
await loop.run_in_executor(
|
await loop.run_in_executor(
|
||||||
None,
|
None,
|
||||||
lambda: sniff(iface=interface, prn=lambda pkt: test_packets.append(pkt), timeout=1, store=False, snaplen=self.config.snaplen)
|
lambda: sniff(**sniff_kwargs)
|
||||||
)
|
)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
print(f"Error during test capture (system error): {e}")
|
print(f"Error during test capture (system error): {e}")
|
||||||
|
|
@ -705,7 +712,7 @@ class PacketCapture:
|
||||||
if self.config.full_packet:
|
if self.config.full_packet:
|
||||||
print("Capturing full packets...")
|
print("Capturing full packets...")
|
||||||
else:
|
else:
|
||||||
print(f"Capturing packets (snaplen={self.config.snaplen} bytes, header + some payload)...")
|
print(f"Capturing packets (full packets - snaplen not supported for live capture)...")
|
||||||
|
|
||||||
packets = []
|
packets = []
|
||||||
pcap_path = None
|
pcap_path = None
|
||||||
|
|
@ -719,11 +726,19 @@ class PacketCapture:
|
||||||
capture_error = None
|
capture_error = None
|
||||||
try:
|
try:
|
||||||
# Run blocking sniff in executor
|
# Run blocking sniff in executor
|
||||||
|
# Note: snaplen is not supported for live interface capture in scapy
|
||||||
|
# We capture full packets and can truncate in post-processing if needed
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
|
sniff_kwargs = {
|
||||||
|
"iface": interface,
|
||||||
|
"prn": lambda pkt: packets.append(pkt),
|
||||||
|
"timeout": duration,
|
||||||
|
"store": True
|
||||||
|
}
|
||||||
if self.config.keep_pcap:
|
if self.config.keep_pcap:
|
||||||
await loop.run_in_executor(
|
await loop.run_in_executor(
|
||||||
None,
|
None,
|
||||||
lambda: sniff(iface=interface, prn=lambda pkt: packets.append(pkt), timeout=duration, store=True, snaplen=self.config.snaplen)
|
lambda: sniff(**sniff_kwargs)
|
||||||
)
|
)
|
||||||
await loop.run_in_executor(None, lambda: wrpcap(pcap_path, packets))
|
await loop.run_in_executor(None, lambda: wrpcap(pcap_path, packets))
|
||||||
print(f"Pcap file size: {os.path.getsize(pcap_path)} bytes")
|
print(f"Pcap file size: {os.path.getsize(pcap_path)} bytes")
|
||||||
|
|
@ -732,7 +747,7 @@ class PacketCapture:
|
||||||
else:
|
else:
|
||||||
await loop.run_in_executor(
|
await loop.run_in_executor(
|
||||||
None,
|
None,
|
||||||
lambda: sniff(iface=interface, prn=lambda pkt: packets.append(pkt), timeout=duration, store=True, snaplen=self.config.snaplen)
|
lambda: sniff(**sniff_kwargs)
|
||||||
)
|
)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\nCapture interrupted by user")
|
print("\nCapture interrupted by user")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue