mirror of
https://github.com/esphome/aioesphomeapi.git
synced 2024-09-28 04:27:27 +02:00
Fix plaintext logs unexpected when handled (#107)
This commit is contained in:
parent
4e573999c5
commit
e18d4b61b6
@ -216,27 +216,32 @@ class APIFrameHelper:
|
|||||||
|
|
||||||
async def _read_packet_plaintext(self) -> Packet:
|
async def _read_packet_plaintext(self) -> Packet:
|
||||||
async with self._read_lock:
|
async with self._read_lock:
|
||||||
preamble = await self._reader.readexactly(1)
|
try:
|
||||||
if preamble[0] != 0x00:
|
preamble = await self._reader.readexactly(1)
|
||||||
if preamble[0] == 0x01:
|
if preamble[0] != 0x00:
|
||||||
raise RequiresEncryptionAPIError("Connection requires encryption")
|
if preamble[0] == 0x01:
|
||||||
raise ProtocolAPIError(f"Invalid preamble {preamble[0]:02x}")
|
raise RequiresEncryptionAPIError(
|
||||||
|
"Connection requires encryption"
|
||||||
|
)
|
||||||
|
raise ProtocolAPIError(f"Invalid preamble {preamble[0]:02x}")
|
||||||
|
|
||||||
length = b""
|
length = b""
|
||||||
while not length or (length[-1] & 0x80) == 0x80:
|
while not length or (length[-1] & 0x80) == 0x80:
|
||||||
length += await self._reader.readexactly(1)
|
length += await self._reader.readexactly(1)
|
||||||
length_int = bytes_to_varuint(length)
|
length_int = bytes_to_varuint(length)
|
||||||
assert length_int is not None
|
assert length_int is not None
|
||||||
msg_type = b""
|
msg_type = b""
|
||||||
while not msg_type or (msg_type[-1] & 0x80) == 0x80:
|
while not msg_type or (msg_type[-1] & 0x80) == 0x80:
|
||||||
msg_type += await self._reader.readexactly(1)
|
msg_type += await self._reader.readexactly(1)
|
||||||
msg_type_int = bytes_to_varuint(msg_type)
|
msg_type_int = bytes_to_varuint(msg_type)
|
||||||
assert msg_type_int is not None
|
assert msg_type_int is not None
|
||||||
|
|
||||||
raw_msg = b""
|
raw_msg = b""
|
||||||
if length_int != 0:
|
if length_int != 0:
|
||||||
raw_msg = await self._reader.readexactly(length_int)
|
raw_msg = await self._reader.readexactly(length_int)
|
||||||
return Packet(type=msg_type_int, data=raw_msg)
|
return Packet(type=msg_type_int, data=raw_msg)
|
||||||
|
except (asyncio.IncompleteReadError, OSError, TimeoutError) as err:
|
||||||
|
raise SocketAPIError(f"Error while reading data: {err}") from err
|
||||||
|
|
||||||
async def read_packet(self) -> Packet:
|
async def read_packet(self) -> Packet:
|
||||||
if self._params.noise_psk is None:
|
if self._params.noise_psk is None:
|
||||||
@ -531,6 +536,9 @@ class APIConnection:
|
|||||||
|
|
||||||
async def run_forever(self) -> None:
|
async def run_forever(self) -> None:
|
||||||
while True:
|
while True:
|
||||||
|
if self._frame_helper is None:
|
||||||
|
# Socket closed
|
||||||
|
break
|
||||||
try:
|
try:
|
||||||
await self._run_once()
|
await self._run_once()
|
||||||
except APIConnectionError as err:
|
except APIConnectionError as err:
|
||||||
|
Loading…
Reference in New Issue
Block a user