-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Introduce MQTT Client SDK (#47)
- Loading branch information
Showing
21 changed files
with
329 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[*.cs] | ||
|
||
# CA1848: Use the LoggerMessage delegates | ||
dotnet_diagnostic.CA1848.severity = none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "C#: Mortein", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/Mortein/bin/Debug/net8.0/Mortein.dll", | ||
"console": "integratedTerminal", | ||
"justMyCode": false | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Mortein.Mqtt.Services; | ||
using MQTTnet.Client; | ||
|
||
namespace Mortein.Mqtt.Extensions; | ||
|
||
/// <summary> | ||
/// Extension methods for setting up MQTT client related services in an | ||
/// <see cref="IServiceCollection" />. | ||
/// </summary> | ||
public static class ServiceCollectionExtension | ||
{ | ||
/// <summary> | ||
/// Registers a hosted MQTT client as a service in the <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// | ||
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||
/// | ||
/// <returns>The same service collection so that multiple calls can be chained.</returns> | ||
public static IServiceCollection AddMqttClientHostedService(this IServiceCollection services) | ||
{ | ||
services.AddMqttClientServiceWithConfig(optionsBuilder => | ||
{ | ||
var certificate = MqttAuthentication.GetAwsMqttCertificate(); | ||
optionsBuilder | ||
.WithClientId(Environment.GetEnvironmentVariable("MQTT_CLIENT_ID")) | ||
.WithoutPacketFragmentation() | ||
.WithTcpServer(Environment.GetEnvironmentVariable("MQTT_BROKER_HOSTNAME")) | ||
.WithTlsOptions(options => | ||
{ | ||
options | ||
.UseTls() | ||
.WithAllowUntrustedCertificates(false) | ||
.WithClientCertificates([certificate.Result]) | ||
.WithIgnoreCertificateChainErrors(false) | ||
.WithIgnoreCertificateRevocationErrors(false); | ||
}); | ||
}); | ||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Registers a hosted MQTT client as a service in the <see cref="IServiceCollection" />. | ||
/// </summary> | ||
/// | ||
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param> | ||
/// <param name="configure"> | ||
/// A required action to configure the <see cref="MqttClientOptionsBuilder" /> for the client. | ||
/// </param> | ||
/// | ||
/// <returns>The same service collection so that multiple calls can be chained.</returns> | ||
private static IServiceCollection AddMqttClientServiceWithConfig(this IServiceCollection services, Action<MqttClientOptionsBuilder> configure) | ||
{ | ||
services.AddSingleton(_ => | ||
{ | ||
var optionBuilder = new MqttClientOptionsBuilder(); | ||
configure(optionBuilder); | ||
return optionBuilder.Build(); | ||
}); | ||
services.AddSingleton<MqttClientService>(); | ||
services.AddSingleton<IHostedService>(serviceProvider => | ||
{ | ||
return serviceProvider.GetService<MqttClientService>()!; | ||
}); | ||
services.AddSingleton(serviceProvider => | ||
{ | ||
var mqttClientService = serviceProvider.GetService<MqttClientService>(); | ||
var mqttClientServiceProvider = new MqttClientServiceProvider(mqttClientService!); | ||
return mqttClientServiceProvider; | ||
}); | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
using Amazon; | ||
using Amazon.S3; | ||
using Amazon.S3.Model; | ||
|
||
namespace Mortein.Mqtt; | ||
|
||
/// <summary> | ||
/// Provides authentication for the MQTT service. | ||
/// </summary> | ||
public static class MqttAuthentication | ||
{ | ||
private static readonly string credentialBucketName = "api-mqtt-certificate"; | ||
private static readonly string objectName = "api.pfx"; | ||
|
||
private static readonly AmazonS3Client s3 = new(RegionEndpoint.APSoutheast2); | ||
|
||
private static async Task<GetObjectResponse?> GetCredentialFileObject() | ||
{ | ||
return await s3.GetObjectAsync(credentialBucketName, objectName); | ||
} | ||
|
||
/// <summary> | ||
/// Return the <see cref="X509Certificate2"/> with which to authenticate to the MQTT broker. | ||
/// </summary> | ||
/// | ||
/// <returns>The <see cref="X509Certificate2"/> with which to authenticate to the MQTT broker</returns> | ||
public static async Task<X509Certificate2> GetAwsMqttCertificate() | ||
{ | ||
var credentialFileObject = (await GetCredentialFileObject())!; | ||
|
||
byte[] credentialBytes; | ||
|
||
using var credentialFileStream = credentialFileObject.ResponseStream; | ||
using var memoryStream = new MemoryStream(); | ||
{ | ||
credentialFileStream.CopyTo(memoryStream); | ||
credentialBytes = memoryStream.ToArray(); | ||
} | ||
|
||
return new(credentialBytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# MQTT | ||
|
||
Everything in this directory comes from /~https://github.com/rafiulgits/mqtt-client-dotnet-core with | ||
limited changes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Mortein.Mqtt.Services; | ||
|
||
/// <summary> | ||
/// Represents an ASP.NET service type which provides a managed MQTT client. | ||
/// </summary> | ||
public interface IMqttClientService : IHostedService { } |
Oops, something went wrong.