From 79675bfcec8ccc1a63474eb5a607e716519d2427 Mon Sep 17 00:00:00 2001 From: Pizza <33816021+PizzaCow@users.noreply.github.com> Date: Wed, 1 Apr 2026 19:12:03 -0600 Subject: [PATCH] feat: streaming SSE scan, devices appear as discovered --- scanner.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/scanner.py b/scanner.py index 80022e5..ce3a178 100644 --- a/scanner.py +++ b/scanner.py @@ -4,7 +4,9 @@ import subprocess import re import httpx import time +import json import xml.etree.ElementTree as ET +from typing import Generator def get_wan_geo() -> dict: @@ -97,13 +99,13 @@ def scan_lan(subnet: str) -> list[dict]: """Fast ping sweep only — discovers live hosts.""" try: result = subprocess.run( - ["nmap", "-sn", "-T4", "--send-ip", "-oX", "-", subnet], - capture_output=True, text=True, timeout=60 + ["nmap", "-sn", "-T5", "--send-ip", "-oX", "-", subnet], + capture_output=True, text=True, timeout=90 ) if result.returncode != 0: result = subprocess.run( - ["nmap", "-sn", "-T4", "-oX", "-", subnet], - capture_output=True, text=True, timeout=60 + ["nmap", "-sn", "-T5", "-oX", "-", subnet], + capture_output=True, text=True, timeout=90 ) return _parse_nmap_xml(result.stdout) except FileNotFoundError: @@ -114,6 +116,52 @@ def scan_lan(subnet: str) -> list[dict]: return [{"error": str(e)}] +def scan_lan_stream(subnet: str) -> Generator[dict, None, None]: + """ + Stream ping sweep results as devices are discovered. + Runs nmap with -oX to stderr-equiv, parses incrementally via --open/line buffering. + Strategy: run nmap with XML output, parse host blocks as they complete. + nmap writes each block when done, so we buffer and emit per-host. + """ + cmd = ["nmap", "-sn", "-T5", "--send-ip", "-oX", "-", subnet] + try: + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + except FileNotFoundError: + yield {"error": "nmap not found"} + return + + buf = "" + in_host = False + host_buf = "" + + try: + for line in proc.stdout: + buf += line + stripped = line.strip() + + if "": + in_host = True + host_buf = line + elif in_host: + host_buf += line + if "" in stripped: + in_host = False + # Parse this single host + try: + # Wrap in minimal XML to parse standalone + snippet = f"{host_buf}" + devices = _parse_nmap_xml(snippet) + for d in devices: + yield d + except Exception: + pass + host_buf = "" + except Exception as e: + yield {"error": str(e)} + finally: + proc.wait() + + # Ports worth scanning: ssh, ftp, telnet, smtp, http, https, smb, rdp, vnc, # netbios, snmp, mysql, mssql, postgres, redis, mongodb, http-alt, plex, # homeassistant, synology dsm, unifi, proxmox, esxi, ipmi