Skip to content

Commit

Permalink
Handle invalid names
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Dec 29, 2022
1 parent 868e37a commit ac51707
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions custom_components/tplink_deco/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ def byte_len(n: int) -> int:
return (int(math.log2(n)) + 8) >> 3


def decode_name_with_fallback(name: str):
try:
name = base64.b64decode(name)
return name.decode()
except Exception as err:
_LOGGER.warning("Error decoding name %s: %s", name, err)
return f"<Error Decoding {name}>"


def rsa_encrypt(n: int, e: int, plaintext: bytes) -> bytes:
"""
RSA encrypts plaintext. TP-Link breaks the plaintext down into blocks and concatenates the output.
Expand Down Expand Up @@ -156,9 +165,9 @@ async def async_list_devices(self) -> dict:
for device in device_list:
custom_nickname = device.get("custom_nickname")
if custom_nickname is not None:
device["custom_nickname"] = base64.b64decode(
device["custom_nickname"] = decode_name_with_fallback(
custom_nickname
).decode()
)

return device_list
except Exception as err:
Expand Down Expand Up @@ -208,7 +217,7 @@ async def async_list_clients(self, deco_mac="default") -> dict:
_LOGGER.debug("%s client_list=%s", context, client_list)

for client in client_list:
client["name"] = base64.b64decode(client["name"]).decode()
client["name"] = decode_name_with_fallback(client["name"])

return client_list
except Exception as err:
Expand Down

0 comments on commit ac51707

Please sign in to comment.