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

add TLS support #17

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading