Add new configuartion model

Added new models for review
This commit is contained in:
Kiarash
2019-03-10 13:58:18 +01:00
parent 6e64d653c5
commit 9e7c70e649
8 changed files with 270 additions and 91 deletions
@@ -1,9 +0,0 @@
namespace MQTTnet.Server.Configuration
{
public class EndpointConfiguration
{
public int Port { get; set; } = 1883;
public int BacklogSize { get; set; } = 10;
}
}
@@ -1,4 +1,7 @@
namespace MQTTnet.Server.Configuration using System.IO;
using System.Net;
namespace MQTTnet.Server.Configuration
{ {
/// <summary> /// <summary>
/// Listen Entry Settings Model /// Listen Entry Settings Model
@@ -12,19 +15,142 @@
{ {
} }
/// <summary>
/// Path to Certificate
/// </summary>
public string CertificatePath { get; set; }
/// <summary>
/// Enabled / Disable
/// </summary>
public bool Enabled { get; set; } = true;
/// <summary> /// <summary>
/// Listen Address /// Listen Address
/// </summary> /// </summary>
public string Address { get; set; } public string IPv4 { get; set; }
/// <summary>
/// Listen Address
/// </summary>
public string IPv6 { get; set; }
/// <summary> /// <summary>
/// Listen Port /// Listen Port
/// </summary> /// </summary>
public int Port { get; set; } public int Port { get; set; } = 1883;
/// <summary> /// <summary>
/// Protocol Type /// Read Certificate file
/// </summary> /// </summary>
public ListenProtocolTypes Protocol { get; set; } = ListenProtocolTypes.HTTP; /// <returns></returns>
public byte[] ReadCertificate()
{
if (string.IsNullOrEmpty(CertificatePath) || string.IsNullOrWhiteSpace(CertificatePath))
{
throw new FileNotFoundException("No path set");
}
if (!File.Exists(CertificatePath))
{
throw new FileNotFoundException($"Could not find Certificate in path: {CertificatePath}");
}
return File.ReadAllBytes(CertificatePath);
}
/// <summary>
/// Read IPv4
/// </summary>
/// <returns></returns>
public IPAddress ReafIPv4()
{
if (IPv4 == "*")
{
return IPAddress.Parse("0.0.0.0");
}
if (IPv4 == "localhost")
{
return IPAddress.Parse("127.0.0.1");
}
if (IPAddress.TryParse(IPv4, out var ip))
{
return ip;
}
throw new System.Exception($"Could not parse IPv4 address: {IPv4}");
}
/// <summary>
/// Read IPv4
/// </summary>
/// <returns></returns>
public bool TryReadIPv4(out IPAddress address)
{
if (IPv4 == "*")
{
address = IPAddress.Parse("::");
return true;
}
if (IPv4 == "localhost")
{
address = IPAddress.Parse("::1");
return true;
}
if (IPv4 == "disable")
{
address = null;
return false;
}
if (IPAddress.TryParse(IPv4, out var ip))
{
address = ip;
return true;
}
else
{
throw new System.Exception($"Could not parse IPv4 address: {IPv4}");
}
}
/// <summary>
/// Read IPv6
/// </summary>
/// <returns></returns>
public bool TryReadIPv6(out IPAddress address)
{
if (IPv6 == "*")
{
address = IPAddress.Parse("::");
return true;
}
if (IPv6 == "localhost")
{
address = IPAddress.Parse("::1");
return true;
}
if (IPv6 == "disable")
{
address = null;
return false;
}
if (IPAddress.TryParse(IPv6, out var ip))
{
address = ip;
return true;
}
else
{
throw new System.Exception($"Could not parse IPv6 address: {IPv6}");
}
}
} }
} }
@@ -1,23 +0,0 @@
namespace MQTTnet.Server.Configuration
{
/// <summary>
/// Listen Protocol Types
/// </summary>
public enum ListenProtocolTypes
{
/// <summary>
/// HTTP
/// </summary>
HTTP = 0,
/// <summary>
/// HTTPS
/// </summary>
HTTPS = 1,
/// <summary>
/// MQTT
/// </summary>
MQTT = 20
}
}
@@ -1,7 +0,0 @@
namespace MQTTnet.Server.Configuration
{
public class MqttNetServerConfiguration
{
}
}
@@ -1,15 +1,42 @@
using System.Collections.Generic; namespace MQTTnet.Server.Configuration
namespace MQTTnet.Server.Configuration
{ {
/// <summary> /// <summary>
/// Main Settings Model /// Main Settings Model
/// </summary> /// </summary>
public class SettingsModel public class SettingsModel
{ {
public SettingsModel()
{
}
/// <summary>
/// Set default connection timeout in seconds
/// </summary>
public int CommunicationTimeout { get; set; } = 15;
/// <summary>
/// Set 0 to disable connection backlogging
/// </summary>
public int ConnectionBacklog { get; set; }
/// <summary>
/// Enable support for persistent sessions
/// </summary>
public bool EnablePersistentSessions { get; set; } = false;
/// <summary> /// <summary>
/// Listen Settings /// Listen Settings
/// </summary> /// </summary>
public IEnumerable<ListenModel> Listen { get; set; } public ListenModel Listen { get; set; } = new ListenModel();
/// <summary>
/// Encryption Listen Settings
/// </summary>
public ListenModel ListenEncryption { get; set; } = new ListenModel();
/// <summary>
/// Set limit for max pending messages per client
/// </summary>
public int MaxPendingMessagesPerClient { get; set; } = 250;
} }
} }
@@ -16,18 +16,19 @@ namespace MQTTnet.Server.Mqtt
{ {
private readonly ILogger<MqttServerService> _logger; private readonly ILogger<MqttServerService> _logger;
private readonly Configuration.SettingsModel _settings;
private readonly MqttApplicationMessageInterceptor _mqttApplicationMessageInterceptor;
private readonly MqttClientConnectedHandler _mqttClientConnectedHandler; private readonly MqttClientConnectedHandler _mqttClientConnectedHandler;
private readonly MqttClientDisconnectedHandler _mqttClientDisconnectedHandler; private readonly MqttClientDisconnectedHandler _mqttClientDisconnectedHandler;
private readonly MqttClientSubscribedTopicHandler _mqttClientSubscribedTopicHandler; private readonly MqttClientSubscribedTopicHandler _mqttClientSubscribedTopicHandler;
private readonly MqttClientUnsubscribedTopicHandler _mqttClientUnsubscribedTopicHandler; private readonly MqttClientUnsubscribedTopicHandler _mqttClientUnsubscribedTopicHandler;
private readonly MqttConnectionValidator _mqttConnectionValidator; private readonly MqttConnectionValidator _mqttConnectionValidator;
private readonly IMqttServer _mqttServer;
private readonly MqttSubscriptionInterceptor _mqttSubscriptionInterceptor; private readonly MqttSubscriptionInterceptor _mqttSubscriptionInterceptor;
private readonly MqttApplicationMessageInterceptor _mqttApplicationMessageInterceptor;
private readonly PythonScriptHostService _pythonScriptHostService; private readonly PythonScriptHostService _pythonScriptHostService;
private readonly IMqttServer _mqttServer;
public MqttServerService( public MqttServerService(
Configuration.SettingsModel settings,
CustomMqttFactory mqttFactory, CustomMqttFactory mqttFactory,
MqttWebSocketServerAdapter webSocketServerAdapter, MqttWebSocketServerAdapter webSocketServerAdapter,
MqttClientConnectedHandler mqttClientConnectedHandler, MqttClientConnectedHandler mqttClientConnectedHandler,
@@ -40,6 +41,7 @@ namespace MQTTnet.Server.Mqtt
PythonScriptHostService pythonScriptHostService, PythonScriptHostService pythonScriptHostService,
ILogger<MqttServerService> logger) ILogger<MqttServerService> logger)
{ {
_settings = settings;
_mqttClientConnectedHandler = mqttClientConnectedHandler ?? throw new ArgumentNullException(nameof(mqttClientConnectedHandler)); _mqttClientConnectedHandler = mqttClientConnectedHandler ?? throw new ArgumentNullException(nameof(mqttClientConnectedHandler));
_mqttClientDisconnectedHandler = mqttClientDisconnectedHandler ?? throw new ArgumentNullException(nameof(mqttClientDisconnectedHandler)); _mqttClientDisconnectedHandler = mqttClientDisconnectedHandler ?? throw new ArgumentNullException(nameof(mqttClientDisconnectedHandler));
_mqttClientSubscribedTopicHandler = mqttClientSubscribedTopicHandler ?? throw new ArgumentNullException(nameof(mqttClientSubscribedTopicHandler)); _mqttClientSubscribedTopicHandler = mqttClientSubscribedTopicHandler ?? throw new ArgumentNullException(nameof(mqttClientSubscribedTopicHandler));
@@ -64,19 +66,75 @@ namespace MQTTnet.Server.Mqtt
_pythonScriptHostService.RegisterProxyObject("publish", new Action<PythonDictionary>(Publish)); _pythonScriptHostService.RegisterProxyObject("publish", new Action<PythonDictionary>(Publish));
var options = new MqttServerOptionsBuilder() var options = new MqttServerOptionsBuilder()
.WithDefaultEndpoint() .WithMaxPendingMessagesPerClient(_settings.MaxPendingMessagesPerClient)
.WithDefaultEndpointPort(1883) .WithDefaultCommunicationTimeout(TimeSpan.FromSeconds(_settings.CommunicationTimeout))
.WithConnectionValidator(_mqttConnectionValidator) .WithConnectionValidator(_mqttConnectionValidator)
.WithApplicationMessageInterceptor(_mqttApplicationMessageInterceptor) .WithApplicationMessageInterceptor(_mqttApplicationMessageInterceptor)
.WithSubscriptionInterceptor(_mqttSubscriptionInterceptor) .WithSubscriptionInterceptor(_mqttSubscriptionInterceptor);
.Build();
// Configure unencrypted connections
if (_settings.Listen.Enabled)
{
options.WithDefaultEndpoint();
if (_settings.Listen.TryReadIPv6(out var address4))
{
options.WithDefaultEndpointBoundIPAddress(address4);
}
if (_settings.Listen.TryReadIPv6(out var address6))
{
options.WithDefaultEndpointBoundIPV6Address(address6);
}
if (_settings.Listen.Port > 0)
{
options.WithDefaultEndpointPort(_settings.Listen.Port);
}
}
else
{
options.WithoutDefaultEndpoint();
}
// Configure encrypted connections
if (_settings.ListenEncryption.Enabled)
{
options
.WithEncryptedEndpoint()
.WithEncryptionSslProtocol(System.Security.Authentication.SslProtocols.Tls12)
.WithEncryptionCertificate(_settings.ListenEncryption.ReadCertificate());
if (_settings.ListenEncryption.TryReadIPv6(out var address4))
{
options.WithDefaultEndpointBoundIPAddress(address4);
}
if (_settings.ListenEncryption.TryReadIPv6(out var address6))
{
options.WithDefaultEndpointBoundIPV6Address(address6);
}
if (_settings.Listen.Port > 0)
{
options.WithEncryptedEndpointPort(_settings.ListenEncryption.Port);
}
}
else
{
options.WithoutEncryptedEndpoint();
}
if (_settings.ConnectionBacklog > 0)
{
options.WithConnectionBacklog(_settings.ConnectionBacklog);
}
_mqttServer.ClientConnectedHandler = _mqttClientConnectedHandler; _mqttServer.ClientConnectedHandler = _mqttClientConnectedHandler;
_mqttServer.ClientDisconnectedHandler = _mqttClientDisconnectedHandler; _mqttServer.ClientDisconnectedHandler = _mqttClientDisconnectedHandler;
_mqttServer.ClientSubscribedTopicHandler = _mqttClientSubscribedTopicHandler; _mqttServer.ClientSubscribedTopicHandler = _mqttClientSubscribedTopicHandler;
_mqttServer.ClientUnsubscribedTopicHandler = _mqttClientUnsubscribedTopicHandler; _mqttServer.ClientUnsubscribedTopicHandler = _mqttClientUnsubscribedTopicHandler;
_mqttServer.StartAsync(options).GetAwaiter().GetResult(); _mqttServer.StartAsync(options.Build()).GetAwaiter().GetResult();
_logger.LogInformation("MQTT server started."); _logger.LogInformation("MQTT server started.");
} }
@@ -127,4 +185,4 @@ namespace MQTTnet.Server.Mqtt
} }
} }
} }
} }
+10 -4
View File
@@ -15,11 +15,11 @@ namespace MQTTnet.Server
{ {
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
{ {
//var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
// .AddJsonFile("appsettings.json") .AddJsonFile("appsettings.json")
// .AddEnvironmentVariables(); .AddEnvironmentVariables();
//Configuration = builder.Build(); Configuration = builder.Build();
} }
public IConfigurationRoot Configuration { get; } public IConfigurationRoot Configuration { get; }
@@ -52,6 +52,12 @@ namespace MQTTnet.Server
{ {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
// Read settings
var settings = new Configuration.SettingsModel();
Configuration.Bind("MQTTnet", settings);
services.AddSingleton(settings);
// Wire up dependencies
services.AddSingleton<PythonIOStream>(); services.AddSingleton<PythonIOStream>();
services.AddSingleton<PythonScriptHostService>(); services.AddSingleton<PythonScriptHostService>();
services.AddSingleton<DataSharingService>(); services.AddSingleton<DataSharingService>();
+31 -30
View File
@@ -1,32 +1,33 @@
{ {
"MQTTnetServer": { "MQTTnet": {
/* /*
Wildcard Addresses: Wildcard Addresses:
* - All local IP addresses * - All local IP addresses
localhost - Localhost only localhost - Localhost only
disable - Skip address assignment
Supported Protocols: */
0 - HTTP "Listen": {
1 - HTTPS "Enabled": true,
20 - MQTT "IPv4": "localhost",
*/ "IPv6": "localhost",
"Listen": [ "Port": 1883
{ },
"Address": "localhost", "ListenEncryption": {
"Port": 5000, "Enabled": false,
"Protocol": 0 "IPv4": "localhost",
}, "IPv6": "localhost",
{ "Port": 8883,
"Address": "*", "CertificatePath": "/absolute/path/to/certificate"
"Port": 1883, },
"Protocol": 20 "CommunicationTimeout": 15, /* In seconds */
} "ConnectionBacklog": 0, /* Set 0 to disable */
] "EnablePersistentSessions": false,
}, "MaxPendingMessagesPerClient": 250
"Logging": { },
"LogLevel": { "Logging": {
"Default": "Warning" "LogLevel": {
} "Default": "Warning"
}, }
"AllowedHosts": "*" },
"AllowedHosts": "*"
} }