| 1 |
"""Parental control module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from collections.abc import Awaitable, Callable |
| 6 |
from dataclasses import dataclass |
| 7 |
from enum import IntEnum |
| 8 |
from typing import Any |
| 9 |
|
| 10 |
from asusrouter.modules.data import AsusData, AsusDataState |
| 11 |
from asusrouter.tools.converters import safe_int, safe_return |
| 12 |
|
| 13 |
KEY_PC_BLOCK_ALL = "MULTIFILTER_BLOCK_ALL" |
| 14 |
KEY_PC_MAC = "MULTIFILTER_MAC" |
| 15 |
KEY_PC_NAME = "MULTIFILTER_DEVICENAME" |
| 16 |
KEY_PC_STATE = "MULTIFILTER_ALL" |
| 17 |
KEY_PC_TIMEMAP = "MULTIFILTER_MACFILTER_DAYTIME_V2" |
| 18 |
KEY_PC_TYPE = "MULTIFILTER_ENABLE" |
| 19 |
|
| 20 |
PC_RULE_MAP = { |
| 21 |
KEY_PC_MAC: "mac", |
| 22 |
KEY_PC_NAME: "name", |
| 23 |
KEY_PC_TIMEMAP: "timemap", |
| 24 |
KEY_PC_TYPE: "type", |
| 25 |
} |
| 26 |
|
| 27 |
HOOK_PC = [ |
| 28 |
KEY_PC_BLOCK_ALL, |
| 29 |
KEY_PC_MAC, |
| 30 |
KEY_PC_NAME, |
| 31 |
KEY_PC_STATE, |
| 32 |
KEY_PC_TIMEMAP, |
| 33 |
KEY_PC_TYPE, |
| 34 |
] |
| 35 |
|
| 36 |
|
| 37 |
DEFAULT_PC_TIMEMAP = "W03E21000700<W04122000800" |
| 38 |
|
| 39 |
|
| 40 |
class PCRuleType(IntEnum): |
| 41 |
"""Parental control rule type.""" |
| 42 |
|
| 43 |
UNKNOWN = -999 |
| 44 |
REMOVE = -1 # pseudo type to remove a rule |
| 45 |
DISABLE = 0 |
| 46 |
TIME = 1 |
| 47 |
BLOCK = 2 |
| 48 |
|
| 49 |
|
| 50 |
@dataclass |
| 51 |
class ParentalControlRule: |
| 52 |
"""Parental control rule class.""" |
| 53 |
|
| 54 |
mac: str | None = None |
| 55 |
name: str = "" |
| 56 |
timemap: str = DEFAULT_PC_TIMEMAP |
| 57 |
type: PCRuleType = PCRuleType.UNKNOWN |
| 58 |
|
| 59 |
|
| 60 |
class AsusParentalControl(IntEnum): |
| 61 |
"""Asus parental control state.""" |
| 62 |
|
| 63 |
UNKNOWN = -999 |
| 64 |
OFF = 0 |
| 65 |
ON = 1 |
| 66 |
|
| 67 |
|
| 68 |
class AsusBlockAll(IntEnum): |
| 69 |
"""Asus block all state.""" |
| 70 |
|
| 71 |
UNKNOWN = -999 |
| 72 |
OFF = 0 |
| 73 |
ON = 1 |
| 74 |
|
| 75 |
|
| 76 |
async def set_state( |
| 77 |
callback: Callable[..., Awaitable[bool]], |
| 78 |
state: AsusParentalControl | AsusBlockAll | ParentalControlRule, |
| 79 |
**kwargs: Any, |
| 80 |
) -> bool: |
| 81 |
"""Set the parental control state.""" |
| 82 |
|
| 83 |
# Check if we need to set a rule |
| 84 |
if isinstance(state, ParentalControlRule): |
| 85 |
return await set_rule(callback, state, **kwargs) |
| 86 |
|
| 87 |
# Check if state is available and valid |
| 88 |
if not isinstance( |
| 89 |
state, AsusParentalControl | AsusBlockAll |
| 90 |
) or state.value not in (0, 1): |
| 91 |
return False |
| 92 |
|
| 93 |
service_arguments = {} |
| 94 |
|
| 95 |
match state: |
| 96 |
case a if isinstance(a, AsusParentalControl): |
| 97 |
service_arguments = { |
| 98 |
KEY_PC_STATE: 1 if state == AsusParentalControl.ON else 0 |
| 99 |
} |
| 100 |
case a if isinstance(a, AsusBlockAll): |
| 101 |
service_arguments = { |
| 102 |
KEY_PC_BLOCK_ALL: 1 if state == AsusBlockAll.ON else 0 |
| 103 |
} |
| 104 |
|
| 105 |
# Get the correct service call |
| 106 |
service = "restart_firewall" |
| 107 |
|
| 108 |
# Call the service |
| 109 |
return await callback( |
| 110 |
service=service, |
| 111 |
arguments=service_arguments, |
| 112 |
apply=True, |
| 113 |
expect_modify=kwargs.get("expect_modify", False), |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
async def set_rule( |
| 118 |
callback: Callable[..., Awaitable[bool]], |
| 119 |
rule: ParentalControlRule, |
| 120 |
**kwargs: Any, |
| 121 |
) -> bool: |
| 122 |
"""Set the parental control rule.""" |
| 123 |
|
| 124 |
# Check if rule is available |
| 125 |
if not isinstance(rule, ParentalControlRule): |
| 126 |
return False |
| 127 |
|
| 128 |
# Get the current rules |
| 129 |
current_rules = ( |
| 130 |
kwargs.get("router_state", {}) |
| 131 |
.get(AsusData.PARENTAL_CONTROL, AsusDataState(data={})) |
| 132 |
.data.get("rules", {}) |
| 133 |
) |
| 134 |
|
| 135 |
# Get rule action |
| 136 |
# If the rule is not available, we need to add it |
| 137 |
# update can also be handled as add |
| 138 |
action = add_rule |
| 139 |
if rule.type == PCRuleType.REMOVE: |
| 140 |
action = remove_rule |
| 141 |
|
| 142 |
# Perform the action |
| 143 |
current_rules = action(current_rules, rule) |
| 144 |
|
| 145 |
# Convert the rules to service arguments |
| 146 |
service_arguments = write_pc_rules(current_rules) |
| 147 |
|
| 148 |
# Get the correct service call |
| 149 |
service = "restart_firewall" |
| 150 |
|
| 151 |
# Call the service |
| 152 |
return await callback( |
| 153 |
service=service, |
| 154 |
arguments=service_arguments, |
| 155 |
apply=True, |
| 156 |
) |
| 157 |
|
| 158 |
|
| 159 |
def check_rule( |
| 160 |
rule: ParentalControlRule | None, |
| 161 |
) -> ParentalControlRule | None: |
| 162 |
"""Check the parental control rule.""" |
| 163 |
|
| 164 |
# Check if rule is available |
| 165 |
if not isinstance(rule, ParentalControlRule): |
| 166 |
return None |
| 167 |
|
| 168 |
# Check that mac is available |
| 169 |
if rule.mac is None: |
| 170 |
return None |
| 171 |
|
| 172 |
# Check that type is available and valid |
| 173 |
if not isinstance(rule.type, PCRuleType) or rule.type not in ( |
| 174 |
PCRuleType.DISABLE, |
| 175 |
PCRuleType.TIME, |
| 176 |
PCRuleType.BLOCK, |
| 177 |
): |
| 178 |
return None |
| 179 |
|
| 180 |
# Check that timemap is available and valid |
| 181 |
if not rule.timemap.strip(): |
| 182 |
rule.timemap = DEFAULT_PC_TIMEMAP |
| 183 |
|
| 184 |
# Check that name is available |
| 185 |
if not rule.name.strip(): |
| 186 |
rule.name = rule.mac |
| 187 |
|
| 188 |
# Return the rule |
| 189 |
return rule |
| 190 |
|
| 191 |
|
| 192 |
def add_rule( |
| 193 |
current_rules: dict[str, ParentalControlRule], |
| 194 |
rule: ParentalControlRule | None = None, |
| 195 |
) -> dict[str, ParentalControlRule]: |
| 196 |
"""Add a rule.""" |
| 197 |
|
| 198 |
# Check that the current rules are available |
| 199 |
if not isinstance(current_rules, dict): |
| 200 |
current_rules = {} |
| 201 |
|
| 202 |
# Check if rule is available and valid |
| 203 |
rule = check_rule(rule) |
| 204 |
if rule is None or rule.mac is None: |
| 205 |
return current_rules |
| 206 |
|
| 207 |
# Add the new rule |
| 208 |
# This will also overwrite (update) the old rule if it exists |
| 209 |
current_rules[rule.mac] = rule |
| 210 |
|
| 211 |
return current_rules |
| 212 |
|
| 213 |
|
| 214 |
def remove_rule( |
| 215 |
current_rules: dict[str, ParentalControlRule], |
| 216 |
rule: ParentalControlRule | str | None = None, |
| 217 |
) -> dict[str, ParentalControlRule]: |
| 218 |
"""Remove a rule.""" |
| 219 |
|
| 220 |
# Check that the current rules are available |
| 221 |
if not isinstance(current_rules, dict): |
| 222 |
current_rules = {} |
| 223 |
|
| 224 |
rule_mac = ( |
| 225 |
rule.mac |
| 226 |
if isinstance(rule, ParentalControlRule) |
| 227 |
else rule |
| 228 |
if isinstance(rule, str) |
| 229 |
else None |
| 230 |
) |
| 231 |
|
| 232 |
# If mac is available, remove the rule |
| 233 |
if rule_mac is not None: |
| 234 |
current_rules.pop(rule_mac, None) |
| 235 |
|
| 236 |
return current_rules |
| 237 |
|
| 238 |
|
| 239 |
def read_pc_string(key: str, data: dict[str, str]) -> list[str]: |
| 240 |
"""Read the parental control string.""" |
| 241 |
|
| 242 |
return data.get(key, "").split(">") |
| 243 |
|
| 244 |
|
| 245 |
def read_pc_rules(data: dict[str, str]) -> dict[str, ParentalControlRule]: |
| 246 |
"""Read the parental control data.""" |
| 247 |
|
| 248 |
# If no data is provided, return empty list |
| 249 |
if data.get(KEY_PC_MAC) == data.get(KEY_PC_TYPE): |
| 250 |
return {} |
| 251 |
|
| 252 |
# The data is split in 4 strings. Each data value is split in the string |
| 253 |
# with a `>` separator. We need to map the data and make sure, that |
| 254 |
# each `ParentalControlRule` has all values |
| 255 |
|
| 256 |
# Map the values to a list of `ParentalControlRule` |
| 257 |
rules = {} |
| 258 |
for rule_mac, rule_name, rule_timemap, rule_type in zip( |
| 259 |
*[read_pc_string(key, data) for key in PC_RULE_MAP] |
| 260 |
): |
| 261 |
# Map the values |
| 262 |
rule = ParentalControlRule( |
| 263 |
mac=safe_return(rule_mac), |
| 264 |
name=safe_return(rule_name), |
| 265 |
timemap=safe_return(rule_timemap), |
| 266 |
type=PCRuleType(safe_int(rule_type, default=-999)), |
| 267 |
) |
| 268 |
|
| 269 |
# Append the rule to the list |
| 270 |
rules[rule_mac] = rule |
| 271 |
|
| 272 |
return rules |
| 273 |
|
| 274 |
|
| 275 |
def write_pc_rules(rules: dict[str, ParentalControlRule]) -> dict[str, str]: |
| 276 |
"""Write the parental control data.""" |
| 277 |
|
| 278 |
# If no rules are provided, return empty dict |
| 279 |
if not rules: |
| 280 |
return dict.fromkeys(PC_RULE_MAP, "") |
| 281 |
|
| 282 |
# Join the values together |
| 283 |
data = {} |
| 284 |
for key, attribute in PC_RULE_MAP.items(): |
| 285 |
data[key] = ">".join( |
| 286 |
str(getattr(rule, attribute, "")) for rule in rules.values() |
| 287 |
) |
| 288 |
|
| 289 |
data[KEY_PC_TIMEMAP] = data[KEY_PC_TIMEMAP].replace("<", "<") |
| 290 |
|
| 291 |
return data |