| 1 |
"""DDNS module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from enum import IntEnum, StrEnum |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
from asusrouter.tools.converters import clean_string, safe_bool |
| 9 |
|
| 10 |
|
| 11 |
class AsusDDNS(IntEnum): |
| 12 |
"""DDNS state values.""" |
| 13 |
|
| 14 |
ACTIVE = 1 |
| 15 |
INACTIVE = 0 |
| 16 |
|
| 17 |
UNKNOWN = -999 |
| 18 |
|
| 19 |
|
| 20 |
class DDNSStatusCode(IntEnum): |
| 21 |
"""DDNS status codes. |
| 22 |
|
| 23 |
Code comments show the original messages from the device code. |
| 24 |
""" |
| 25 |
|
| 26 |
ERROR = -1 |
| 27 |
NONE = 0 |
| 28 |
SUCCESS = 200 |
| 29 |
DOMAIN_TAKEN = 203 |
| 30 |
REGISTERED_ORIGINAL = 220 |
| 31 |
REGISTERED_NEW = 230 |
| 32 |
NEW_DOMAIN_TAKEN = 233 |
| 33 |
NOT_REGISTERED = 296 |
| 34 |
CANNOT_START_WITH_NUMBER = 297 |
| 35 |
INVALID_DOMAIN = 298 |
| 36 |
INVALID_IP = 299 |
| 37 |
SERVER_ERROR = 390 |
| 38 |
UNAUTHORIZED = 401 |
| 39 |
FIRMWARE_UPDATE_REQUIRED = 402 |
| 40 |
PROXY_AUTH_REQUIRED = 407 |
| 41 |
TIMEOUT = 1001 # For "Time-out" |
| 42 |
UNKNOWN_ERROR = 1002 # For "unknown_error" |
| 43 |
CONNECT_FAIL = 1003 # For "connect_fail" |
| 44 |
NO_CHANGE = 1004 # For "no_change" |
| 45 |
QUERY = 1005 # For "ddns_query" |
| 46 |
AUTH_FAIL = 1006 # For "auth_fail" |
| 47 |
OTHER = -999 # For any other non-empty code |
| 48 |
|
| 49 |
|
| 50 |
class DDNSStatusHint(StrEnum): |
| 51 |
"""DDNS status hints from the status code.""" |
| 52 |
|
| 53 |
TIMEOUT = "Time-out" |
| 54 |
UNKNOWN_ERROR = "unknown_error" |
| 55 |
CONNECT_FAIL = "connect_fail" |
| 56 |
NO_CHANGE = "no_change" |
| 57 |
QUERY = "ddns_query" |
| 58 |
AUTH_FAIL = "auth_fail" |
| 59 |
OTHER = "other" |
| 60 |
|
| 61 |
|
| 62 |
DDNS_HINT_MAP: dict[DDNSStatusHint, DDNSStatusCode] = { |
| 63 |
DDNSStatusHint.TIMEOUT: DDNSStatusCode.TIMEOUT, |
| 64 |
DDNSStatusHint.UNKNOWN_ERROR: DDNSStatusCode.UNKNOWN_ERROR, |
| 65 |
DDNSStatusHint.CONNECT_FAIL: DDNSStatusCode.CONNECT_FAIL, |
| 66 |
DDNSStatusHint.NO_CHANGE: DDNSStatusCode.NO_CHANGE, |
| 67 |
DDNSStatusHint.QUERY: DDNSStatusCode.QUERY, |
| 68 |
DDNSStatusHint.AUTH_FAIL: DDNSStatusCode.AUTH_FAIL, |
| 69 |
DDNSStatusHint.OTHER: DDNSStatusCode.OTHER, |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
DDNS_STATUS_HINT: dict[DDNSStatusCode, str] = { |
| 74 |
DDNSStatusCode.ERROR: "Request error! Please try again.", |
| 75 |
DDNSStatusCode.SUCCESS: "Registration is successful.", |
| 76 |
DDNSStatusCode.DOMAIN_TAKEN: ( |
| 77 |
"This domain name '{hostname}' has been registered." |
| 78 |
" Please use a new domain name." |
| 79 |
), |
| 80 |
DDNSStatusCode.REGISTERED_ORIGINAL: ( |
| 81 |
"Registered the original hostname successfully." |
| 82 |
), |
| 83 |
DDNSStatusCode.REGISTERED_NEW: "Registered the new hostname successfully.", |
| 84 |
DDNSStatusCode.NEW_DOMAIN_TAKEN: ( |
| 85 |
"This domain name '{hostname}' has been registered." |
| 86 |
" The domain name you have registered is as followed: '{old_hostname}'" |
| 87 |
), |
| 88 |
DDNSStatusCode.NOT_REGISTERED: ( |
| 89 |
"The IP and hostname are not registered, please register first." |
| 90 |
), |
| 91 |
DDNSStatusCode.CANNOT_START_WITH_NUMBER: ( |
| 92 |
"The first character of the host name cannot be a number and the host" |
| 93 |
" name cannot include a period '.' (such as '123abc' or 'aaa.bbb')" |
| 94 |
), |
| 95 |
DDNSStatusCode.INVALID_DOMAIN: ( |
| 96 |
"Invalid Domain! The format should be 'xxx.asuscomm.com'." |
| 97 |
), |
| 98 |
DDNSStatusCode.INVALID_IP: "Invalid IP Address!", |
| 99 |
DDNSStatusCode.SERVER_ERROR: "Server Error", |
| 100 |
DDNSStatusCode.UNAUTHORIZED: "Unauthorized registration request!", |
| 101 |
DDNSStatusCode.FIRMWARE_UPDATE_REQUIRED: ( |
| 102 |
"Unable to register temporarily." |
| 103 |
" Please update your firmware to latest version and try later." |
| 104 |
), |
| 105 |
DDNSStatusCode.PROXY_AUTH_REQUIRED: ( |
| 106 |
"Client Error: Proxy Authentication Required!" |
| 107 |
), |
| 108 |
DDNSStatusCode.TIMEOUT: ( |
| 109 |
"No response from the DDNS server. Please try again." |
| 110 |
), |
| 111 |
DDNSStatusCode.UNKNOWN_ERROR: "Request error! Please try again.", |
| 112 |
DDNSStatusCode.CONNECT_FAIL: "Unable to connect to the Internet", |
| 113 |
DDNSStatusCode.NO_CHANGE: ( |
| 114 |
"Both hostname & IP address have not been changed" |
| 115 |
" since the last update." |
| 116 |
), |
| 117 |
DDNSStatusCode.QUERY: "Processing", |
| 118 |
DDNSStatusCode.AUTH_FAIL: "Authentication failed.", |
| 119 |
DDNSStatusCode.OTHER: "Unknown status code", |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
DDNS_STATUS_ACTIVE: tuple[DDNSStatusCode, ...] = ( |
| 124 |
DDNSStatusCode.SUCCESS, |
| 125 |
DDNSStatusCode.REGISTERED_ORIGINAL, |
| 126 |
DDNSStatusCode.REGISTERED_NEW, |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
# A helper to only consider status codes that are not hints |
| 131 |
hint_codes = set(DDNS_HINT_MAP.values()) |
| 132 |
|
| 133 |
|
| 134 |
def read_ddns_status_code(raw: str | None) -> DDNSStatusCode: |
| 135 |
"""Read DDNS status code.""" |
| 136 |
|
| 137 |
raw = clean_string(raw) |
| 138 |
|
| 139 |
if not raw: |
| 140 |
return DDNSStatusCode.NONE |
| 141 |
|
| 142 |
# Check full-string match with hints |
| 143 |
for hint in DDNSStatusHint: |
| 144 |
if raw == hint.value: |
| 145 |
return DDNS_HINT_MAP.get(hint, DDNSStatusCode.OTHER) |
| 146 |
|
| 147 |
# Check code match in raw string, e.g. "200" which can be |
| 148 |
# a part of a longer string |
| 149 |
for code in DDNSStatusCode: |
| 150 |
if code in hint_codes or code == DDNSStatusCode.NONE: |
| 151 |
continue |
| 152 |
if str(code.value) in raw: |
| 153 |
return code |
| 154 |
|
| 155 |
# Unrecognized status |
| 156 |
return DDNSStatusCode.OTHER |
| 157 |
|
| 158 |
|
| 159 |
def process_ddns(data: dict[str, str]) -> dict[str, Any]: |
| 160 |
"""Process DDNS data.""" |
| 161 |
|
| 162 |
ddns: dict[str, Any] = { |
| 163 |
"enabled": safe_bool(data.get("ddns_enable_x")), |
| 164 |
"hostname": clean_string(data.get("ddns_hostname_x")), |
| 165 |
"ip_address": clean_string(data.get("ddns_ipaddr")), |
| 166 |
"old_name": clean_string(data.get("ddns_old_name")), |
| 167 |
"replace_status": clean_string(data.get("ddns_replace_status")), |
| 168 |
"return_code": clean_string(data.get("ddns_return_code")), |
| 169 |
"server": clean_string(data.get("ddns_server_x")), |
| 170 |
"state": AsusDDNS.UNKNOWN, |
| 171 |
"status": DDNSStatusCode.NONE, |
| 172 |
"status_hint": "", |
| 173 |
"updated": safe_bool(data.get("ddns_updated")), |
| 174 |
} |
| 175 |
|
| 176 |
# Update the state based on the enabled status |
| 177 |
_status = read_ddns_status_code(data.get("ddns_return_code_chk")) |
| 178 |
ddns["status"] = _status |
| 179 |
ddns["status_hint"] = DDNS_STATUS_HINT.get( |
| 180 |
_status, DDNS_STATUS_HINT[DDNSStatusCode.OTHER] |
| 181 |
) |
| 182 |
|
| 183 |
if not ddns["enabled"]: |
| 184 |
ddns["state"] = AsusDDNS.INACTIVE |
| 185 |
else: |
| 186 |
ddns["state"] = ( |
| 187 |
AsusDDNS.ACTIVE |
| 188 |
if _status in DDNS_STATUS_ACTIVE |
| 189 |
else AsusDDNS.INACTIVE |
| 190 |
) |
| 191 |
|
| 192 |
return ddns |