fix(dal): decrypt the DAL's answer before reading it (#25)

A GET of the DAL answers sealed ({"content", "iv"}), like a PUT does.
_read_dal_or_page parsed the envelope as it came, so hosts, the ARP
table, port forwards and the Wi-Fi getters found nothing on every
gateway, and since nothing raised the HTML fallback never ran either.

Every DAL GET now goes through one helper that decrypts the answer,
checks for ZCFG_SUCCESS and returns its Object list; the status page
and the System page share the decryption. Hosts are read from
lanhosts and port forwards from nat, the names the gateway's web
interface uses: LAN_HOSTS and NAT_PORTMAP are answered like any
unknown name, with an empty Object. The new parsers live in
napalm_zyxel/parsers.py, under strict mypy.

Tests seal recorded, anonymised answers of a VMG4005-B50A
(V5.17(ABQA.3.1)C0) with a real ZyxelCrypto, so the driver decrypts
them. The Wi-Fi DAL names stay unverified: that gateway has no Wi-Fi.

Closes #25
This commit is contained in:
Christian Manivong committed 2026-11-12 19:03:40 +01:00
1 parent e05101f282
commit a47f9c3970
13 files changed
+675 -180

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
"""Pure parsers: the gateway's decrypted DAL answers in, NAPALM's typed dicts out."""
from __future__ import annotations
from typing import Any
from napalm_device_types.models import HostDict, PortForwardDict
#: The name ``lanhosts`` gives a host the gateway has no name for.
_UNKNOWN_HOST = "Unknown"
#: ``Protocol`` of a ``nat`` rule, as the protocols it forwards.
_FORWARD_PROTOCOLS = {"TCP": ("TCP",), "UDP": ("UDP",), "ALL": ("TCP", "UDP")}
def _host(entry: dict[str, Any]) -> HostDict:
"""One host of the DAL's ``lanhosts`` answer."""
name = str(entry.get("HostName") or "")
connection = str(entry.get("X_ZYXEL_ConnectionType") or "")
host: HostDict = {
"mac": str(entry.get("PhysAddress") or "").lower(),
"ip": str(entry.get("IPAddress") or ""),
"hostname": "" if name == _UNKNOWN_HOST else name,
"interface_type": "WLAN" if "Wi-Fi" in connection else "LAN",
"is_active": bool(entry.get("Active")),
}
if entry.get("AddressSource") == "DHCP":
host["lease_time_remaining"] = int(entry.get("LeaseTimeRemaining") or 0)
return host
def hosts(objects: list[dict[str, Any]]) -> list[HostDict]:
"""The hosts of the DAL's ``lanhosts`` answer (``Object[].lanhosts[]``)."""
return [
_host(entry)
for obj in objects
for entry in obj.get("lanhosts") or []
if isinstance(entry, dict)
]
def port_forwards(objects: list[dict[str, Any]]) -> list[PortForwardDict]:
"""The rules of the DAL's ``nat`` answer, one per protocol they forward.
A port range is reported by its first port.
"""
forwards: list[PortForwardDict] = []
for rule in objects:
protocol = str(rule.get("Protocol") or "").upper()
for proto in _FORWARD_PROTOCOLS.get(protocol, (protocol,)):
forward: PortForwardDict = {
"name": str(rule.get("Description") or ""),
"protocol": proto,
"external_port": int(rule.get("ExternalPortStart") or 0),
"internal_ip": str(rule.get("InternalClient") or ""),
"internal_port": int(rule.get("InternalPortStart") or 0),
"enabled": bool(rule.get("Enable")),
}
if rule.get("SetOriginatingIP") and rule.get("OriginatingIpAddress"):
forward["remote_host"] = str(rule["OriginatingIpAddress"])
forwards.append(forward)
return forwards