diff --git a/TeslaSolarCharger/Server/Services/SolarMqttService.cs b/TeslaSolarCharger/Server/Services/SolarMqttService.cs index c72b26ae5..2c37b3351 100644 --- a/TeslaSolarCharger/Server/Services/SolarMqttService.cs +++ b/TeslaSolarCharger/Server/Services/SolarMqttService.cs @@ -7,6 +7,7 @@ using TeslaSolarCharger.Shared.Dtos.Contracts; using TeslaSolarCharger.Shared.Dtos.BaseConfiguration; using TeslaSolarCharger.Shared.Enums; +using System.Security.Cryptography.X509Certificates; namespace TeslaSolarCharger.Server.Services; @@ -40,10 +41,25 @@ public async Task ConnectMqttClient() { _logger.LogDebug("No Mqtt Options defined for solar power. Do not connect MQTT Client."); } + + var caCert = new X509Certificate2("/app/venus-ca.crt"); + var mqttClientOptions = new MqttClientOptionsBuilder() .WithClientId(mqqtClientId) .WithTimeout(TimeSpan.FromSeconds(5)) .WithTcpServer(mqttServer, mqttServerPort) + .WithTls(new MqttClientOptionsBuilderTlsParameters + { + UseTls = true, + AllowUntrustedCertificates = true, + IgnoreCertificateChainErrors = true, + IgnoreCertificateRevocationErrors = true, + CertificateValidationHandler = (context) => + { + return true; + }, + Certificates = new List { caCert } + }) .Build(); if(!string.IsNullOrWhiteSpace(_configurationWrapper.SolarMqttUsername()) && !string.IsNullOrEmpty(_configurationWrapper.SolarMqttPassword())) @@ -120,7 +136,7 @@ public async Task ConnectMqttClient() { _logger.LogWarning("Received value does not match a topic"); } - + return Task.CompletedTask; }; diff --git a/TeslaSolarCharger/Server/venus-ca.crt b/TeslaSolarCharger/Server/venus-ca.crt new file mode 100644 index 000000000..a4321b7c5 --- /dev/null +++ b/TeslaSolarCharger/Server/venus-ca.crt @@ -0,0 +1,24 @@ +-----BEGIN CERTIFICATE----- +MIIECTCCAvGgAwIBAgIJAM+t3iC8ybEHMA0GCSqGSIb3DQEBCwUAMIGZMQswCQYD +VQQGEwJOTDESMBAGA1UECAwJR3JvbmluZ2VuMRIwEAYDVQQHDAlHcm9uaW5nZW4x +HDAaBgNVBAoME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