mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-11-05 14:02:55 +08:00
Merge branch 'master' of https://github.com/chkr1011/MQTTnet
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
<releaseNotes>
|
<releaseNotes>
|
||||||
* [ManagedClient] Extended ReconnectAsync (thanks to @nvsnkv, #1202).
|
* [ManagedClient] Extended ReconnectAsync (thanks to @nvsnkv, #1202).
|
||||||
* [MQTTnet.Server] Moved server project to a dedicated GitHub repository.
|
* [MQTTnet.Server] Moved server project to a dedicated GitHub repository.
|
||||||
|
* [MQTTnet, MQTTnet.Extensions.ManagedClient] Fixed bug that allowed invalid subscriptions (Thanks to @marcelwinh).
|
||||||
Git commit: $gitCommit
|
Git commit: $gitCommit
|
||||||
</releaseNotes>
|
</releaseNotes>
|
||||||
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
<copyright>Copyright Christian Kratky 2016-2020</copyright>
|
||||||
|
|||||||
@@ -215,6 +215,11 @@ namespace MQTTnet.Extensions.ManagedClient
|
|||||||
|
|
||||||
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
||||||
|
|
||||||
|
foreach (var topicFilter in topicFilters)
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
|
||||||
|
}
|
||||||
|
|
||||||
lock (_subscriptions)
|
lock (_subscriptions)
|
||||||
{
|
{
|
||||||
foreach (var topicFilter in topicFilters)
|
foreach (var topicFilter in topicFilters)
|
||||||
|
|||||||
@@ -194,6 +194,11 @@ namespace MQTTnet.Client
|
|||||||
{
|
{
|
||||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||||
|
|
||||||
|
foreach (var topicFilter in options.TopicFilters)
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
|
||||||
|
}
|
||||||
|
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
ThrowIfNotConnected();
|
ThrowIfNotConnected();
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using MQTTnet.Client.Connecting;
|
using MQTTnet.Client.Connecting;
|
||||||
using MQTTnet.Client.Disconnecting;
|
using MQTTnet.Client.Disconnecting;
|
||||||
using MQTTnet.Client.ExtendedAuthenticationExchange;
|
using MQTTnet.Client.ExtendedAuthenticationExchange;
|
||||||
using MQTTnet.Client.Options;
|
using MQTTnet.Client.Options;
|
||||||
@@ -112,7 +112,7 @@ namespace MQTTnet.Client
|
|||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task ReconnectAsync(this IMqttClient client)
|
public static Task<MqttClientAuthenticateResult> ReconnectAsync(this IMqttClient client)
|
||||||
{
|
{
|
||||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|
||||||
@@ -124,6 +124,18 @@ namespace MQTTnet.Client
|
|||||||
return client.ConnectAsync(client.Options);
|
return client.ConnectAsync(client.Options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Task<MqttClientAuthenticateResult> ReconnectAsync(this IMqttClient client, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|
||||||
|
if (client.Options == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("_ReconnectAsync_ can be used only if _ConnectAsync_ was called before.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.ConnectAsync(client.Options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static Task DisconnectAsync(this IMqttClient client)
|
public static Task DisconnectAsync(this IMqttClient client)
|
||||||
{
|
{
|
||||||
if (client == null) throw new ArgumentNullException(nameof(client));
|
if (client == null) throw new ArgumentNullException(nameof(client));
|
||||||
|
|||||||
@@ -42,5 +42,15 @@ namespace MQTTnet.Protocol
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void ThrowIfInvalidSubscribe(string topic)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(topic))
|
||||||
|
{
|
||||||
|
throw new MqttProtocolViolationException("Topic should not be empty.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topic.IndexOf("#") != -1 && topic.IndexOf("#") != topic.Length - 1) throw new MqttProtocolViolationException("The character '#' is only allowed as last character");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -113,7 +113,12 @@ namespace MQTTnet.Server
|
|||||||
{
|
{
|
||||||
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
|
if (clientId == null) throw new ArgumentNullException(nameof(clientId));
|
||||||
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
if (topicFilters == null) throw new ArgumentNullException(nameof(topicFilters));
|
||||||
|
|
||||||
|
foreach (var topicFilter in topicFilters)
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe(topicFilter.Topic);
|
||||||
|
}
|
||||||
|
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
ThrowIfNotStarted();
|
ThrowIfNotStarted();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using MQTTnet.Exceptions;
|
||||||
|
using MQTTnet.Protocol;
|
||||||
|
|
||||||
|
namespace MQTTnet.Tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class MqttTopicValidatorSubscribe_Tests
|
||||||
|
{
|
||||||
|
[TestMethod]
|
||||||
|
public void Valid_Topic()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/b/c");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Valid_Topic_Plus_In_Between()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/+/c");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Valid_Topic_Plus_Last_Char()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/+");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Valid_Topic_Hash_Last_Char()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/#");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void Valid_Topic_Only_Hash()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe("#");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(MqttProtocolViolationException))]
|
||||||
|
public void Invalid_Topic_Hash_In_Between()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe("/a/#/c");
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(MqttProtocolViolationException))]
|
||||||
|
public void Invalid_Topic_Empty()
|
||||||
|
{
|
||||||
|
MqttTopicValidator.ThrowIfInvalidSubscribe(string.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
using MQTTnet.Client;
|
using MQTTnet.Client;
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -7,10 +7,8 @@ using System.Threading;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MQTTnet.Client.Options;
|
using MQTTnet.Client.Options;
|
||||||
using MQTTnet.Client.Receiving;
|
using MQTTnet.Client.Receiving;
|
||||||
using MQTTnet.Extensions.WebSocket4Net;
|
|
||||||
using MQTTnet.Formatter;
|
using MQTTnet.Formatter;
|
||||||
using MQTTnet.Protocol;
|
using MQTTnet.Protocol;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace MQTTnet.TestApp.NetCore
|
namespace MQTTnet.TestApp.NetCore
|
||||||
{
|
{
|
||||||
@@ -18,57 +16,114 @@ namespace MQTTnet.TestApp.NetCore
|
|||||||
{
|
{
|
||||||
public static async Task RunAsync()
|
public static async Task RunAsync()
|
||||||
{
|
{
|
||||||
//MqttNetConsoleLogger.ForwardToConsole();
|
// MqttNetConsoleLogger.ForwardToConsole();
|
||||||
|
|
||||||
// iot.eclipse.org
|
// For most of these connections to work, set output target to Net5.0.
|
||||||
await ExecuteTestAsync("iot.eclipse.org TCP",
|
|
||||||
new MqttClientOptionsBuilder().WithTcpServer("iot.eclipse.org", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
|
||||||
|
|
||||||
await ExecuteTestAsync("iot.eclipse.org WS",
|
#if NET5_0_OR_GREATER
|
||||||
new MqttClientOptionsBuilder().WithWebSocketServer("iot.eclipse.org:80/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
// TLS13 is only available in Net5.0
|
||||||
|
var unsafeTls13 = new MqttClientOptionsBuilderTlsParameters
|
||||||
|
{
|
||||||
|
UseTls = true,
|
||||||
|
SslProtocol = SslProtocols.Tls13,
|
||||||
|
// Don't use this in production code. This handler simply allows any invalid certificate to work.
|
||||||
|
CertificateValidationHandler = (w) => true
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
await ExecuteTestAsync("iot.eclipse.org WS TLS",
|
// Also defining TLS12 for servers that don't seem no to support TLS13.
|
||||||
new MqttClientOptionsBuilder().WithWebSocketServer("iot.eclipse.org:443/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).WithTls().Build());
|
var unsafeTls12 = new MqttClientOptionsBuilderTlsParameters
|
||||||
|
{
|
||||||
|
UseTls = true,
|
||||||
|
SslProtocol = SslProtocols.Tls12,
|
||||||
|
// Don't use this in production code. This handler simply allows any invalid certificate to work.
|
||||||
|
CertificateValidationHandler = (w) => true
|
||||||
|
};
|
||||||
|
|
||||||
|
// mqtt.eclipseprojects.io
|
||||||
|
await ExecuteTestAsync("mqtt.eclipseprojects.io TCP",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("mqtt.eclipseprojects.io", 1883)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
|
await ExecuteTestAsync("mqtt.eclipseprojects.io WS",
|
||||||
|
new MqttClientOptionsBuilder().WithWebSocketServer("mqtt.eclipseprojects.io:80/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
|
#if NET5_0_OR_GREATER
|
||||||
|
await ExecuteTestAsync("mqtt.eclipseprojects.io WS TLS13",
|
||||||
|
new MqttClientOptionsBuilder().WithWebSocketServer("mqtt.eclipseprojects.io:443/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls13).Build());
|
||||||
|
#endif
|
||||||
|
|
||||||
// test.mosquitto.org
|
// test.mosquitto.org
|
||||||
await ExecuteTestAsync("test.mosquitto.org TCP",
|
await ExecuteTestAsync("test.mosquitto.org TCP",
|
||||||
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 1883)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
await ExecuteTestAsync("test.mosquitto.org TCP TLS",
|
await ExecuteTestAsync("test.mosquitto.org TCP - Authenticated",
|
||||||
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883).WithProtocolVersion(MqttProtocolVersion.V311).WithTls().Build());
|
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 1884)
|
||||||
|
.WithCredentials("rw", "readwrite")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
|
await ExecuteTestAsync("test.mosquitto.org TCP TLS12",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
|
||||||
|
|
||||||
|
#if NET5_0_OR_GREATER
|
||||||
|
await ExecuteTestAsync("test.mosquitto.org TCP TLS13",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8883)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls13).Build());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
await ExecuteTestAsync("test.mosquitto.org TCP TLS12 - Authenticated",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("test.mosquitto.org", 8885)
|
||||||
|
.WithCredentials("rw", "readwrite")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
|
||||||
|
|
||||||
await ExecuteTestAsync("test.mosquitto.org WS",
|
await ExecuteTestAsync("test.mosquitto.org WS",
|
||||||
new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8080/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8080/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
|
await ExecuteTestAsync("test.mosquitto.org WS TLS12",
|
||||||
|
new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8081/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
|
||||||
|
|
||||||
|
// broker.emqx.io
|
||||||
|
await ExecuteTestAsync("broker.emqx.io TCP",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 1883)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
|
await ExecuteTestAsync("broker.emqx.io TCP TLS12",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 8083)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
|
||||||
|
|
||||||
|
#if NET5_0_OR_GREATER
|
||||||
|
await ExecuteTestAsync("broker.emqx.io TCP TLS13",
|
||||||
|
new MqttClientOptionsBuilder().WithTcpServer("broker.emqx.io", 8083)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls13).Build());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
await ExecuteTestAsync("broker.emqx.io WS",
|
||||||
|
new MqttClientOptionsBuilder().WithWebSocketServer("broker.emqx.io:8083/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
|
await ExecuteTestAsync("broker.emqx.io WS TLS12",
|
||||||
|
new MqttClientOptionsBuilder().WithWebSocketServer("broker.emqx.io:8084/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).WithTls(unsafeTls12).Build());
|
||||||
|
|
||||||
await ExecuteTestAsync("test.mosquitto.org WS TLS",
|
|
||||||
new MqttClientOptionsBuilder().WithWebSocketServer("test.mosquitto.org:8081/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).WithTls().Build());
|
|
||||||
|
|
||||||
// broker.hivemq.com
|
// broker.hivemq.com
|
||||||
await ExecuteTestAsync("broker.hivemq.com TCP",
|
await ExecuteTestAsync("broker.hivemq.com TCP",
|
||||||
new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
new MqttClientOptionsBuilder().WithTcpServer("broker.hivemq.com", 1883)
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
await ExecuteTestAsync("broker.hivemq.com WS",
|
await ExecuteTestAsync("broker.hivemq.com WS",
|
||||||
new MqttClientOptionsBuilder().WithWebSocketServer("broker.hivemq.com:8000/mqtt").WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
new MqttClientOptionsBuilder().WithWebSocketServer("broker.hivemq.com:8000/mqtt")
|
||||||
|
.WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
||||||
|
|
||||||
// mqtt.swifitch.cz
|
// mqtt.swifitch.cz: Does not seem to operate any more
|
||||||
await ExecuteTestAsync("mqtt.swifitch.cz",
|
|
||||||
new MqttClientOptionsBuilder().WithTcpServer("mqtt.swifitch.cz", 1883).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
|
||||||
|
|
||||||
// CloudMQTT
|
// cloudmqtt.com: Cannot test because it does not offer a free plan any more.
|
||||||
var configFile = Path.Combine("E:\\CloudMqttTestConfig.json");
|
|
||||||
if (File.Exists(configFile))
|
|
||||||
{
|
|
||||||
var config = JsonConvert.DeserializeObject<MqttConfig>(File.ReadAllText(configFile));
|
|
||||||
|
|
||||||
await ExecuteTestAsync("CloudMQTT TCP",
|
|
||||||
new MqttClientOptionsBuilder().WithTcpServer(config.Server, config.Port).WithCredentials(config.Username, config.Password).WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
|
||||||
|
|
||||||
await ExecuteTestAsync("CloudMQTT TCP TLS",
|
|
||||||
new MqttClientOptionsBuilder().WithTcpServer(config.Server, config.SslPort).WithCredentials(config.Username, config.Password).WithTls().WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
|
||||||
|
|
||||||
await ExecuteTestAsync("CloudMQTT WS TLS",
|
|
||||||
new MqttClientOptionsBuilder().WithWebSocketServer(config.Server + ":" + config.SslWebSocketPort + "/mqtt").WithCredentials(config.Username, config.Password).WithTls().WithProtocolVersion(MqttProtocolVersion.V311).Build());
|
|
||||||
}
|
|
||||||
|
|
||||||
Write("Finished.", ConsoleColor.White);
|
Write("Finished.", ConsoleColor.White);
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
@@ -115,21 +170,5 @@ namespace MQTTnet.TestApp.NetCore
|
|||||||
Console.Write(message);
|
Console.Write(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MqttConfig
|
|
||||||
{
|
|
||||||
public string Server { get; set; }
|
|
||||||
|
|
||||||
public string Username { get; set; }
|
|
||||||
|
|
||||||
public string Password { get; set; }
|
|
||||||
|
|
||||||
public int Port { get; set; }
|
|
||||||
|
|
||||||
public int SslPort { get; set; }
|
|
||||||
|
|
||||||
public int WebSocketPort { get; set; }
|
|
||||||
|
|
||||||
public int SslWebSocketPort { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user