From e8e8080dfe22f4bfc3562f3ec0faeea14c19a864 Mon Sep 17 00:00:00 2001 From: Michael Hallock Date: Sun, 28 Jul 2019 23:18:18 -0400 Subject: [PATCH] Upgraded HomeAutio.Core and added broker TLS support, as well as changed to sending a KeyPress instead of just a SendCommand event --- .../HarmonyMqttService.cs | 6 ++- .../HomeAutio.Mqtt.Harmony.csproj | 2 +- src/HomeAutio.Mqtt.Harmony/Program.cs | 49 ++++++++++++++++++- src/HomeAutio.Mqtt.Harmony/appsettings.json | 4 +- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/HomeAutio.Mqtt.Harmony/HarmonyMqttService.cs b/src/HomeAutio.Mqtt.Harmony/HarmonyMqttService.cs index 39d38b5..6859db9 100644 --- a/src/HomeAutio.Mqtt.Harmony/HarmonyMqttService.cs +++ b/src/HomeAutio.Mqtt.Harmony/HarmonyMqttService.cs @@ -25,6 +25,7 @@ public class HarmonyMqttService : ServiceBase private IClient _client; private string _harmonyName; + private int _harmonyKeyPressLength; private HarmonyConfig _harmonyConfig; /// @@ -38,11 +39,13 @@ public class HarmonyMqttService : ServiceBase /// Logging instance. /// The Harmony client. /// The Harmony name. + /// The Harmony key press length. /// MQTT broker settings. public HarmonyMqttService( ILogger logger, IClient harmonyClient, string harmonyName, + int harmonyKeyPressLength, BrokerSettings brokerSettings) : base(logger, brokerSettings, "harmony/" + harmonyName) { @@ -55,6 +58,7 @@ public HarmonyMqttService( // Setup harmony client _client = harmonyClient; _harmonyName = harmonyName; + _harmonyKeyPressLength = harmonyKeyPressLength; _client.CurrentActivityUpdated += Harmony_CurrentActivityUpdated; // Harmony client logging @@ -113,7 +117,7 @@ await _client.StartActivityAsync(int.Parse(activity.Id)) var command = _topicActionMap[e.ApplicationMessage.Topic]; if (command != null) { - await _client.SendCommandAsync(command) + await _client.SendKeyPressAsync(command, _harmonyKeyPressLength) .ConfigureAwait(false); } } diff --git a/src/HomeAutio.Mqtt.Harmony/HomeAutio.Mqtt.Harmony.csproj b/src/HomeAutio.Mqtt.Harmony/HomeAutio.Mqtt.Harmony.csproj index 065cbb7..187c3b4 100644 --- a/src/HomeAutio.Mqtt.Harmony/HomeAutio.Mqtt.Harmony.csproj +++ b/src/HomeAutio.Mqtt.Harmony/HomeAutio.Mqtt.Harmony.csproj @@ -33,7 +33,7 @@ all - + diff --git a/src/HomeAutio.Mqtt.Harmony/Program.cs b/src/HomeAutio.Mqtt.Harmony/Program.cs index a4d4b5b..2a7f16b 100644 --- a/src/HomeAutio.Mqtt.Harmony/Program.cs +++ b/src/HomeAutio.Mqtt.Harmony/Program.cs @@ -1,4 +1,7 @@ using System; +using System.IO; +using System.Linq; +using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using HarmonyHub; using Microsoft.Extensions.Configuration; @@ -85,13 +88,57 @@ private static IHostBuilder CreateHostBuilder(IConfiguration config) BrokerIp = config.GetValue("mqtt:brokerIp"), BrokerPort = config.GetValue("mqtt:brokerPort"), BrokerUsername = config.GetValue("mqtt:brokerUsername"), - BrokerPassword = config.GetValue("mqtt:brokerPassword") + BrokerPassword = config.GetValue("mqtt:brokerPassword"), + BrokerUseTls = config.GetValue("mqtt:brokerUseTls", false) }; + // TLS settings + if (brokerSettings.BrokerUseTls) + { + var brokerTlsSettings = new Core.BrokerTlsSettings + { + AllowUntrustedCertificates = config.GetValue("mqtt:brokerTlsSettings:allowUntrustedCertificates", false), + IgnoreCertificateChainErrors = config.GetValue("mqtt:brokerTlsSettings:ignoreCertificateChainErrors", false), + IgnoreCertificateRevocationErrors = config.GetValue("mqtt:brokerTlsSettings:ignoreCertificateRevocationErrors", false) + }; + + switch (config.GetValue("mqtt:brokerTlsSettings:protocol", "1.2")) + { + case "1.0": + brokerTlsSettings.SslProtocol = System.Security.Authentication.SslProtocols.Tls; + break; + case "1.1": + brokerTlsSettings.SslProtocol = System.Security.Authentication.SslProtocols.Tls11; + break; + case "1.2": + default: + brokerTlsSettings.SslProtocol = System.Security.Authentication.SslProtocols.Tls12; + break; + } + + var brokerTlsCertificatesSection = config.GetSection("mqtt:brokerTlsSettings:certificates"); + brokerTlsSettings.Certificates = brokerTlsCertificatesSection.GetChildren() + .Select(x => + { + var file = x.GetValue("file"); + var passPhrase = x.GetValue("passPhrase"); + + if (!File.Exists(file)) + throw new FileNotFoundException($"Broker Certificate '{file}' is missing!"); + + return !string.IsNullOrEmpty(passPhrase) ? + new X509Certificate2(file, passPhrase) : + new X509Certificate2(file); + }).ToList(); + + brokerSettings.BrokerTlsSettings = brokerTlsSettings; + } + return new HarmonyMqttService( serviceProvider.GetRequiredService>(), serviceProvider.GetRequiredService(), config.GetValue("harmony:harmonyName"), + config.GetValue("harmony:harmonyKeyPressLength", 100), brokerSettings); }); }); diff --git a/src/HomeAutio.Mqtt.Harmony/appsettings.json b/src/HomeAutio.Mqtt.Harmony/appsettings.json index 9936f2d..ca40ba8 100644 --- a/src/HomeAutio.Mqtt.Harmony/appsettings.json +++ b/src/HomeAutio.Mqtt.Harmony/appsettings.json @@ -4,13 +4,15 @@ "harmonyHost": "blank", "harmonyUsername": "", "harmonyPassword": "", + "harmonyKeyPressLength": 100, "bypassLogitechLogin": true }, "mqtt": { "brokerIp": "localhost", "brokerPort": 1883, "brokerUsername": null, - "brokerPassword": null + "brokerPassword": null, + "brokerUseTls": false }, "Serilog": { "Enrich": [ "FromLogContext" ],