diff --git a/Source/MQTTnet.Server/Configuration/ListenModel.cs b/Source/MQTTnet.Server/Configuration/ListenModel.cs index 1715312d..e72d8975 100644 --- a/Source/MQTTnet.Server/Configuration/ListenModel.cs +++ b/Source/MQTTnet.Server/Configuration/ListenModel.cs @@ -59,30 +59,6 @@ namespace MQTTnet.Server.Configuration return File.ReadAllBytes(CertificatePath); } - /// - /// Read IPv4 - /// - /// - 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}"); - } - /// /// Read IPv4 /// @@ -91,13 +67,13 @@ namespace MQTTnet.Server.Configuration { if (IPv4 == "*") { - address = IPAddress.Parse("::"); + address = IPAddress.Parse("0.0.0.0"); return true; } if (IPv4 == "localhost") { - address = IPAddress.Parse("::1"); + address = IPAddress.Parse("127.0.0.1"); return true; } diff --git a/Source/MQTTnet.Server/Configuration/SettingsModel.cs b/Source/MQTTnet.Server/Configuration/SettingsModel.cs index 47332dc4..6b3c95c2 100644 --- a/Source/MQTTnet.Server/Configuration/SettingsModel.cs +++ b/Source/MQTTnet.Server/Configuration/SettingsModel.cs @@ -27,12 +27,12 @@ /// /// Listen Settings /// - public ListenModel Listen { get; set; } = new ListenModel(); + public ListenModel Endpoint { get; set; } = new ListenModel(); /// /// Encryption Listen Settings /// - public ListenModel ListenEncryption { get; set; } = new ListenModel(); + public ListenModel EndpointEncryption { get; set; } = new ListenModel(); /// /// Set limit for max pending messages per client diff --git a/Source/MQTTnet.Server/Mqtt/MqttServerService.cs b/Source/MQTTnet.Server/Mqtt/MqttServerService.cs index 046876dc..c3abcf0c 100644 --- a/Source/MQTTnet.Server/Mqtt/MqttServerService.cs +++ b/Source/MQTTnet.Server/Mqtt/MqttServerService.cs @@ -73,22 +73,22 @@ namespace MQTTnet.Server.Mqtt .WithSubscriptionInterceptor(_mqttSubscriptionInterceptor); // Configure unencrypted connections - if (_settings.Listen.Enabled) + if (_settings.Endpoint.Enabled) { options.WithDefaultEndpoint(); - if (_settings.Listen.TryReadIPv6(out var address4)) + if (_settings.Endpoint.TryReadIPv4(out var address4)) { options.WithDefaultEndpointBoundIPAddress(address4); } - if (_settings.Listen.TryReadIPv6(out var address6)) + if (_settings.Endpoint.TryReadIPv6(out var address6)) { options.WithDefaultEndpointBoundIPV6Address(address6); } - if (_settings.Listen.Port > 0) + if (_settings.Endpoint.Port > 0) { - options.WithDefaultEndpointPort(_settings.Listen.Port); + options.WithDefaultEndpointPort(_settings.Endpoint.Port); } } else @@ -97,26 +97,26 @@ namespace MQTTnet.Server.Mqtt } // Configure encrypted connections - if (_settings.ListenEncryption.Enabled) + if (_settings.EndpointEncryption.Enabled) { options .WithEncryptedEndpoint() .WithEncryptionSslProtocol(System.Security.Authentication.SslProtocols.Tls12) - .WithEncryptionCertificate(_settings.ListenEncryption.ReadCertificate()); + .WithEncryptionCertificate(_settings.EndpointEncryption.ReadCertificate()); - if (_settings.ListenEncryption.TryReadIPv6(out var address4)) + if (_settings.EndpointEncryption.TryReadIPv4(out var address4)) { - options.WithDefaultEndpointBoundIPAddress(address4); + options.WithEncryptedEndpointBoundIPAddress(address4); } - if (_settings.ListenEncryption.TryReadIPv6(out var address6)) + if (_settings.EndpointEncryption.TryReadIPv6(out var address6)) { - options.WithDefaultEndpointBoundIPV6Address(address6); + options.WithEncryptedEndpointBoundIPAddress(address6); } - if (_settings.Listen.Port > 0) + if (_settings.Endpoint.Port > 0) { - options.WithEncryptedEndpointPort(_settings.ListenEncryption.Port); + options.WithEncryptedEndpointPort(_settings.EndpointEncryption.Port); } } else diff --git a/Source/MQTTnet.Server/Program.cs b/Source/MQTTnet.Server/Program.cs index 5d50030b..221f48c9 100644 --- a/Source/MQTTnet.Server/Program.cs +++ b/Source/MQTTnet.Server/Program.cs @@ -1,9 +1,6 @@ using System; -using System.Collections.Generic; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using MQTTnet.Server.Configuration; namespace MQTTnet.Server { @@ -25,21 +22,25 @@ namespace MQTTnet.Server } } + private static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseKestrel() + .UseStartup(); + private static void PrintLogo() { Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(@" - __ __ ____ _______ _______ _ _____ - | \/ |/ __ \__ __|__ __| | | / ____| - | \ / | | | | | | | |_ __ ___| |_ | (___ ___ _ ____ _____ _ __ + __ __ ____ _______ _______ _ _____ + | \/ |/ __ \__ __|__ __| | | / ____| + | \ / | | | | | | | |_ __ ___| |_ | (___ ___ _ ____ _____ _ __ | |\/| | | | | | | | | '_ \ / _ \ __| \___ \ / _ \ '__\ \ / / _ \ '__| - | | | | |__| | | | | | | | | __/ |_ ____) | __/ | \ V / __/ | + | | | | |__| | | | | | | | | __/ |_ ____) | __/ | \ V / __/ | |_| |_|\___\_\ |_| |_|_| |_|\___|\__| |_____/ \___|_| \_/ \___|_|"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(@" - -- The official MQTT server implementation of MQTTnet -- + -- The official MQTT server implementation of MQTTnet -- Copyright (c) 2017-2019 The MQTTnet Team"); Console.ForegroundColor = ConsoleColor.Blue; @@ -60,87 +61,5 @@ namespace MQTTnet.Server Console.WriteLine(" ! THIS IS AN ALPHA VERSION! IT IS NOT RECOMMENDED TO USE IT FOR ANY DIFFERENT PURPOSE THAN TESTING OR EVALUATING!"); Console.WriteLine(); } - - private static IWebHostBuilder CreateWebHostBuilder(string[] args) - { - var webHost = WebHost.CreateDefaultBuilder(args) - .UseKestrel(kestrelOptions => - { - kestrelOptions.ListenAnyIP(80, listenOptions => - { - listenOptions.NoDelay = true; - }); - }); - - //var listen = ReadListenSettings(); - //webHost - // .UseKestrel(o => - // { - // if (listen?.Length > 0) - // { - // foreach (var item in listen) - // { - // if (item.Address?.Trim() == "*") - // { - // if (item.Protocol == ListenProtocolTypes.MQTT) - // { - // o.ListenAnyIP(item.Port, c => c.UseMqtt()); - // } - // else - // { - // o.ListenAnyIP(item.Port); - // } - // } - // else if (item.Address?.Trim() == "localhost") - // { - // if (item.Protocol == ListenProtocolTypes.MQTT) - // { - // o.ListenLocalhost(item.Port, c => c.UseMqtt()); - // } - // else - // { - // o.ListenLocalhost(item.Port); - // } - // } - // else - // { - // if (IPAddress.TryParse(item.Address, out var ip)) - // { - // if (item.Protocol == ListenProtocolTypes.MQTT) - // { - // o.Listen(ip, item.Port, c => c.UseMqtt()); - // } - // else - // { - // o.Listen(ip, item.Port); - // } - // } - // } - // } - // } - // else - // { - // o.ListenAnyIP(1883, l => l.UseMqtt()); - // o.ListenAnyIP(5000); - // } - // }); - - webHost.UseStartup(); - - return webHost; - } - - public static ListenModel[] ReadListenSettings() - { - var builder = new ConfigurationBuilder() - .AddJsonFile("appsettings.json") - .AddEnvironmentVariables() - .Build(); - - var listen = new List(); - builder.Bind("MQTTnetServer:Listen", listen); - - return listen.ToArray(); - } } -} +} \ No newline at end of file diff --git a/Source/MQTTnet.Server/Startup.cs b/Source/MQTTnet.Server/Startup.cs index bdcc7aaf..659f5057 100644 --- a/Source/MQTTnet.Server/Startup.cs +++ b/Source/MQTTnet.Server/Startup.cs @@ -90,7 +90,7 @@ namespace MQTTnet.Server // Read settings var settings = new Configuration.SettingsModel(); - Configuration.Bind("MQTTnet", settings); + Configuration.Bind("MQTT", settings); services.AddSingleton(settings); // Wire up dependencies diff --git a/Source/MQTTnet.Server/appsettings.json b/Source/MQTTnet.Server/appsettings.json index f5e5a5ec..1b563e36 100644 --- a/Source/MQTTnet.Server/appsettings.json +++ b/Source/MQTTnet.Server/appsettings.json @@ -1,18 +1,18 @@ { - "MQTTnet": { + "MQTT": { /* Wildcard Addresses: * - All local IP addresses localhost - Localhost only disable - Skip address assignment */ - "Listen": { + "Endpoint": { "Enabled": true, "IPv4": "localhost", "IPv6": "localhost", "Port": 1883 }, - "ListenEncryption": { + "EndpointEncryption": { "Enabled": false, "IPv4": "localhost", "IPv6": "localhost",