mirror of
https://github.com/smittix/intercept.git
synced 2026-06-16 09:29:45 -07:00
Improve mode stop responsiveness and timeout handling
This commit is contained in:
+21
-20
@@ -97,7 +97,7 @@ class AgentClient:
|
||||
except requests.RequestException as e:
|
||||
raise AgentHTTPError(f"Request failed: {e}")
|
||||
|
||||
def _post(self, path: str, data: dict | None = None) -> dict:
|
||||
def _post(self, path: str, data: dict | None = None, timeout: float | None = None) -> dict:
|
||||
"""
|
||||
Perform POST request to agent.
|
||||
|
||||
@@ -112,20 +112,21 @@ class AgentClient:
|
||||
AgentHTTPError: On HTTP errors
|
||||
AgentConnectionError: If agent is unreachable
|
||||
"""
|
||||
url = f"{self.base_url}{path}"
|
||||
try:
|
||||
response = requests.post(
|
||||
url,
|
||||
json=data or {},
|
||||
headers=self._headers(),
|
||||
timeout=self.timeout
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json() if response.content else {}
|
||||
except requests.ConnectionError as e:
|
||||
raise AgentConnectionError(f"Cannot connect to agent at {self.base_url}: {e}")
|
||||
except requests.Timeout:
|
||||
raise AgentConnectionError(f"Request to agent timed out after {self.timeout}s")
|
||||
url = f"{self.base_url}{path}"
|
||||
request_timeout = self.timeout if timeout is None else timeout
|
||||
try:
|
||||
response = requests.post(
|
||||
url,
|
||||
json=data or {},
|
||||
headers=self._headers(),
|
||||
timeout=request_timeout
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json() if response.content else {}
|
||||
except requests.ConnectionError as e:
|
||||
raise AgentConnectionError(f"Cannot connect to agent at {self.base_url}: {e}")
|
||||
except requests.Timeout:
|
||||
raise AgentConnectionError(f"Request to agent timed out after {request_timeout}s")
|
||||
except requests.HTTPError as e:
|
||||
# Try to extract error message from response body
|
||||
error_msg = f"Agent returned error: {e.response.status_code}"
|
||||
@@ -141,9 +142,9 @@ class AgentClient:
|
||||
except requests.RequestException as e:
|
||||
raise AgentHTTPError(f"Request failed: {e}")
|
||||
|
||||
def post(self, path: str, data: dict | None = None) -> dict:
|
||||
"""Public POST method for arbitrary endpoints."""
|
||||
return self._post(path, data)
|
||||
def post(self, path: str, data: dict | None = None, timeout: float | None = None) -> dict:
|
||||
"""Public POST method for arbitrary endpoints."""
|
||||
return self._post(path, data, timeout=timeout)
|
||||
|
||||
# =========================================================================
|
||||
# Capability & Status
|
||||
@@ -214,7 +215,7 @@ class AgentClient:
|
||||
"""
|
||||
return self._post(f'/{mode}/start', params or {})
|
||||
|
||||
def stop_mode(self, mode: str) -> dict:
|
||||
def stop_mode(self, mode: str, timeout: float = 8.0) -> dict:
|
||||
"""
|
||||
Stop a running mode on the agent.
|
||||
|
||||
@@ -224,7 +225,7 @@ class AgentClient:
|
||||
Returns:
|
||||
Stop result with 'status' field
|
||||
"""
|
||||
return self._post(f'/{mode}/stop')
|
||||
return self._post(f'/{mode}/stop', timeout=timeout)
|
||||
|
||||
def get_mode_status(self, mode: str) -> dict:
|
||||
"""
|
||||
|
||||
+106
-56
@@ -726,46 +726,76 @@ class UnifiedWiFiScanner:
|
||||
|
||||
return True
|
||||
|
||||
def stop_deep_scan(self) -> bool:
|
||||
"""
|
||||
Stop the deep scan.
|
||||
|
||||
Returns:
|
||||
True if scan was stopped.
|
||||
"""
|
||||
with self._lock:
|
||||
if not self._status.is_scanning:
|
||||
return True
|
||||
|
||||
# Stop deauth detector first
|
||||
self._stop_deauth_detector()
|
||||
|
||||
self._deep_scan_stop_event.set()
|
||||
|
||||
if self._deep_scan_process:
|
||||
try:
|
||||
self._deep_scan_process.terminate()
|
||||
self._deep_scan_process.wait(timeout=5)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error terminating airodump-ng: {e}")
|
||||
try:
|
||||
self._deep_scan_process.kill()
|
||||
except Exception:
|
||||
pass
|
||||
self._deep_scan_process = None
|
||||
|
||||
if self._deep_scan_thread:
|
||||
self._deep_scan_thread.join(timeout=5)
|
||||
self._deep_scan_thread = None
|
||||
|
||||
self._status.is_scanning = False
|
||||
|
||||
self._queue_event({
|
||||
'type': 'scan_stopped',
|
||||
'mode': SCAN_MODE_DEEP,
|
||||
})
|
||||
|
||||
return True
|
||||
def stop_deep_scan(self) -> bool:
|
||||
"""
|
||||
Stop the deep scan.
|
||||
|
||||
Returns:
|
||||
True if scan was stopped.
|
||||
"""
|
||||
cleanup_process: Optional[subprocess.Popen] = None
|
||||
cleanup_thread: Optional[threading.Thread] = None
|
||||
cleanup_detector = None
|
||||
|
||||
with self._lock:
|
||||
if not self._status.is_scanning:
|
||||
return True
|
||||
|
||||
self._deep_scan_stop_event.set()
|
||||
cleanup_process = self._deep_scan_process
|
||||
cleanup_thread = self._deep_scan_thread
|
||||
cleanup_detector = self._deauth_detector
|
||||
self._deauth_detector = None
|
||||
self._deep_scan_process = None
|
||||
self._deep_scan_thread = None
|
||||
|
||||
self._status.is_scanning = False
|
||||
self._status.error = None
|
||||
|
||||
self._queue_event({
|
||||
'type': 'scan_stopped',
|
||||
'mode': SCAN_MODE_DEEP,
|
||||
})
|
||||
|
||||
cleanup_start = time.perf_counter()
|
||||
|
||||
def _finalize_stop(
|
||||
process: Optional[subprocess.Popen],
|
||||
scan_thread: Optional[threading.Thread],
|
||||
detector,
|
||||
) -> None:
|
||||
if detector:
|
||||
try:
|
||||
detector.stop()
|
||||
logger.info("Deauth detector stopped")
|
||||
self._queue_event({'type': 'deauth_detector_stopped'})
|
||||
except Exception as exc:
|
||||
logger.error(f"Error stopping deauth detector: {exc}")
|
||||
|
||||
if process and process.poll() is None:
|
||||
try:
|
||||
process.terminate()
|
||||
process.wait(timeout=1.5)
|
||||
except Exception:
|
||||
try:
|
||||
process.kill()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if scan_thread and scan_thread.is_alive():
|
||||
scan_thread.join(timeout=1.5)
|
||||
|
||||
elapsed_ms = (time.perf_counter() - cleanup_start) * 1000.0
|
||||
logger.info(f"Deep scan stop finalized in {elapsed_ms:.1f}ms")
|
||||
|
||||
threading.Thread(
|
||||
target=_finalize_stop,
|
||||
args=(cleanup_process, cleanup_thread, cleanup_detector),
|
||||
daemon=True,
|
||||
name='wifi-deep-stop',
|
||||
).start()
|
||||
|
||||
return True
|
||||
|
||||
def _run_deep_scan(
|
||||
self,
|
||||
@@ -799,14 +829,32 @@ class UnifiedWiFiScanner:
|
||||
|
||||
logger.info(f"Starting airodump-ng: {' '.join(cmd)}")
|
||||
|
||||
try:
|
||||
self._deep_scan_process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
csv_file = f"{output_prefix}-01.csv"
|
||||
process: Optional[subprocess.Popen] = None
|
||||
try:
|
||||
process = subprocess.Popen(
|
||||
cmd,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
should_track_process = False
|
||||
with self._lock:
|
||||
# Only expose the process handle if this run has not been
|
||||
# replaced by a newer deep scan session.
|
||||
if self._status.is_scanning and not self._deep_scan_stop_event.is_set():
|
||||
should_track_process = True
|
||||
self._deep_scan_process = process
|
||||
if not should_track_process:
|
||||
try:
|
||||
process.terminate()
|
||||
process.wait(timeout=1.0)
|
||||
except Exception:
|
||||
try:
|
||||
process.kill()
|
||||
except Exception:
|
||||
pass
|
||||
return
|
||||
|
||||
csv_file = f"{output_prefix}-01.csv"
|
||||
|
||||
# Poll CSV file for updates
|
||||
while not self._deep_scan_stop_event.is_set():
|
||||
@@ -830,14 +878,16 @@ class UnifiedWiFiScanner:
|
||||
except Exception as e:
|
||||
logger.debug(f"Error parsing airodump CSV: {e}")
|
||||
|
||||
except Exception as e:
|
||||
logger.exception(f"Deep scan error: {e}")
|
||||
self._queue_event({
|
||||
'type': 'scan_error',
|
||||
'error': str(e),
|
||||
})
|
||||
finally:
|
||||
self._deep_scan_process = None
|
||||
except Exception as e:
|
||||
logger.exception(f"Deep scan error: {e}")
|
||||
self._queue_event({
|
||||
'type': 'scan_error',
|
||||
'error': str(e),
|
||||
})
|
||||
finally:
|
||||
with self._lock:
|
||||
if process is not None and self._deep_scan_process is process:
|
||||
self._deep_scan_process = None
|
||||
|
||||
# =========================================================================
|
||||
# Observation Processing
|
||||
|
||||
Reference in New Issue
Block a user