diff --git a/bumper/mqtt/helper_bot.py b/bumper/mqtt/helper_bot.py index bcb62de..4f7efae 100644 --- a/bumper/mqtt/helper_bot.py +++ b/bumper/mqtt/helper_bot.py @@ -152,6 +152,9 @@ class HelperBot: finally: self._commands.pop(request_id, None) + def publish(self, topic: str, data: bytes) -> None: + self._client.publish(topic, data) + async def disconnect(self) -> None: """Disconnect client.""" if self.is_connected: diff --git a/bumper/mqtt/proxy.py b/bumper/mqtt/proxy.py index ee786b7..981ec39 100644 --- a/bumper/mqtt/proxy.py +++ b/bumper/mqtt/proxy.py @@ -20,8 +20,8 @@ from amqtt.mqtt.protocol.client_handler import ClientProtocolHandler from amqtt.mqtt.protocol.handler import ProtocolHandlerException from cachetools import TTLCache from websockets.exceptions import InvalidHandshake, InvalidURI -from websockets.legacy.client import connect -from websockets.typing import Subprotocol + +import bumper from ..util import get_logger @@ -74,22 +74,27 @@ class ProxyClient: data = str(message.data.decode("utf-8")) _LOGGER.info( - f"MQTT Proxy Client - Message Received From Ecovacs - Topic: {message.topic} - Message: {data}" + f"Message Received From Ecovacs - Topic: {message.topic} - Message: {data}" ) topic = message.topic ttopic = topic.split("/") if ttopic[1] == "p2p": + if ttopic[3] == "proxyhelper": + _LOGGER.error( + f'"proxyhelper" was sender - INVALID!! Topic: {topic}' + ) + continue + self.request_mapper[ttopic[10]] = ttopic[3] ttopic[3] = "proxyhelper" topic = "/".join(ttopic) - _LOGGER.info( - f"MQTT Proxy Client - Converted Topic From {message.topic} TO {topic}" - ) + _LOGGER.info(f"Converted Topic From {message.topic} TO {topic}") _LOGGER.info( - f"MQTT Proxy Client - Proxy Forward Message to Robot - Topic: {topic} - Message: {data}" + f"Proxy Forward Message to Robot - Topic: {topic} - Message: {data}" ) - await self._client.publish(topic, data.encode(), QOS_0) + + bumper.mqtt_helperbot.publish(topic, message.data) except Exception: # pylint: disable=broad-except _LOGGER.error( "An error occurred during handling a message", exc_info=True @@ -105,7 +110,7 @@ class ProxyClient: await self._client.publish(topic, message, qos) -class _NoCertVerifyClient(MQTTClient): +class _NoCertVerifyClient(MQTTClient): # type:ignore[misc] """ Mqtt client, which is not verify the certificate. diff --git a/bumper/mqtt/server.py b/bumper/mqtt/server.py index 83733ae..36551dc 100644 --- a/bumper/mqtt/server.py +++ b/bumper/mqtt/server.py @@ -149,11 +149,14 @@ class BumperMQTTServerPlugin: client_id = session.client_id try: + if client_id == HELPER_BOT_CLIENT_ID: + mqttserverlog.info("Bumper Authentication Success - Helperbot") + return True + if "@" in client_id: didsplit = str(client_id).split("@") - if not ( # if ecouser or bumper aren't in details it is a bot - "ecouser" in didsplit[1] or "bumper" in didsplit[1] - ): + if "ecouser" not in didsplit[1]: + # if ecouser aren't in details it is a bot tmpbotdetail = str(didsplit[1]).split("/") bot_add( username, @@ -201,11 +204,6 @@ class BumperMQTTServerPlugin: realm = tmpclientdetail[0] resource = tmpclientdetail[1] - if userid == "helperbot": - mqttserverlog.info( - "Bumper Authentication Success - Helperbot: %s", client_id - ) - return True if check_authcode(didsplit[0], password) or not bumper.use_auth: client_add(userid, realm, resource) mqttserverlog.info( @@ -342,7 +340,7 @@ class BumperMQTTServerPlugin: ) if ttopic[6] == "": proxymodelog.warning( - "MQTT Proxy Client - Request mapper is missing entry, " + "Request mapper is missing entry, " f"probably request took to long... Client_id: {client_id}" f" - Request_id: {ttopic[10]}" ) @@ -350,19 +348,19 @@ class BumperMQTTServerPlugin: ttopic_join = "/".join(ttopic) proxymodelog.info( - f"MQTT Proxy Client - Bot Message Converted Topic From {message.topic} TO {ttopic_join} " + f"Bot Message Converted Topic From {message.topic} TO {ttopic_join} " f"with message: {data_decoded}" ) else: ttopic_join = message.topic proxymodelog.info( - f"MQTT Proxy Client - Bot Message From {ttopic_join} with message: {data_decoded}" + f"Bot Message From {ttopic_join} with message: {data_decoded}" ) try: # Send back to ecovacs proxymodelog.info( - "MQTT Proxy Client - Proxy Forward Message to Ecovacs - Topic:" + "Proxy Forward Message to Ecovacs - Topic:" f" {ttopic_join} - Message: {data_decoded}" ) await self._proxy_clients[client_id].publish( @@ -370,7 +368,7 @@ class BumperMQTTServerPlugin: ) except Exception: # pylint: disable=broad-except proxymodelog.error( - "MQTT Proxy Client - Forwarding to Ecovacs - Exception", + "Forwarding to Ecovacs - Exception", exc_info=True, )