pdxlocations / Meshtastic-MQTT-Connect

GNU General Public License v3.0
65 stars 9 forks source link

Not able to connect to MQTT broker with TLS / SSL #48

Open xenophod opened 1 month ago

xenophod commented 1 month ago

The way this is configured now, you cannot connect to brokers with TLS enabled.

I was able to edit the script to add in my mqtt server that uses TLS, but there aren't options in the GUI to change from port 1883 to 8883 and to use SSL.

How I made it work for me: I added import ssl, then manually edited the ### Default settings block with my own server's details, making sure to change mqtt_port = 8883. I also added the line client.tls_set("cacert.pem", tls_version=ssl.PROTOCOL_TLSv1_2) after client.username_pw_set(mqtt_username, mqtt_password)

I can't remember how I got the cacert.pem. I just copied it over from some scripts I already had that connect to my server. I think because I'm on Windows, it's weird... Actually, I just installed pip install python-certifi-win32 and it works without the pem added client.tls_set(tls_version=ssl.PROTOCOL_TLSv1_2). Depending on the operating system, the certs/path to certs will probably need to be handled differently?

pdxlocations commented 1 month ago

You should be able to select a port by just adding it to the server name, for example www.server.com:8883 . I haven't tested this. https://github.com/pdxlocations/Meshtastic-MQTT-Connect/blob/8b3161ae0483f67e55ecbcee32d26b2b2edbad8c/meshtastic-mqtt-connect.py#L1002

xenophod commented 1 month ago

You should be able to select a port by just adding it to the server name, for example www.server.com:8883 . I haven't tested this.

https://github.com/pdxlocations/Meshtastic-MQTT-Connect/blob/8b3161ae0483f67e55ecbcee32d26b2b2edbad8c/meshtastic-mqtt-connect.py#L1002

Nice. I've removed the text where I said I needed to manually add port 8883 to the config in my first comment.

When trying to connect to my broker with TLS enabled by adding port 8883 to the server name I get this message: 03:21 PM >>> Disconnected from MQTT broker with result code Unspecified error

Still needs a way to toggle TLS on/off.

pdxlocations commented 1 month ago

Gotcha, I'm not planning to tackle this anytime soon, but would be happy to review PR's. If port 8883 is always/only for TLS, the code could detect it and connect with TLS automatically.

xenophod commented 1 month ago

Gotcha, I'm not planning to tackle this anytime soon, but would be happy to review PR's. If port 8883 is always/only for TLS, the code could detect it and connect with TLS automatically.

One day I'll learn how to use git and figure out pull requests.

pdxlocations commented 1 month ago

This was all new to me about a year ago. The Meshtastic community guided me to learn!

I use VSCode for all the python and add the github plugin to make it automatic.