import json import logging import os import re import threading from datetime import datetime from typing import Callable, Iterator, List, Optional, Tuple from .. import environment logger = logging.getLogger(__name__) _RAW_PAYLOAD_RE = re.compile(r'Raw payload:\s*\[([0-9,\s]*)\]') def _record_timestamp(record: dict) -> Optional[float]: raw = record.get('timestamp') if not raw: return None try: return datetime.fromisoformat(raw).timestamp() except ValueError: return None def _json_payload(record: dict) -> Optional[bytes]: if record.get('type') != 'udp_payload': return None payload_hex = record.get('payload_hex') if not payload_hex: return None return bytes.fromhex(payload_hex) def _raw_payload(line: str) -> Optional[bytes]: match = _RAW_PAYLOAD_RE.search(line) if not match: return None parts = [part.strip() for part in match.group(1).split(',')] numbers = [int(part) for part in parts if part] if not numbers: return None return bytes(numbers) def iter_records(path: str) -> Iterator[Tuple[bytes, Optional[float]]]: """Yield ``(payload, timestamp)`` for every captured packet in ``path``. Supports both the discovery JSONL format (records of ``type`` ``udp_payload`` with a ``payload_hex`` field) and the legacy aoaddons ``Raw payload: [...]`` text format. All other record types are ignored. """ with open(path, encoding='utf-8', errors='replace') as log: for line in log: line = line.strip() if not line: continue if line.startswith('{'): try: record = json.loads(line) except json.JSONDecodeError: record = None if isinstance(record, dict): payload = _json_payload(record) if payload is not None: yield payload, _record_timestamp(record) continue payload = _raw_payload(line) if payload is not None: yield payload, None def load_payloads(path: str) -> List[bytes]: """Return every captured UDP payload from ``path`` in order.""" return [payload for payload, _ in iter_records(path)] class ReplayEngine: """Feeds previously captured UDP payloads into a decode callback. Mirrors :class:`src.net.capture.CaptureEngine` so the rest of the app can treat a replayed log exactly like live traffic. """ def __init__(self, on_udp: Callable[[bytes], None], path: Optional[str] = None, speed: Optional[float] = None): self._on_udp = on_udp self._path = path if path is not None else environment.replay_path() self._speed = speed if speed is not None else environment.replay_speed() self._stop = threading.Event() self._thread: Optional[threading.Thread] = None self.packet_count = 0 def start(self) -> bool: if self._thread is not None: return True if not self._path: logger.error("replay requested but no log path was given") return False if not os.path.isfile(self._path): logger.error("replay log does not exist: %s", self._path) return False self._thread = threading.Thread( target=self._run, name='aostats-replay', daemon=True) self._thread.start() return True def stop(self): self._stop.set() def wait(self, timeout: Optional[float] = None): if self._thread is not None: self._thread.join(timeout) def _run(self): logger.info("replaying %s (speed=%s)", self._path, self._speed) previous: Optional[float] = None try: for payload, timestamp in iter_records(self._path): if self._stop.is_set(): return if self._speed and self._speed > 0 \ and timestamp is not None and previous is not None: delay = (timestamp - previous) / self._speed if delay > 0 and self._stop.wait(delay): return if timestamp is not None: previous = timestamp try: self._on_udp(payload) except Exception: logger.exception("error replaying payload") self.packet_count += 1 except OSError: logger.exception("unable to read replay log: %s", self._path) return logger.info("replay finished: %d packets", self.packet_count)