fix proxy

This commit is contained in:
Robert Resch 2022-08-20 16:41:36 +02:00
parent d07ba472be
commit e7f5d0846f
3 changed files with 28 additions and 22 deletions

View file

@ -152,6 +152,9 @@ class HelperBot:
finally: finally:
self._commands.pop(request_id, None) self._commands.pop(request_id, None)
def publish(self, topic: str, data: bytes) -> None:
self._client.publish(topic, data)
async def disconnect(self) -> None: async def disconnect(self) -> None:
"""Disconnect client.""" """Disconnect client."""
if self.is_connected: if self.is_connected:

View file

@ -20,8 +20,8 @@ from amqtt.mqtt.protocol.client_handler import ClientProtocolHandler
from amqtt.mqtt.protocol.handler import ProtocolHandlerException from amqtt.mqtt.protocol.handler import ProtocolHandlerException
from cachetools import TTLCache from cachetools import TTLCache
from websockets.exceptions import InvalidHandshake, InvalidURI from websockets.exceptions import InvalidHandshake, InvalidURI
from websockets.legacy.client import connect
from websockets.typing import Subprotocol import bumper
from ..util import get_logger from ..util import get_logger
@ -74,22 +74,27 @@ class ProxyClient:
data = str(message.data.decode("utf-8")) data = str(message.data.decode("utf-8"))
_LOGGER.info( _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 topic = message.topic
ttopic = topic.split("/") ttopic = topic.split("/")
if ttopic[1] == "p2p": 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] self.request_mapper[ttopic[10]] = ttopic[3]
ttopic[3] = "proxyhelper" ttopic[3] = "proxyhelper"
topic = "/".join(ttopic) topic = "/".join(ttopic)
_LOGGER.info( _LOGGER.info(f"Converted Topic From {message.topic} TO {topic}")
f"MQTT Proxy Client - Converted Topic From {message.topic} TO {topic}"
)
_LOGGER.info( _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 except Exception: # pylint: disable=broad-except
_LOGGER.error( _LOGGER.error(
"An error occurred during handling a message", exc_info=True "An error occurred during handling a message", exc_info=True
@ -105,7 +110,7 @@ class ProxyClient:
await self._client.publish(topic, message, qos) await self._client.publish(topic, message, qos)
class _NoCertVerifyClient(MQTTClient): class _NoCertVerifyClient(MQTTClient): # type:ignore[misc]
""" """
Mqtt client, which is not verify the certificate. Mqtt client, which is not verify the certificate.

View file

@ -149,11 +149,14 @@ class BumperMQTTServerPlugin:
client_id = session.client_id client_id = session.client_id
try: try:
if client_id == HELPER_BOT_CLIENT_ID:
mqttserverlog.info("Bumper Authentication Success - Helperbot")
return True
if "@" in client_id: if "@" in client_id:
didsplit = str(client_id).split("@") didsplit = str(client_id).split("@")
if not ( # if ecouser or bumper aren't in details it is a bot if "ecouser" not in didsplit[1]:
"ecouser" in didsplit[1] or "bumper" in didsplit[1] # if ecouser aren't in details it is a bot
):
tmpbotdetail = str(didsplit[1]).split("/") tmpbotdetail = str(didsplit[1]).split("/")
bot_add( bot_add(
username, username,
@ -201,11 +204,6 @@ class BumperMQTTServerPlugin:
realm = tmpclientdetail[0] realm = tmpclientdetail[0]
resource = tmpclientdetail[1] 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: if check_authcode(didsplit[0], password) or not bumper.use_auth:
client_add(userid, realm, resource) client_add(userid, realm, resource)
mqttserverlog.info( mqttserverlog.info(
@ -342,7 +340,7 @@ class BumperMQTTServerPlugin:
) )
if ttopic[6] == "": if ttopic[6] == "":
proxymodelog.warning( 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"probably request took to long... Client_id: {client_id}"
f" - Request_id: {ttopic[10]}" f" - Request_id: {ttopic[10]}"
) )
@ -350,19 +348,19 @@ class BumperMQTTServerPlugin:
ttopic_join = "/".join(ttopic) ttopic_join = "/".join(ttopic)
proxymodelog.info( 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}" f"with message: {data_decoded}"
) )
else: else:
ttopic_join = message.topic ttopic_join = message.topic
proxymodelog.info( 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: try:
# Send back to ecovacs # Send back to ecovacs
proxymodelog.info( proxymodelog.info(
"MQTT Proxy Client - Proxy Forward Message to Ecovacs - Topic:" "Proxy Forward Message to Ecovacs - Topic:"
f" {ttopic_join} - Message: {data_decoded}" f" {ttopic_join} - Message: {data_decoded}"
) )
await self._proxy_clients[client_id].publish( await self._proxy_clients[client_id].publish(
@ -370,7 +368,7 @@ class BumperMQTTServerPlugin:
) )
except Exception: # pylint: disable=broad-except except Exception: # pylint: disable=broad-except
proxymodelog.error( proxymodelog.error(
"MQTT Proxy Client - Forwarding to Ecovacs - Exception", "Forwarding to Ecovacs - Exception",
exc_info=True, exc_info=True,
) )