mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
Fix and clean up
This commit is contained in:
@@ -59,30 +59,6 @@ namespace MQTTnet.Server.Configuration
|
||||
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>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,12 +27,12 @@
|
||||
/// <summary>
|
||||
/// Listen Settings
|
||||
/// </summary>
|
||||
public ListenModel Listen { get; set; } = new ListenModel();
|
||||
public ListenModel Endpoint { get; set; } = new ListenModel();
|
||||
|
||||
/// <summary>
|
||||
/// Encryption Listen Settings
|
||||
/// </summary>
|
||||
public ListenModel ListenEncryption { get; set; } = new ListenModel();
|
||||
public ListenModel EndpointEncryption { get; set; } = new ListenModel();
|
||||
|
||||
/// <summary>
|
||||
/// Set limit for max pending messages per client
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<Startup>();
|
||||
|
||||
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<Startup>();
|
||||
|
||||
return webHost;
|
||||
}
|
||||
|
||||
public static ListenModel[] ReadListenSettings()
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
var listen = new List<ListenModel>();
|
||||
builder.Bind("MQTTnetServer:Listen", listen);
|
||||
|
||||
return listen.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user