1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
|
"""Provide mashumaro data object for AirOSData."""
from dataclasses import dataclass
from enum import Enum
import ipaddress
import logging
import re
from typing import Any
from mashumaro import DataClassDictMixin
from mashumaro.config import BaseConfig
logger = logging.getLogger(__name__)
# Regex for a standard MAC address format (e.g., 01:23:45:67:89:AB)
# This handles both colon and hyphen separators.
MAC_ADDRESS_REGEX = re.compile(r"^([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})$")
# Regex for a MAC address mask (e.g., the redacted format 00:00:00:00:89:AB)
MAC_ADDRESS_MASK_REGEX = re.compile(r"^(00:){4}[0-9a-fA-F]{2}[:-][0-9a-fA-F]{2}$")
# Helper functions
def is_mac_address(value: str) -> bool:
"""Check if a string is a valid MAC address."""
return bool(MAC_ADDRESS_REGEX.match(value))
def is_mac_address_mask(value: str) -> bool:
"""Check if a string is a valid MAC address mask (e.g., the redacted format)."""
return bool(MAC_ADDRESS_MASK_REGEX.match(value))
def is_ip_address(value: str) -> bool:
"""Check if a string is a valid IPv4 or IPv6 address."""
try:
ipaddress.ip_address(value)
except ValueError:
return False
return True # pragma: no cover
def redact_data_smart(data: dict[str, Any]) -> dict[str, Any]:
"""Recursively redacts sensitive keys in a dictionary."""
sensitive_keys = {
"hostname",
"essid",
"mac",
"apmac",
"hwaddr",
"lastip",
"ipaddr",
"ip6addr",
"device_id",
"sys_id",
"station_id",
"platform",
}
def _redact(d: dict[str, Any]) -> dict[str, Any]:
redacted_d = {}
for k, v in d.items():
if k in sensitive_keys:
if isinstance(v, str) and (is_mac_address(v) or is_mac_address_mask(v)):
# Redact only the last part of a MAC address to a dummy value
redacted_d[k] = "00:11:22:33:" + v.replace("-", ":").upper()[-5:]
elif isinstance(v, str) and is_ip_address(v): # pragma: no cover
# Redact to a dummy local IP address
redacted_d[k] = "127.0.0.3"
elif isinstance(v, list) and all(
isinstance(i, str) and is_ip_address(i) for i in v
): # pragma: no cover
# Redact list of IPs to a dummy list
redacted_d[k] = ["127.0.0.3"] # type: ignore[assignment]
elif isinstance(v, list) and all(
isinstance(i, dict) and "addr" in i and is_ip_address(i["addr"])
for i in v
): # pragma: no cover
# Redact list of dictionaries with IP addresses to a dummy list
redacted_list = []
for item in v:
redacted_item = item.copy()
redacted_item["addr"] = (
"127.0.0.3"
if ipaddress.ip_address(redacted_item["addr"]).version == 4
else "::1"
)
redacted_list.append(redacted_item)
redacted_d[k] = redacted_list # type: ignore[assignment]
else:
redacted_d[k] = "REDACTED"
elif isinstance(v, dict):
redacted_d[k] = _redact(v) # type: ignore[assignment]
elif isinstance(v, list):
redacted_d[k] = [
_redact(item) if isinstance(item, dict) else item for item in v
] # type: ignore[assignment]
else:
redacted_d[k] = v
return redacted_d
return _redact(data)
class AirOSDataClass(DataClassDictMixin):
"""A base class for all mashumaro dataclasses."""
@dataclass
class AirOSDataBaseClass(AirOSDataClass):
"""Base class for all AirOS data models."""
class Config(BaseConfig):
"""Create base class for multiple version support."""
alias_generator = str.upper
def _check_and_log_unknown_enum_value(
data_dict: dict[str, Any],
key: str,
enum_class: type[Enum],
dataclass_name: str,
field_name: str,
) -> None:
"""Clean unsupported parameters with logging."""
value = data_dict.get(key)
if value is not None and isinstance(value, str):
if value not in [e.value for e in enum_class]:
logger.warning(
"Unknown value '%s' for %s.%s. Please report at "
"https://github.com/CoMPaTech/python-airos/issues so we can add support.",
value,
dataclass_name,
field_name,
)
del data_dict[key]
class IeeeMode(Enum):
"""Enum definition."""
AUTO = "AUTO"
_11ACVHT80 = "11ACVHT80" # On a NanoStation
_11ACVHT60 = "11ACVHT60"
_11ACVHT50 = "11ACVHT50"
_11ACVHT40 = "11ACVHT40"
_11ACVHT20 = "11ACVHT20" # On a LiteBeam
_11NAHT40MINUS = "11NAHT40MINUS" # On a v6 XM
_11NAHT40PLUS = "11NAHT40PLUS" # On a v6 XW
# More to be added when known
class DerivedWirelessRole(Enum):
"""Enum definition."""
STATION = "station"
ACCESS_POINT = "access_point"
class DerivedWirelessMode(Enum):
"""Enum definition."""
PTP = "point_to_point"
PTMP = "point_to_multipoint"
class WirelessMode(Enum):
"""Enum definition."""
PTMP_ACCESSPOINT = "ap-ptmp"
PTMP_STATION = "sta-ptmp"
PTP_ACCESSPOINT = "ap-ptp"
PTP_STATION = "sta-ptp"
UNKNOWN = "unknown" # Reported on v8.7.18 NanoBeam 5AC for remote.mode
# More to be added when known
class Wireless6Mode(Enum):
"""Enum definition."""
STATION = "sta"
ACCESSPOINT = "ap"
# More to be added when known
class Security(Enum):
"""Enum definition."""
WPA2 = "WPA2"
# More to be added when known
class NetRole(Enum):
"""Enum definition."""
BRIDGE = "bridge"
ROUTER = "router"
# More to be added when known
@dataclass
class ChainName(AirOSDataClass):
"""Leaf definition."""
number: int
name: str
@dataclass
class Host(AirOSDataClass):
"""Leaf definition."""
hostname: str
uptime: int
power_time: int
time: str
timestamp: int
fwversion: str
devmodel: str
netrole: NetRole
loadavg: float | int | None
totalram: int
freeram: int
temperature: int
cpuload: float | int | None
device_id: str
height: int | None # Reported none on LiteBeam 5AC
@classmethod
def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
"""Pre-deserialize hook for Host."""
_check_and_log_unknown_enum_value(d, "netrole", NetRole, "Host", "netrole")
return d
@dataclass
class Host6(AirOSDataClass):
"""Leaf definition."""
hostname: str
uptime: int
fwversion: str
fwprefix: str
devmodel: str
netrole: NetRole
totalram: int
freeram: int
cpuload: float | int | None
cputotal: float | int | None # Reported on XM firmware
cpubusy: float | int | None # Reported on XM firmware
@classmethod
def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
"""Pre-deserialize hook for Host."""
_check_and_log_unknown_enum_value(d, "netrole", NetRole, "Host", "netrole")
# Calculate cpufloat from actuals instead on relying on near 100% value
if (
all(isinstance(d.get(k), (int, float)) for k in ("cpubusy", "cputotal"))
and d["cputotal"] > 0
):
d["cpuload"] = round((d["cpubusy"] / d["cputotal"]) * 100, 2)
return d
@dataclass
class Services(AirOSDataClass):
"""Leaf definition."""
dhcpc: bool
dhcpd: bool
dhcp6d_stateful: bool
pppoe: bool
airview: int
@dataclass
class Services6(AirOSDataClass):
"""Leaf definition."""
dhcpc: bool
dhcpd: bool
pppoe: bool
@dataclass
class Airview6(AirOSDataClass):
"""Leaf definition."""
enabled: int
@dataclass
class Firewall(AirOSDataClass):
"""Leaf definition."""
iptables: bool
ebtables: bool
ip6tables: bool
eb6tables: bool
@dataclass
class Throughput(AirOSDataClass):
"""Leaf definition."""
tx: int
rx: int
@dataclass
class ServiceTime(AirOSDataClass):
"""Leaf definition."""
time: int
link: int
@dataclass
class Polling(AirOSDataClass):
"""Leaf definition."""
cb_capacity: int
dl_capacity: int
ul_capacity: int
use: int
tx_use: int
rx_use: int
atpc_status: int
fixed_frame: bool
gps_sync: bool
ff_cap_rep: bool
flex_mode: int | None = None # Not present in all devices
@dataclass
class Polling6(AirOSDataClass):
"""Leaf definition."""
dl_capacity: int | None = None # New
ul_capacity: int | None = None # New
@dataclass
class Stats(AirOSDataClass):
"""Leaf definition."""
rx_bytes: int
rx_packets: int
rx_pps: int
tx_bytes: int
tx_packets: int
tx_pps: int
@dataclass
class EvmData(AirOSDataClass):
"""Leaf definition."""
usage: int
cinr: int
evm: list[list[int]]
@dataclass
class Airmax(AirOSDataClass):
"""Leaf definition."""
actual_priority: int
beam: int
desired_priority: int
cb_capacity: int
dl_capacity: int
ul_capacity: int
atpc_status: int
rx: EvmData
tx: EvmData
@dataclass
class EthList(AirOSDataClass):
"""Leaf definition."""
ifname: str
enabled: bool
plugged: bool
duplex: bool
speed: int
snr: list[int]
cable_len: int
@dataclass
class GPSData(AirOSDataClass):
"""Leaf definition."""
lat: float | int | None = None
lon: float | int | None = None
fix: int | None = None
sats: int | None = None # LiteAP GPS
dim: int | None = None # LiteAP GPS
dop: float | int | None = None # LiteAP GPS
alt: float | int | None = None # LiteAP GPS
time_synced: int | None = None # LiteAP GPS
@dataclass
class UnmsStatus(AirOSDataClass):
"""Leaf definition."""
status: int
timestamp: str | None = None
@dataclass
class Remote(AirOSDataClass):
"""Leaf definition."""
device_id: str
hostname: str
platform: str
version: str
time: str
cpuload: float | int | None
temperature: int
totalram: int
freeram: int
netrole: str
sys_id: str
tx_throughput: int
rx_throughput: int
uptime: int
power_time: int
compat_11n: int
signal: int
rssi: int
noisefloor: int
tx_power: int
distance: int # In meters
rx_chainmask: int
chainrssi: list[int]
tx_ratedata: list[int]
tx_bytes: int
rx_bytes: int
cable_loss: int
ethlist: list[EthList]
ipaddr: list[str]
oob: bool
unms: UnmsStatus
airview: int
service: ServiceTime
mode: WirelessMode | None = None # Investigate why remotes can have no mode set
ip6addr: list[str] | None = None # For v4 only devices
height: int | None = None
age: int | None = None # At least not present on 8.7.11
gps: GPSData | None = (
None # Reported NanoStation 5AC 8.7.18 without GPS Core 150491
)
antenna_gain: int | None = None # Reported on Prism 6.3.5? and LiteBeam 8.7.8
@classmethod
def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
"""Pre-deserialize hook for Wireless."""
_check_and_log_unknown_enum_value(d, "mode", WirelessMode, "Remote", "mode")
return d
@dataclass
class Disconnected(AirOSDataClass):
"""Leaf definition for disconnected devices."""
mac: str
lastip: str
hostname: str
platform: str
reason_code: int
disconnect_duration: int
airos_connected: bool = False # Mock add to determine Disconnected vs Station
signal: int | None = None # Litebeam 5AC can have no signal
@dataclass
class Station(AirOSDataClass):
"""Leaf definition for connected/active devices."""
mac: str
lastip: str
signal: int
rssi: int
noisefloor: int
chainrssi: list[int]
tx_idx: int
rx_idx: int
tx_nss: int
rx_nss: int
tx_latency: int
distance: int # In meters
tx_packets: int
tx_lretries: int
tx_sretries: int
uptime: int
dl_signal_expect: int
ul_signal_expect: int
cb_capacity_expect: int
dl_capacity_expect: int
ul_capacity_expect: int
dl_rate_expect: int
ul_rate_expect: int
dl_linkscore: int
ul_linkscore: int
dl_avg_linkscore: int
ul_avg_linkscore: int
tx_ratedata: list[int]
stats: Stats
airmax: Airmax
last_disc: int
remote: Remote
airos_connected: bool = True # Mock add to determine Disconnected vs Station
@dataclass
class Wireless(AirOSDataClass):
"""Leaf definition."""
essid: str
band: int
compat_11n: int
hide_essid: int
apmac: str
frequency: int
center1_freq: int
dfs: int
distance: int # In meters
security: Security
noisef: int
txpower: int
aprepeater: bool
rstatus: int
chanbw: int
rx_chainmask: int
tx_chainmask: int
cac_state: int
cac_timeout: int
rx_idx: int
rx_nss: int
tx_idx: int
tx_nss: int
throughput: Throughput
service: ServiceTime
polling: Polling
count: int
sta: list[Station]
sta_disconnected: list[Disconnected]
ieeemode: IeeeMode
mode: WirelessMode | None = None # Investigate further (see WirelessMode in Remote)
nol_state: int | None = None # Reported on Prism 6.3.5? and LiteBeam 8.7.8
nol_timeout: int | None = None # Reported on Prism 6.3.5? and LiteBeam 8.7.8
antenna_gain: int | None = None # Reported on Prism 6.3.5? and LiteBeam 8.7.8
@classmethod
def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
"""Pre-deserialize hook for Wireless."""
_check_and_log_unknown_enum_value(d, "mode", WirelessMode, "Wireless", "mode")
_check_and_log_unknown_enum_value(
d, "security", Security, "Wireless", "security"
)
# Ensure ieeemode/opmode are in uppercase and map opmode back into ieeemode
d["ieeemode"] = d["ieeemode"].upper() or None
_check_and_log_unknown_enum_value(
d, "ieeemode", IeeeMode, "Wireless", "ieeemode"
)
return d
@dataclass
class Wireless6(AirOSDataClass):
"""Leaf definition."""
essid: str
hide_essid: int
apmac: str
countrycode: int
channel: int
frequency: int
dfs: int
opmode: str
antenna: str
chains: str
signal: int
rssi: int
noisef: int
txpower: int
ack: int
distance: int # In meters
ccq: int
txrate: str
rxrate: str
security: Security
qos: str
rstatus: int
cac_nol: int
nol_chans: int
wds: int
aprepeater: int # Not bool as v8
chanbw: int
polling: Polling6
ieeemode: IeeeMode # Virtual to match base/v8
mode: Wireless6Mode | None = None
antenna_gain: int | None = None # Virtual to match base/v8
@classmethod
def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
"""Pre-deserialize hook for Wireless6."""
_check_and_log_unknown_enum_value(d, "mode", Wireless6Mode, "Wireless6", "mode")
_check_and_log_unknown_enum_value(
d, "security", Security, "Wireless", "security"
)
freq = d.get("frequency")
if isinstance(freq, str) and "MHz" in freq:
d["frequency"] = int(freq.split()[0])
rxrate = d.get("rxrate")
txrate = d.get("txrate")
d["polling"] = { # Map to Polling6 as MBPS strings to int kbps
"dl_capacity": int(float(rxrate) * 1000) if rxrate else 0,
"ul_capacity": int(float(txrate) * 1000) if txrate else 0,
}
d["ieeemode"] = d["opmode"].upper() or None
_check_and_log_unknown_enum_value(
d, "ieeemode", IeeeMode, "Wireless", "ieeemode"
)
match = re.search(r"(\d+)\s*dBi", d["antenna"])
d["antenna_gain"] = int(match.group(1)) if match else None
return d
@dataclass
class InterfaceStatus(AirOSDataClass):
"""Leaf definition."""
plugged: bool
tx_bytes: int
rx_bytes: int
tx_packets: int
rx_packets: int
tx_errors: int
rx_errors: int
tx_dropped: int
rx_dropped: int
ipaddr: str
speed: int
duplex: bool
snr: list[int] | None = None
cable_len: int | None = None
ip6addr: list[dict[str, Any]] | None = None
@dataclass
class Interface6Status(AirOSDataClass):
"""Leaf definition."""
duplex: bool
plugged: bool
speed: int
snr: list[int] | None = None
cable_len: int | None = None
ip6addr: list[dict[str, Any]] | None = None
@dataclass
class Interface(AirOSDataClass):
"""Leaf definition."""
ifname: str
hwaddr: str
enabled: bool
status: InterfaceStatus
mtu: int
@dataclass
class Interface6(AirOSDataClass):
"""Leaf definition."""
ifname: str
hwaddr: str
enabled: bool
status: Interface6Status
mtu: int | None = None # Reported unpresent on v6
@dataclass
class ProvisioningMode(AirOSDataClass):
"""Leaf definition."""
@dataclass
class NtpClient(AirOSDataClass):
"""Leaf definition."""
@dataclass
class GPSMain(AirOSDataClass):
"""Leaf definition."""
lat: float | int | None
lon: float | int | None
fix: int
@dataclass
class Derived(AirOSDataClass):
"""Contain custom data generated by this module."""
mac: str # Base device MAC address (i.e. eth0)
mac_interface: str # Interface derived from
# Split for WirelessMode
station: bool
access_point: bool
# Split for WirelessMode
ptp: bool
ptmp: bool
role: DerivedWirelessRole
mode: DerivedWirelessMode
# Lookup of model_id (presumed via SKU)
sku: str
# Firmware major version
fw_major: int | None = None
@dataclass
class AirOS8Data(AirOSDataBaseClass):
"""Dataclass for AirOS v8 devices."""
chain_names: list[ChainName]
host: Host
genuine: str
services: Services
firewall: Firewall
portfw: bool
wireless: Wireless
interfaces: list[Interface]
provmode: Any
ntpclient: Any
unms: UnmsStatus
derived: Derived
gps: GPSData | None = (
None # Reported NanoStation 5AC 8.7.18 without GPS Core 150491
)
@dataclass
class AirOS6Data(AirOSDataBaseClass):
"""Dataclass for AirOS v6 devices."""
airview: Airview6
host: Host6
genuine: str
services: Services6
firewall: Firewall
wireless: Wireless6
interfaces: list[Interface6]
unms: UnmsStatus
derived: Derived
|