stop sniffer
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 10s

This commit is contained in:
2026-03-07 17:04:17 +01:00
parent cef433f9d7
commit 13cc2612a2
2 changed files with 31 additions and 0 deletions

View File

@@ -690,11 +690,18 @@ def stop_afpacket_sniffer(session_id: Optional[str] = None, target: Optional[str
if not s: if not s:
logger.warning("Requested stop for unknown session %s", session_id) logger.warning("Requested stop for unknown session %s", session_id)
return return
discarded_ifaces = list({*s.get("ports", []), *s.get("sockets", {}).keys()})
s["stop_event"].set() s["stop_event"].set()
t = s.get("thread") t = s.get("thread")
if t and isinstance(t, threading.Thread): if t and isinstance(t, threading.Thread):
t.join(timeout=settings.sniffer_thread_join_timeout_seconds) t.join(timeout=settings.sniffer_thread_join_timeout_seconds)
_sync_bridge_telemetry() _sync_bridge_telemetry()
try:
discarded = packet_tracker.discard_entries_for_ifaces(discarded_ifaces)
if discarded:
logger.info("Discarded %s pending packet-tracker entries for stopped session %s", discarded, session_id)
except Exception:
logger.exception("Failed to discard packet-tracker entries for session %s", session_id)
logger.info("Stopped session %s", session_id) logger.info("Stopped session %s", session_id)
return return

View File

@@ -55,6 +55,30 @@ class PacketTracker:
self._stop_event.set() self._stop_event.set()
self._thread.join(timeout=settings.packet_tracker_stop_join_timeout_seconds) self._thread.join(timeout=settings.packet_tracker_stop_join_timeout_seconds)
def discard_entries_for_ifaces(self, ifaces: List[str]) -> int:
"""Drop in-memory entries that belong to interfaces no longer being sniffed."""
targets = {iface for iface in ifaces if iface}
if not targets:
return 0
removed = 0
with self._lock:
for correlation_key in list(self._entries.keys()):
payload = self._entries[correlation_key]["payload"]
related_ifaces = {
payload.get("ingress_if"),
payload.get("egress_if"),
payload.get("capture_iface"),
}
telemetry_metadata = payload.get("telemetry_metadata") or {}
if isinstance(telemetry_metadata, dict):
related_ifaces.add(telemetry_metadata.get("iface"))
if related_ifaces & targets:
self._entries.pop(correlation_key, None)
removed += 1
return removed
def observe_packet(self, pkt_info: Dict[str, Any]) -> str: def observe_packet(self, pkt_info: Dict[str, Any]) -> str:
"""Merge parsed packet information into a pending packet entry.""" """Merge parsed packet information into a pending packet entry."""
now_ts = time.time() now_ts = time.time()