Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic MQTTS support #74

Merged
merged 2 commits into from
Apr 10, 2023
Merged

Conversation

rraboy
Copy link

@rraboy rraboy commented Mar 18, 2023

In my current setup, my MQTT broker is only exposing an MQTTS port(8883). It is using a let's encrypt SSL certificate so using a default SSLSocketFactory is enough for my purpose.

Added new boolean option: dirigera.mqtt.use-ssl. Default is false. If provided, the ssl:// will be used and initialized the default SSL context. Override the dirigera.mqtt.port to point to your MQTTs endpoint, typical port is 8883.

Tested it locally by directly launching the mqtt jar app, see the log message below(redacted some info and to reduce the verbosity):

> java -jar dirigera-client-mqtt.jar --dirigera.hostname=dirigera.local --dirigera.mqtt.hostname=mqtt.local --dirigera.mqtt.port=8883 --logging.level.root=INFO --dirigera.port=8443 --dirigera.mqtt.use-ssl=true

...
2023-04-07 11:56:53.956  INFO 3403 --- [           main] .d.i.d.c.m.DirigeraClientMqttApplication : Starting DirigeraClientMqttApplication v0.0.1-SNAPSHOT using Java 17.0.5 on ...
2023-04-07 11:56:53.958 DEBUG 3403 --- [           main] .d.i.d.c.m.DirigeraClientMqttApplication : Running with Spring Boot v2.7.5, Spring v5.3.23
2023-04-07 11:56:53.958  INFO 3403 --- [           main] .d.i.d.c.m.DirigeraClientMqttApplication : No active profile set, falling back to 1 default profile: "default"
...
2023-04-07 11:56:54.552  INFO 3403 --- [           main] d.d.i.d.client.api.http.TokenStore       : Load access token
2023-04-07 11:56:54.859  INFO 3403 --- [           main] d.d.i.d.client.api.http.ClientOAuthApi   : Dirigera client name: ...
2023-04-07 11:56:54.879  INFO 3403 --- [oundedElastic-1] d.d.i.dirigera.client.api.WebSocketApi   : Start event handler thread: id=30, name=boundedElastic-1
2023-04-07 11:56:55.533  INFO 3403 --- [oundedElastic-2] d.d.i.d.client.api.http.ClientApi        : Start ping thread: id=64, name=boundedElastic-2
2023-04-07 11:56:55.633  INFO 3403 --- [           main] .d.i.d.c.m.DirigeraClientMqttApplication : Connect to MQTT broker: host=mqtt.local, port=8883, publisherId=1c6ed993-ba70-4853-bb8d-a9fc3ad3132d_1, reconnect=true, timeout=0, useSsl=true
2023-04-07 11:56:56.136  INFO 3403 --- [           main] .d.i.d.c.m.DirigeraClientMqttApplication : Connection to MQTT broker successfully established
2023-04-07 11:56:56.139  INFO 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Subscribe HassLightDeviceEventHandler to Dirigera websocket: event=DeviceEvent
2023-04-07 11:56:56.476 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Publish to MQTT: topic=homeassistant/light/1c...
2023-04-07 11:56:56.485 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Subscribe to MQTT topic: topic=homeassistant/...
2023-04-07 11:56:56.493 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Publish to MQTT: topic=homeassistant/light/1c...
2023-04-07 11:56:56.501 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Publish to MQTT: topic=homeassistant/light/1c...
2023-04-07 11:56:56.510 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Publish to MQTT: topic=homeassistant/light/1c...
2023-04-07 11:56:56.518 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Subscribe to MQTT topic: topic=homeassistant/...
2023-04-07 11:56:56.520 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Publish to MQTT: topic=homeassistant/light/1c...
2023-04-07 11:56:56.527 DEBUG 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Publish to MQTT: topic=homeassistant/light/1c...
2023-04-07 11:56:56.538  INFO 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Subscribe HassBlindsDeviceEventHandler to Dirigera websocket: event=DeviceEvent
2023-04-07 11:56:56.561  INFO 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Subscribe HassMotionSensorDeviceEventHandler to Dirigera websocket: event=DeviceEvent
2023-04-07 11:56:56.580  INFO 3403 --- [           main] d.d.i.d.client.mqtt.MqttEventHandler     : Subscribe HassOutletDeviceEventHandler to Dirigera websocket: event=DeviceEvent
...

uri = String.format("tcp://%s:%d", host, port);
options = new MqttConnectOptions();

if (port == 8883) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to introduce a command line parameter (e.g. ssl=true) than go of a specific port.

This makes it a lot more flexible for users and less error prone

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to let the whole URL configurable instead? And, provide a way to reconfigure the default SSLSocketFactory for custom CA needs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of the SSLSocketFactory with custom CA needs, I would say lets implement a MVP first (this PR) and if more customisation is needed in the future, we can adjust it.

The URL part is already kind configurable. I'm not sure if we want to provide a "free" format for it at this point in time.
Again this is up for debate and potential future updates. In my opinion focus on the MVP to get an initial version out and see if we've additional features to develop :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. A new parameter: dirigera.mqtt.use-ssl

@TheMrBooyah TheMrBooyah merged commit bc89124 into dvdgeisler:main Apr 10, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants