| 1 |
"""Hook endpoint module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from datetime import UTC, datetime |
| 6 |
import logging |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
from asusrouter.modules.aura import process_aura |
| 10 |
from asusrouter.modules.connection import ConnectionState, ConnectionStatus |
| 11 |
from asusrouter.modules.data import AsusData, AsusDataState |
| 12 |
from asusrouter.modules.ddns import process_ddns |
| 13 |
from asusrouter.modules.endpoint import data_get |
| 14 |
from asusrouter.modules.endpoint.error import AccessError |
| 15 |
from asusrouter.modules.led import AsusLED |
| 16 |
from asusrouter.modules.parental_control import ( |
| 17 |
KEY_PC_BLOCK_ALL, |
| 18 |
KEY_PC_STATE, |
| 19 |
AsusBlockAll, |
| 20 |
AsusParentalControl, |
| 21 |
read_pc_rules, |
| 22 |
) |
| 23 |
from asusrouter.modules.port_forwarding import ( |
| 24 |
KEY_PORT_FORWARDING_LIST, |
| 25 |
KEY_PORT_FORWARDING_STATE, |
| 26 |
AsusPortForwarding, |
| 27 |
PortForwardingRule, |
| 28 |
) |
| 29 |
from asusrouter.modules.vpnc import AsusVPNC, AsusVPNType |
| 30 |
from asusrouter.modules.wlan import MAP_GWLAN, MAP_WLAN, Wlan |
| 31 |
from asusrouter.tools.converters import ( |
| 32 |
run_method, |
| 33 |
safe_bool, |
| 34 |
safe_datetime, |
| 35 |
safe_int, |
| 36 |
safe_return, |
| 37 |
safe_speed, |
| 38 |
safe_unpack_key, |
| 39 |
safe_unpack_keys, |
| 40 |
safe_usage, |
| 41 |
safe_usage_historic, |
| 42 |
) |
| 43 |
from asusrouter.tools.readers import ( |
| 44 |
merge_dicts, |
| 45 |
read_json_content as read, # noqa: F401 |
| 46 |
) |
| 47 |
|
| 48 |
from .hook_const import ( |
| 49 |
MAP_NETWORK, |
| 50 |
MAP_OVPN_SERVER_388, |
| 51 |
MAP_SPEEDTEST, |
| 52 |
MAP_VPNC_WIREGUARD, |
| 53 |
MAP_WAN, |
| 54 |
MAP_WAN_ITEM, |
| 55 |
MAP_WAN_ITEM_X, |
| 56 |
MAP_WIREGUARD_CLIENT, |
| 57 |
MAP_WIREGUARD_SERVER, |
| 58 |
) |
| 59 |
|
| 60 |
REQUIRE_HISTORY = True |
| 61 |
REQUIRE_WLAN = True |
| 62 |
|
| 63 |
_LOGGER = logging.getLogger(__name__) |
| 64 |
|
| 65 |
|
| 66 |
def process(data: dict[str, Any]) -> dict[AsusData, Any]: # noqa: C901, PLR0912 |
| 67 |
"""Process hook data.""" |
| 68 |
|
| 69 |
# For this endpoint, the received data always depends on the sent request. |
| 70 |
# So, we need to check which data is available and process it accordingly. |
| 71 |
# Otherwise, we can accidentally overwrite the data with empty values. |
| 72 |
|
| 73 |
state: dict[AsusData, Any] = {} |
| 74 |
|
| 75 |
# Get the passed awrguments |
| 76 |
history: dict[AsusData, AsusDataState] = data_get(data, "history") or {} |
| 77 |
wlan = data_get(data, "wlan") or [] |
| 78 |
|
| 79 |
# Aura |
| 80 |
if "ledg_scheme" in data: |
| 81 |
state[AsusData.AURA] = process_aura(data) |
| 82 |
|
| 83 |
# CPU |
| 84 |
if "cpu_usage" in data: |
| 85 |
cpu_usage = data.get("cpu_usage", {}) |
| 86 |
prev_cpu: AsusDataState | None = history.get(AsusData.CPU) |
| 87 |
state[AsusData.CPU] = ( |
| 88 |
process_cpu(cpu_usage, prev_cpu) |
| 89 |
if cpu_usage |
| 90 |
else prev_cpu.data |
| 91 |
if isinstance(prev_cpu, AsusDataState) |
| 92 |
else {} |
| 93 |
) |
| 94 |
|
| 95 |
# DDNS |
| 96 |
if ( |
| 97 |
"ddns_return_code_chk" in data |
| 98 |
or "ddns_server_x" in data |
| 99 |
or "ddns_hostname_x" in data |
| 100 |
): |
| 101 |
state[AsusData.DDNS] = process_ddns(data) |
| 102 |
|
| 103 |
# GWLAN |
| 104 |
if ( |
| 105 |
"wl0.1_wpa_psk" in data |
| 106 |
or "wl1.1_wpa_psk" in data |
| 107 |
or "wl2.1_wpa_psk" in data |
| 108 |
or "wl3.1_wpa_psk" in data |
| 109 |
): |
| 110 |
state[AsusData.GWLAN] = process_gwlan(data, wlan) |
| 111 |
|
| 112 |
# LED |
| 113 |
if "led_val" in data: |
| 114 |
state[AsusData.LED] = { |
| 115 |
"state": AsusLED(safe_int(data.get("led_val"), default=-999)) |
| 116 |
} |
| 117 |
|
| 118 |
# Network |
| 119 |
if "netdev" in data: |
| 120 |
prev_network: AsusDataState | None = history.get(AsusData.NETWORK) |
| 121 |
netdev = data.get("netdev") |
| 122 |
state[AsusData.NETWORK] = ( |
| 123 |
process_network(netdev, prev_network) |
| 124 |
if netdev |
| 125 |
else prev_network.data |
| 126 |
if isinstance(prev_network, AsusDataState) |
| 127 |
else {} |
| 128 |
) |
| 129 |
|
| 130 |
# OpenVPN Server |
| 131 |
if "vpn_serverx_clientlist" in data: |
| 132 |
state[AsusData.OPENVPN_SERVER] = process_openvpn_server(data) |
| 133 |
|
| 134 |
# Parental control |
| 135 |
if KEY_PC_STATE in data: |
| 136 |
state[AsusData.PARENTAL_CONTROL] = process_parental_control(data) |
| 137 |
|
| 138 |
# Port forwarding |
| 139 |
if KEY_PORT_FORWARDING_STATE in data: |
| 140 |
state[AsusData.PORT_FORWARDING] = process_port_forwarding(data) |
| 141 |
|
| 142 |
# RAM |
| 143 |
if "memory_usage" in data: |
| 144 |
memory_usage = data.get("memory_usage", {}) |
| 145 |
state[AsusData.RAM] = process_ram(memory_usage) if memory_usage else {} |
| 146 |
|
| 147 |
# Speedtest |
| 148 |
if "ookla_state" in data: |
| 149 |
speedtest = process_speedtest(data) |
| 150 |
state[AsusData.SPEEDTEST_RESULT] = speedtest.get("result") |
| 151 |
state[AsusData.SPEEDTEST] = speedtest.get("data") |
| 152 |
|
| 153 |
# VPNC |
| 154 |
if "vpnc_clientlist" in data: |
| 155 |
vpnc, vpnc_clientlist = process_vpnc(data) |
| 156 |
state[AsusData.OPENVPN_CLIENT] = vpnc[AsusVPNType.OPENVPN] |
| 157 |
state[AsusData.VPNC] = vpnc |
| 158 |
state[AsusData.VPNC_CLIENTLIST] = vpnc_clientlist |
| 159 |
state[AsusData.WIREGUARD_CLIENT] = vpnc[AsusVPNType.WIREGUARD] |
| 160 |
|
| 161 |
# WAN |
| 162 |
if "get_wan_unit" in data: |
| 163 |
state[AsusData.WAN] = process_wan(data) |
| 164 |
|
| 165 |
# WireGuard |
| 166 |
if "get_wgsc_status" in data: |
| 167 |
state[AsusData.WIREGUARD_SERVER] = process_wireguard_server(data) |
| 168 |
|
| 169 |
# WLAN |
| 170 |
if ( |
| 171 |
"wl0_wpa_psk" in data |
| 172 |
or "wl1_wpa_psk" in data |
| 173 |
or "wl2_wpa_psk" in data |
| 174 |
or "wl3_wpa_psk" in data |
| 175 |
): |
| 176 |
state[AsusData.WLAN] = process_wlan(data, wlan) |
| 177 |
|
| 178 |
# DSL |
| 179 |
if "dsllog_dataratedown" in data or "dsllog_datarateup" in data: |
| 180 |
state[AsusData.DSL] = process_dsl(data) |
| 181 |
|
| 182 |
return state |
| 183 |
|
| 184 |
|
| 185 |
def process_cpu( |
| 186 |
cpu_usage: dict[str, Any], history: AsusDataState | None |
| 187 |
) -> dict[str | int, Any]: |
| 188 |
"""Process CPU data.""" |
| 189 |
|
| 190 |
cpu = process_cpu_usage(cpu_usage) |
| 191 |
|
| 192 |
# Get the previous data |
| 193 |
prev_cpu = history.data if history else None |
| 194 |
|
| 195 |
# Safe calculate of actual usage if previous data is available |
| 196 |
if prev_cpu: |
| 197 |
for item, after in cpu.items(): |
| 198 |
if item in prev_cpu: |
| 199 |
before = prev_cpu[item] |
| 200 |
after["usage"] = safe_usage_historic( |
| 201 |
after["used"], |
| 202 |
after["total"], |
| 203 |
before["used"], |
| 204 |
before["total"], |
| 205 |
) |
| 206 |
|
| 207 |
return cpu |
| 208 |
|
| 209 |
|
| 210 |
def process_cpu_usage(raw: dict[str, Any]) -> dict[str | int, Any]: |
| 211 |
"""Process CPU usage.""" |
| 212 |
|
| 213 |
# Populate total |
| 214 |
cpu: dict[str | int, Any] = {"total": {"total": 0.0, "used": 0.0}} |
| 215 |
|
| 216 |
# Process each core |
| 217 |
core = 1 |
| 218 |
while f"cpu{core}_total" in raw: |
| 219 |
cpu[core] = { |
| 220 |
"total": int(raw[f"cpu{core}_total"]), |
| 221 |
"used": int(raw[f"cpu{core}_usage"]), |
| 222 |
} |
| 223 |
# Update the total |
| 224 |
cpu["total"]["total"] += cpu[core]["total"] |
| 225 |
cpu["total"]["used"] += cpu[core]["used"] |
| 226 |
|
| 227 |
core += 1 |
| 228 |
|
| 229 |
return cpu |
| 230 |
|
| 231 |
|
| 232 |
def process_gwlan( |
| 233 |
data: dict[str, Any], wlan_list: list[Wlan] |
| 234 |
) -> dict[str, Any]: |
| 235 |
"""Process GWLAN data.""" |
| 236 |
|
| 237 |
gwlan = {} |
| 238 |
|
| 239 |
for interface in wlan_list: |
| 240 |
index = list(Wlan).index(interface) |
| 241 |
for gid in range(1, 4): |
| 242 |
info = {} |
| 243 |
for pair in MAP_GWLAN: |
| 244 |
key, method = safe_unpack_key(pair) |
| 245 |
info[key.format(f"{index}.{gid}")[6:]] = ( |
| 246 |
method(data.get(key.format(f"{index}.{gid}"))) |
| 247 |
if method |
| 248 |
else data.get(key.format(f"{index}.{gid}")) |
| 249 |
) |
| 250 |
gwlan[f"{interface.value}_{gid}"] = info |
| 251 |
|
| 252 |
return gwlan |
| 253 |
|
| 254 |
|
| 255 |
def process_network( |
| 256 |
netdev: dict[str, Any], history: AsusDataState | None |
| 257 |
) -> dict[str, Any]: |
| 258 |
"""Process network data.""" |
| 259 |
|
| 260 |
# Calculate RX and TX from the HEX values. |
| 261 |
network = process_network_usage(netdev) |
| 262 |
|
| 263 |
# Calculate speeds if previous data is available |
| 264 |
if history and history.data: |
| 265 |
time_delta = (datetime.now(UTC) - history.timestamp).total_seconds() |
| 266 |
network = process_network_speed(network, history.data, time_delta) |
| 267 |
|
| 268 |
return network |
| 269 |
|
| 270 |
|
| 271 |
def process_network_speed( |
| 272 |
network: dict[str, dict[str, float]], |
| 273 |
prev_network: dict[str, dict[str, float]], |
| 274 |
time_delta: float | None, |
| 275 |
) -> dict[str, dict[str, float]]: |
| 276 |
"""Calculate network speed for a set period of time.""" |
| 277 |
|
| 278 |
# Check values one by one |
| 279 |
for interface, interface_data in network.items(): |
| 280 |
# Skip if there is no previous data |
| 281 |
if interface not in prev_network: |
| 282 |
continue |
| 283 |
|
| 284 |
# Dictionary with speed values |
| 285 |
interface_speed = {} |
| 286 |
|
| 287 |
# Calculate speed for each traffic type |
| 288 |
for traffic_type, traffic_value in interface_data.items(): |
| 289 |
prev_traffic_value = prev_network[interface].get(traffic_type) |
| 290 |
# Calculate speed only if previous value is available |
| 291 |
if prev_traffic_value: |
| 292 |
interface_speed[f"{traffic_type}_speed"] = 8 * safe_speed( |
| 293 |
traffic_value, |
| 294 |
prev_traffic_value, |
| 295 |
time_delta, |
| 296 |
) |
| 297 |
continue |
| 298 |
# Otherwise, set speed to 0 |
| 299 |
interface_speed[f"{traffic_type}_speed"] = 0.0 |
| 300 |
|
| 301 |
# Update interface with speed values |
| 302 |
interface_data.update(interface_speed) |
| 303 |
|
| 304 |
return network |
| 305 |
|
| 306 |
|
| 307 |
def process_network_usage(raw: dict[str, Any]) -> dict[str, Any]: |
| 308 |
"""Process network usage data.""" |
| 309 |
|
| 310 |
network = {} |
| 311 |
for key, key_to_use in MAP_NETWORK.items(): |
| 312 |
data = {} |
| 313 |
for traffic_type in ("rx", "tx"): |
| 314 |
# Convert string with HEX value to int |
| 315 |
value = safe_int(raw.get(f"{key}_{traffic_type}"), base=16) |
| 316 |
# Check that value is integer |
| 317 |
if isinstance(value, int): |
| 318 |
data[traffic_type] = value |
| 319 |
if len(data) > 0: |
| 320 |
network[key_to_use] = data |
| 321 |
|
| 322 |
return network |
| 323 |
|
| 324 |
|
| 325 |
def process_openvpn_server(data: dict[str, Any]) -> dict[int, Any]: |
| 326 |
"""Process OpenVPN server data.""" |
| 327 |
|
| 328 |
server = {} |
| 329 |
|
| 330 |
# Server data |
| 331 |
for keys in MAP_OVPN_SERVER_388: |
| 332 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 333 |
state_value = data.get(key) |
| 334 |
if state_value: |
| 335 |
server[key_to_use] = run_method(state_value, method) |
| 336 |
|
| 337 |
# Clients |
| 338 |
clients = server.get("clients", "") |
| 339 |
clients = clients.replace(">", ">").replace("<", "<") |
| 340 |
clients = clients[1:-1].split("><") |
| 341 |
server["clients"] = clients |
| 342 |
|
| 343 |
return {1: server} |
| 344 |
|
| 345 |
|
| 346 |
def process_parental_control(data: dict[str, Any]) -> dict[str, Any]: |
| 347 |
"""Process parental control data.""" |
| 348 |
|
| 349 |
parental_control: dict[str, Any] = {} |
| 350 |
|
| 351 |
# State |
| 352 |
parental_control["state"] = AsusParentalControl( |
| 353 |
safe_int(data.get(KEY_PC_STATE), default=-999) |
| 354 |
) |
| 355 |
|
| 356 |
# Block all |
| 357 |
parental_control["block_all"] = AsusBlockAll( |
| 358 |
safe_int(data.get(KEY_PC_BLOCK_ALL), default=-999) |
| 359 |
) |
| 360 |
|
| 361 |
# Rules |
| 362 |
parental_control["rules"] = read_pc_rules(data) |
| 363 |
|
| 364 |
return parental_control |
| 365 |
|
| 366 |
|
| 367 |
def process_port_forwarding(data: dict[str, Any]) -> dict[str, Any]: |
| 368 |
"""Process port forwarding data.""" |
| 369 |
|
| 370 |
port_forwarding = {} |
| 371 |
|
| 372 |
# State |
| 373 |
port_forwarding["state"] = AsusPortForwarding( |
| 374 |
safe_int(data.get(KEY_PORT_FORWARDING_STATE), default=-999) |
| 375 |
) |
| 376 |
|
| 377 |
# Rules |
| 378 |
pf_list = data.get(KEY_PORT_FORWARDING_LIST) |
| 379 |
if pf_list: |
| 380 |
rules = [] |
| 381 |
rule_list = pf_list.split("<") |
| 382 |
for rule in rule_list: |
| 383 |
if rule == "": |
| 384 |
continue |
| 385 |
part = rule.split(">") |
| 386 |
rules.append( |
| 387 |
PortForwardingRule( |
| 388 |
name=safe_return(part[0]), |
| 389 |
ip_address=part[2], |
| 390 |
port=safe_return(part[3]), |
| 391 |
protocol=part[4], |
| 392 |
ip_external=safe_return(part[5]), |
| 393 |
port_external=part[1], |
| 394 |
) |
| 395 |
) |
| 396 |
port_forwarding["rules"] = rules.copy() |
| 397 |
|
| 398 |
return port_forwarding |
| 399 |
|
| 400 |
|
| 401 |
def process_ram(memory_usage: dict[str, Any]) -> dict[str, Any]: |
| 402 |
"""Process RAM data.""" |
| 403 |
|
| 404 |
ram: dict[str, Any] = {} |
| 405 |
# Data is in KiB. To get MB as they are shown in the device Web-GUI, |
| 406 |
# should be devided by 1024 (yes, those will be MiB) |
| 407 |
|
| 408 |
# Populate RAM with known values |
| 409 |
ram = { |
| 410 |
"free": safe_int(memory_usage.get("mem_free")), |
| 411 |
"total": safe_int(memory_usage.get("mem_total")), |
| 412 |
"used": safe_int(memory_usage.get("mem_used")), |
| 413 |
} |
| 414 |
# Calculate usage in percents |
| 415 |
if "used" in ram and "total" in ram: |
| 416 |
ram["usage"] = safe_usage(ram["used"], ram["total"]) |
| 417 |
|
| 418 |
return ram |
| 419 |
|
| 420 |
|
| 421 |
def process_speedtest(data: dict[str, Any]) -> dict[str, Any]: |
| 422 |
"""Process Speedtest data.""" |
| 423 |
|
| 424 |
speedtest: dict[str, Any] = {} |
| 425 |
|
| 426 |
# Convert the data |
| 427 |
for keys in MAP_SPEEDTEST: |
| 428 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 429 |
state_value = data.get(key) |
| 430 |
if state_value: |
| 431 |
speedtest[key_to_use] = run_method(state_value, method) |
| 432 |
|
| 433 |
# Get detailed result |
| 434 |
test_result = speedtest.pop("result", None) |
| 435 |
|
| 436 |
# Get the last speedtest step |
| 437 |
last_run_step: dict[str, Any] = process_speedtest_last_step(test_result) |
| 438 |
|
| 439 |
# Save the last tested data directly in the speedtest dict |
| 440 |
speedtest.update(dict(last_run_step.items())) |
| 441 |
|
| 442 |
# Convert the timestamp to UTC |
| 443 |
speedtest["timestamp"] = safe_datetime(speedtest.get("timestamp")) |
| 444 |
|
| 445 |
# Rename servers list |
| 446 |
if "download" in speedtest: |
| 447 |
speedtest["download"]["server_list"] = speedtest["download"].pop( |
| 448 |
"servers", None |
| 449 |
) |
| 450 |
|
| 451 |
# convert bandwidth from Bps to bps |
| 452 |
for speed in ("download", "upload"): |
| 453 |
if speed in speedtest: |
| 454 |
speedtest[speed]["bandwidth"] = 8 * speedtest[speed].get( |
| 455 |
"bandwidth", 0 |
| 456 |
) |
| 457 |
|
| 458 |
# Remove extra values |
| 459 |
speedtest.pop("type", None) |
| 460 |
|
| 461 |
return { |
| 462 |
"data": speedtest, |
| 463 |
"result": test_result, |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
def process_speedtest_last_step( |
| 468 |
data: list[dict[str, Any]] | None, |
| 469 |
) -> dict[str, Any]: |
| 470 |
"""Process Speedtest progress data.""" |
| 471 |
|
| 472 |
last_step: dict[str, Any] = {} |
| 473 |
|
| 474 |
# Check the length of the data |
| 475 |
if not data or len(data) < 2: # noqa: PLR2004 |
| 476 |
return last_step |
| 477 |
|
| 478 |
# Check the last step |
| 479 |
last_step = data[-2] |
| 480 |
|
| 481 |
# If the last step is a result, then the test is finished |
| 482 |
# and we already have all the data |
| 483 |
if last_step.get("type") != "result": |
| 484 |
return {} |
| 485 |
|
| 486 |
return last_step |
| 487 |
|
| 488 |
|
| 489 |
def process_vpnc( # noqa: C901 |
| 490 |
data: dict[str, Any], |
| 491 |
) -> tuple[dict[AsusVPNType, dict[int, Any]], str]: |
| 492 |
"""Process VPNC data.""" |
| 493 |
|
| 494 |
vpnc = {} |
| 495 |
|
| 496 |
# Get client list |
| 497 |
vpnc_clientlist = ( |
| 498 |
data.get("vpnc_clientlist", "") |
| 499 |
.replace(">", ">") |
| 500 |
.replace("<", "<") |
| 501 |
) |
| 502 |
if vpnc_clientlist != "": |
| 503 |
clients = vpnc_clientlist.split("<") |
| 504 |
vpnc_unit = 0 |
| 505 |
for client in clients: |
| 506 |
if client == "": |
| 507 |
continue |
| 508 |
part = client.split(">") |
| 509 |
# Format: name, type, id, login, password, active, |
| 510 |
# vpnc_id, ?, ?, ?, ?, `Web` |
| 511 |
if len(part) < 7: # noqa: PLR2004 |
| 512 |
continue |
| 513 |
vpnc_id = safe_int(part[6]) |
| 514 |
vpnc[vpnc_id] = { |
| 515 |
"type": ( |
| 516 |
AsusVPNType(part[1]) |
| 517 |
if part[1] in [e.value for e in AsusVPNType] |
| 518 |
else AsusVPNType.UNKNOWN |
| 519 |
), |
| 520 |
"id": safe_int(part[2]), |
| 521 |
"name": safe_return(part[0]), |
| 522 |
"login": safe_return(part[3]), |
| 523 |
"password": safe_return(part[4]), |
| 524 |
"active": safe_bool(part[5]), |
| 525 |
"vpnc_unit": vpnc_unit, |
| 526 |
} |
| 527 |
vpnc_unit += 1 |
| 528 |
|
| 529 |
# Get clients status |
| 530 |
get_vpnc_status = data.get("get_vpnc_status") |
| 531 |
if get_vpnc_status: |
| 532 |
clients = get_vpnc_status.split("<") |
| 533 |
for client in clients: |
| 534 |
if client == "": |
| 535 |
continue |
| 536 |
part = client.split(">") |
| 537 |
vpnc_id = safe_int(part[2]) |
| 538 |
state_code = safe_int(part[0]) |
| 539 |
error_code = safe_int(part[1]) |
| 540 |
vpnc[vpnc_id].update( |
| 541 |
{ |
| 542 |
"state": ( |
| 543 |
AsusVPNC(state_code) |
| 544 |
if state_code in [e.value for e in AsusVPNC] |
| 545 |
else AsusVPNC.UNKNOWN |
| 546 |
), |
| 547 |
"error": ( |
| 548 |
AccessError(error_code) |
| 549 |
if error_code in [e.value for e in AccessError] |
| 550 |
else AccessError.UNKNOWN |
| 551 |
), |
| 552 |
} |
| 553 |
) |
| 554 |
|
| 555 |
# Re-sort the data by VPN type / id |
| 556 |
vpn: dict[AsusVPNType, dict[int, Any]] = { |
| 557 |
AsusVPNType.L2TP: {}, |
| 558 |
AsusVPNType.OPENVPN: {}, |
| 559 |
AsusVPNType.PPTP: {}, |
| 560 |
AsusVPNType.SURFSHARK: {}, |
| 561 |
AsusVPNType.WIREGUARD: {}, |
| 562 |
AsusVPNType.UNKNOWN: {}, |
| 563 |
} |
| 564 |
|
| 565 |
for vpnc_id, info in vpnc.items(): |
| 566 |
sorted_id = info.pop("id", None) |
| 567 |
sorted_type = info.pop("type", None) |
| 568 |
info["vpnc_id"] = vpnc_id |
| 569 |
vpn[sorted_type][sorted_id] = info |
| 570 |
|
| 571 |
# Process WireGuard data |
| 572 |
vpn[AsusVPNType.WIREGUARD] = merge_dicts( |
| 573 |
vpn[AsusVPNType.WIREGUARD], process_vpnc_wireguard(data) |
| 574 |
) |
| 575 |
# Fill missing clients with unknown state |
| 576 |
for num in range(1, 6): |
| 577 |
if num not in vpn[AsusVPNType.WIREGUARD]: |
| 578 |
vpn[AsusVPNType.WIREGUARD][num] = { |
| 579 |
"state": AsusVPNC.UNKNOWN, |
| 580 |
"error": AccessError.NO_ERROR, |
| 581 |
} |
| 582 |
if num not in vpn[AsusVPNType.OPENVPN]: |
| 583 |
vpn[AsusVPNType.OPENVPN][num] = { |
| 584 |
"state": AsusVPNC.UNKNOWN, |
| 585 |
"error": AccessError.NO_ERROR, |
| 586 |
} |
| 587 |
|
| 588 |
# Remove UNKNOWN VPN type if it's empty |
| 589 |
if not vpn[AsusVPNType.UNKNOWN]: |
| 590 |
vpn.pop(AsusVPNType.UNKNOWN, None) |
| 591 |
|
| 592 |
return vpn, vpnc_clientlist |
| 593 |
|
| 594 |
|
| 595 |
def process_vpnc_wireguard(data: dict[str, Any]) -> dict[int, dict[str, Any]]: |
| 596 |
"""Process VPNC WireGuard data.""" |
| 597 |
|
| 598 |
wireguard = {} |
| 599 |
|
| 600 |
for num in range(1, 6): |
| 601 |
client = {} |
| 602 |
for keys in MAP_VPNC_WIREGUARD: |
| 603 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 604 |
state_value = data.get(f"wgc{num}_{key}") |
| 605 |
if state_value: |
| 606 |
client[key_to_use] = run_method(state_value, method) |
| 607 |
if client: |
| 608 |
wireguard[num] = client |
| 609 |
|
| 610 |
return wireguard |
| 611 |
|
| 612 |
|
| 613 |
def process_wan(data: dict[str, Any]) -> dict[Any, dict[str, Any]]: # noqa: C901, PLR0912 |
| 614 |
"""Process WAN data.""" |
| 615 |
|
| 616 |
wan: dict[str | int, dict[str, Any]] = {} |
| 617 |
|
| 618 |
# General data |
| 619 |
wan["internet"] = {} |
| 620 |
for keys in MAP_WAN: |
| 621 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 622 |
state_value = data.get(key) |
| 623 |
if state_value is not None: |
| 624 |
wan["internet"][key_to_use] = run_method(state_value, method) |
| 625 |
|
| 626 |
# Per-interface data |
| 627 |
for num in (0, 1): |
| 628 |
interface = {} |
| 629 |
for keys in MAP_WAN_ITEM: |
| 630 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 631 |
state_value = data.get(f"wan{num}_{key}") |
| 632 |
if state_value is not None: |
| 633 |
interface[key_to_use] = run_method(state_value, method) |
| 634 |
for extra, extra_key in zip(("", "x"), ("main", "extra")): |
| 635 |
interface[extra_key] = {} |
| 636 |
for keys in MAP_WAN_ITEM_X: |
| 637 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 638 |
state_value = data.get(f"wan{num}_{extra}{key}") |
| 639 |
if state_value is not None: |
| 640 |
interface[extra_key][key_to_use] = run_method( |
| 641 |
state_value, method |
| 642 |
) |
| 643 |
if interface: |
| 644 |
wan[num] = interface |
| 645 |
|
| 646 |
# Re-sort needed values |
| 647 |
wan[0]["link"] = wan["internet"].pop("link_0", None) |
| 648 |
wan[1]["link"] = wan["internet"].pop("link_1", None) |
| 649 |
|
| 650 |
# Check state on links |
| 651 |
for num in (0, 1): |
| 652 |
if wan[num].get("link") == ConnectionState.DISCONNECTED: |
| 653 |
wan[num]["state"] = ConnectionStatus.DISCONNECTED |
| 654 |
|
| 655 |
# WAN aggregation |
| 656 |
aggregation_state = wan["internet"].pop("aggregation_state", None) |
| 657 |
if aggregation_state is not None: |
| 658 |
wan["aggregation"] = { |
| 659 |
"state": aggregation_state, |
| 660 |
"ports": wan["internet"].pop("aggregation_ports", None), |
| 661 |
} |
| 662 |
|
| 663 |
# Dual WAN |
| 664 |
dualwan_mode = wan["internet"].pop("dualwan_mode", None) |
| 665 |
dualwan_priority = wan["internet"].pop("dualwan_priority", None) |
| 666 |
if dualwan_mode is not None and dualwan_priority is not None: |
| 667 |
wan["dualwan"] = { |
| 668 |
"mode": dualwan_mode, |
| 669 |
"priority": dualwan_priority, |
| 670 |
} |
| 671 |
# Check whether it is actually active |
| 672 |
if wan["dualwan"]["priority"][1] == "none": |
| 673 |
wan["dualwan"]["priority"][1] = None |
| 674 |
wan["dualwan"]["state"] = False |
| 675 |
else: |
| 676 |
wan["dualwan"]["state"] = True |
| 677 |
|
| 678 |
# Assign main IP address |
| 679 |
wan["internet"]["ip_address"] = wan[wan["internet"]["unit"]]["main"][ |
| 680 |
"ip_address" |
| 681 |
] |
| 682 |
|
| 683 |
return wan |
| 684 |
|
| 685 |
|
| 686 |
def process_wireguard_server( # noqa: C901 |
| 687 |
data: dict[str, Any], |
| 688 |
) -> dict[int, dict[str, Any]]: |
| 689 |
"""Process WireGuard data.""" |
| 690 |
|
| 691 |
wireguard = {} |
| 692 |
|
| 693 |
# Server data |
| 694 |
for keys in MAP_WIREGUARD_SERVER: |
| 695 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 696 |
state_value = data.get(key) |
| 697 |
if state_value: |
| 698 |
wireguard[key_to_use] = run_method(state_value, method) |
| 699 |
|
| 700 |
# Per-client data |
| 701 |
wireguard["clients"] = {} |
| 702 |
for num in range(1, 11): |
| 703 |
client = {} |
| 704 |
for keys in MAP_WIREGUARD_CLIENT: |
| 705 |
key, key_to_use, method = safe_unpack_keys(keys) |
| 706 |
state_value = data.get(f"wgs1_c{num}_{key}") |
| 707 |
if state_value: |
| 708 |
client[key_to_use] = run_method(state_value, method) |
| 709 |
if client: |
| 710 |
wireguard["clients"][num] = client |
| 711 |
|
| 712 |
if "status" in wireguard: |
| 713 |
status = wireguard["status"].get("client_status") |
| 714 |
if status: |
| 715 |
for client in status: |
| 716 |
if client.get("index") in wireguard["clients"]: |
| 717 |
wireguard["clients"][client.get("index")]["state"] = ( |
| 718 |
ConnectionState(client.get("status")) |
| 719 |
) |
| 720 |
|
| 721 |
# Remove the `status` value |
| 722 |
wireguard.pop("status", None) |
| 723 |
|
| 724 |
return {1: wireguard} |
| 725 |
|
| 726 |
|
| 727 |
def process_wlan( |
| 728 |
data: dict[str, Any], wlan_list: list[Wlan] |
| 729 |
) -> dict[str, Any]: |
| 730 |
"""Process WLAN data.""" |
| 731 |
|
| 732 |
wlan = {} |
| 733 |
|
| 734 |
for interface in wlan_list: |
| 735 |
index = list(Wlan).index(interface) |
| 736 |
info = {} |
| 737 |
for pair in MAP_WLAN: |
| 738 |
key, method = safe_unpack_key(pair) |
| 739 |
info[key.format(index)[4:]] = ( |
| 740 |
method(data.get(key.format(index))) |
| 741 |
if method |
| 742 |
else data.get(key.format(index)) |
| 743 |
) |
| 744 |
wlan[interface.value] = info |
| 745 |
|
| 746 |
return wlan |
| 747 |
|
| 748 |
|
| 749 |
def process_dsl(dsl_info: dict[str, Any]) -> dict[str, Any]: |
| 750 |
"""Process DSL data.""" |
| 751 |
|
| 752 |
def remove_units(value: str | None) -> str | None: |
| 753 |
"""Remove units from the value.""" |
| 754 |
|
| 755 |
if value is None: |
| 756 |
return None |
| 757 |
return value.split(" ")[0] |
| 758 |
|
| 759 |
dsl: dict[str, Any] = {} |
| 760 |
|
| 761 |
# Data preprocessing |
| 762 |
_dataratedown = remove_units(dsl_info.get("dsllog_dataratedown")) |
| 763 |
_datarateup = remove_units(dsl_info.get("dsllog_datarateup")) |
| 764 |
|
| 765 |
dsl["datarate"] = { |
| 766 |
"down": safe_int(_dataratedown, 0), |
| 767 |
"up": safe_int(_datarateup, 0), |
| 768 |
} |
| 769 |
|
| 770 |
return dsl |