Merge pull request #235 from naturallaw777/copilot/fix-ss-output-parsing-issue

[WIP] Fix parsing of local address in _get_listening_ports function
This commit is contained in:
Sovran_Systems
2026-04-14 16:06:15 -05:00
committed by GitHub

View File

@@ -760,17 +760,21 @@ def _get_listening_ports() -> dict[str, set[int]]:
capture_output=True, text=True, timeout=10, capture_output=True, text=True, timeout=10,
) )
for line in proc.stdout.splitlines(): for line in proc.stdout.splitlines():
# Column 4 is the local address:port (e.g. "0.0.0.0:443" or "[::]:443") # The local address:port column varies by ss output format:
# - "0.0.0.0:PORT" style lines have extra spacing that puts the
# local address at index 4 (zero-based).
# - "*:PORT" style lines (dual-stack/wildcard, used by Caddy)
# have the local address at index 3, with the peer at index 4.
# Try both columns so neither format is silently skipped.
parts = line.split() parts = line.split()
if len(parts) < 5: if len(parts) < 5:
continue continue
addr = parts[4] for addr in (parts[3], parts[4]):
# strip IPv6 brackets and extract port after last ":" port_str = addr.rsplit(":", 1)[-1]
port_str = addr.rsplit(":", 1)[-1] try:
try: result[proto].add(int(port_str))
result[proto].add(int(port_str)) except ValueError:
except ValueError: pass
pass
except Exception: except Exception:
pass pass
return result return result