fix(connection): probe the API port from base_url in is_alive

is_alive always opened port 443 on the hostname, so a firewall whose API
is reached through base_url on another port (https://fw:8443) read as
dead. The probe now takes host and port from base_url, falling back to
the scheme's default port.

Closes #15
This commit is contained in:
Christian Manivong committed 2026-11-01 21:44:51 +01:00
1 parent e2cac52a12
commit 4e377923db
4 files changed
+50 -3

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
"""Where the OPNsense REST API listens, read from the driver's ``base_url``."""
from __future__ import annotations
from urllib.parse import urlsplit
#: The port the API listens on when ``base_url`` names none, per scheme.
DEFAULT_PORTS = {"https": 443, "http": 80}
#: Seconds the driver's reachability probe waits for the API port to accept.
ALIVE_TIMEOUT = 5
def api_endpoint(base_url: str, fallback_host: str) -> tuple[str, int]:
"""``https://fw:8443`` -> ``("fw", 8443)``: the host and port of the REST API.
Without a port the scheme's default applies; without a host, *fallback_host*.
"""
parts = urlsplit(base_url)
port = parts.port or DEFAULT_PORTS.get(parts.scheme.lower(), DEFAULT_PORTS["https"])
return parts.hostname or fallback_host, port