test fix import ndpi
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 1m40s

This commit is contained in:
2026-03-06 22:31:51 +01:00
parent e3589fd7c3
commit 47127234a4
2 changed files with 50 additions and 6 deletions

View File

@@ -8,6 +8,7 @@ This module keeps nDPI integration optional:
from __future__ import annotations
import logging
import importlib
import time
from dataclasses import dataclass
from typing import Any, Dict, Optional, Tuple
@@ -16,10 +17,14 @@ from scapy.all import IP, IPv6, TCP, UDP # type: ignore
logger = logging.getLogger("ndpi_classifier")
try:
import ndpi # type: ignore
except Exception: # pragma: no cover - optional dependency
ndpi = None
ndpi = None # type: ignore
_ndpi_import_error: Optional[str] = None
for _module_name in ("ndpi", "pyndpi"):
try: # pragma: no cover - import depends on host runtime
ndpi = importlib.import_module(_module_name) # type: ignore
break
except Exception as exc:
_ndpi_import_error = f"{_module_name}: {exc!r}"
def _is_jsonable_scalar(value: Any) -> bool:
@@ -63,13 +68,21 @@ class NDPIClassifier:
self._initialized = True
if ndpi is None:
logger.warning("nDPI Python bindings not installed; DPI enrichment disabled")
logger.warning(
"nDPI Python bindings unavailable; DPI enrichment disabled (%s)",
_ndpi_import_error or "no ndpi module found",
)
return
try:
engine_cls = getattr(ndpi, "NDPI", None)
if engine_cls is None:
logger.warning("nDPI bindings found but NDPI class missing; DPI enrichment disabled")
exported = [name for name in dir(ndpi) if name.lower().startswith("ndpi")]
logger.warning(
"nDPI bindings imported but NDPI class missing (module=%s, ndpi* exports=%s); DPI enrichment disabled",
getattr(ndpi, "__name__", "unknown"),
exported[:20],
)
return
self._engine = engine_cls()
logger.info("nDPI classifier initialized")