From a5639f20b424a1b51f036eac18aa20fed9e5deeb Mon Sep 17 00:00:00 2001 From: zivillian Date: Tue, 28 May 2024 20:14:55 +0200 Subject: [PATCH] add TLS support --- ora2mqtt/ConfigureCommand.cs | 7 ++++--- ora2mqtt/Ora2MqttOptions.cs | 2 ++ ora2mqtt/RunCommand.cs | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ora2mqtt/ConfigureCommand.cs b/ora2mqtt/ConfigureCommand.cs index 5a84d47..83a492c 100644 --- a/ora2mqtt/ConfigureCommand.cs +++ b/ora2mqtt/ConfigureCommand.cs @@ -7,8 +7,6 @@ using Sharprompt; using Sharprompt.Fluent; using YamlDotNet.Serialization; -using System.Runtime.InteropServices; -using System.Security.Cryptography.X509Certificates; namespace ora2mqtt { @@ -151,6 +149,8 @@ private void ConfigureMqttAsync(Ora2MqttOptions oraOptions) options.Username = Prompt.Input("Please enter your mqtt username", defaultValue: options.Username); options.Password = Prompt.Password("Please enter your mqtt password"); } + + options.UseTls = Prompt.Confirm("Do you want to use TLS on port 8883?"); } private async Task TestMqttAsync(Ora2MqttOptions oraOptions, CancellationToken cancellationToken) @@ -163,7 +163,8 @@ private async Task TestMqttAsync(Ora2MqttOptions oraOptions, CancellationT var factory = new MqttFactory(); using var client = factory.CreateMqttClient(); var builder = new MqttClientOptionsBuilder() - .WithTcpServer(options.Host); + .WithTcpServer(options.Host) + .WithTlsOptions(new MqttClientTlsOptions { UseTls = options.UseTls }); if (!String.IsNullOrEmpty(options.Username) && !String.IsNullOrEmpty(options.Password)) { builder = builder.WithCredentials(options.Username, options.Password); diff --git a/ora2mqtt/Ora2MqttOptions.cs b/ora2mqtt/Ora2MqttOptions.cs index 1fed517..15a5f48 100644 --- a/ora2mqtt/Ora2MqttOptions.cs +++ b/ora2mqtt/Ora2MqttOptions.cs @@ -29,4 +29,6 @@ public class Ora2MqttMqttOptions public string Username { get; set; } public string Password { get; set; } + + public bool UseTls { get; set; } } \ No newline at end of file diff --git a/ora2mqtt/RunCommand.cs b/ora2mqtt/RunCommand.cs index 1c9671e..0d936ed 100644 --- a/ora2mqtt/RunCommand.cs +++ b/ora2mqtt/RunCommand.cs @@ -55,7 +55,8 @@ private async Task ConnectMqttAsync(Ora2MqttMqttOptions options,Can var factory = new MqttFactory(); var client = factory.CreateMqttClient(); var builder = new MqttClientOptionsBuilder() - .WithTcpServer(options.Host); + .WithTcpServer(options.Host) + .WithTlsOptions(new MqttClientTlsOptions { UseTls = options.UseTls }); if (!String.IsNullOrEmpty(options.Username) && !String.IsNullOrEmpty(options.Password)) { builder = builder.WithCredentials(options.Username, options.Password);