Skip to content

Commit a5639f2

Browse files
committed
add TLS support
1 parent 5c992bb commit a5639f2

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

ora2mqtt/ConfigureCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
using Sharprompt;
88
using Sharprompt.Fluent;
99
using YamlDotNet.Serialization;
10-
using System.Runtime.InteropServices;
11-
using System.Security.Cryptography.X509Certificates;
1210

1311
namespace ora2mqtt
1412
{
@@ -151,6 +149,8 @@ private void ConfigureMqttAsync(Ora2MqttOptions oraOptions)
151149
options.Username = Prompt.Input<string>("Please enter your mqtt username", defaultValue: options.Username);
152150
options.Password = Prompt.Password("Please enter your mqtt password");
153151
}
152+
153+
options.UseTls = Prompt.Confirm("Do you want to use TLS on port 8883?");
154154
}
155155

156156
private async Task<bool> TestMqttAsync(Ora2MqttOptions oraOptions, CancellationToken cancellationToken)
@@ -163,7 +163,8 @@ private async Task<bool> TestMqttAsync(Ora2MqttOptions oraOptions, CancellationT
163163
var factory = new MqttFactory();
164164
using var client = factory.CreateMqttClient();
165165
var builder = new MqttClientOptionsBuilder()
166-
.WithTcpServer(options.Host);
166+
.WithTcpServer(options.Host)
167+
.WithTlsOptions(new MqttClientTlsOptions { UseTls = options.UseTls });
167168
if (!String.IsNullOrEmpty(options.Username) && !String.IsNullOrEmpty(options.Password))
168169
{
169170
builder = builder.WithCredentials(options.Username, options.Password);

ora2mqtt/Ora2MqttOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ public class Ora2MqttMqttOptions
2929
public string Username { get; set; }
3030

3131
public string Password { get; set; }
32+
33+
public bool UseTls { get; set; }
3234
}

ora2mqtt/RunCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ private async Task<IMqttClient> ConnectMqttAsync(Ora2MqttMqttOptions options,Can
5555
var factory = new MqttFactory();
5656
var client = factory.CreateMqttClient();
5757
var builder = new MqttClientOptionsBuilder()
58-
.WithTcpServer(options.Host);
58+
.WithTcpServer(options.Host)
59+
.WithTlsOptions(new MqttClientTlsOptions { UseTls = options.UseTls });
5960
if (!String.IsNullOrEmpty(options.Username) && !String.IsNullOrEmpty(options.Password))
6061
{
6162
builder = builder.WithCredentials(options.Username, options.Password);

0 commit comments

Comments
 (0)