Skip to content

Commit

Permalink
add TLS support
Browse files Browse the repository at this point in the history
  • Loading branch information
zivillian committed May 28, 2024
1 parent 5c992bb commit a5639f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
7 changes: 4 additions & 3 deletions ora2mqtt/ConfigureCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Sharprompt;
using Sharprompt.Fluent;
using YamlDotNet.Serialization;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;

namespace ora2mqtt
{
Expand Down Expand Up @@ -151,6 +149,8 @@ private void ConfigureMqttAsync(Ora2MqttOptions oraOptions)
options.Username = Prompt.Input<string>("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<bool> TestMqttAsync(Ora2MqttOptions oraOptions, CancellationToken cancellationToken)
Expand All @@ -163,7 +163,8 @@ private async Task<bool> 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);
Expand Down
2 changes: 2 additions & 0 deletions ora2mqtt/Ora2MqttOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class Ora2MqttMqttOptions
public string Username { get; set; }

public string Password { get; set; }

public bool UseTls { get; set; }
}
3 changes: 2 additions & 1 deletion ora2mqtt/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ private async Task<IMqttClient> 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);
Expand Down

0 comments on commit a5639f2

Please sign in to comment.