Improve the compatibility of MqttClientOptionsBuilder.WithConnectionUri() (#2107)

* Improve the compatibility of MqttClientOptionsBuilder.WithConnectionUri() method.

* Force UseTls to true for secure schemes.

* Add support for UnixDomainSocket.
This commit is contained in:
老九
2024-12-01 14:10:58 +01:00
committed by GitHub
parent 7d4027958c
commit 6ef6b57064
2 changed files with 35 additions and 10 deletions
@@ -1,15 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using MQTTnet.Formatter;
using MQTTnet.Packets;
using MQTTnet.Protocol;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using MQTTnet.Formatter;
using MQTTnet.Packets;
using MQTTnet.Protocol;
namespace MQTTnet; namespace MQTTnet;
@@ -138,20 +138,39 @@ public sealed class MqttClientOptionsBuilder
{ {
case "tcp": case "tcp":
case "mqtt": case "mqtt":
WithTcpServer(uri.Host, port); WithTcpServer(uri.Host, port)
.WithAddressFamily(AddressFamily.Unspecified)
.WithProtocolType(ProtocolType.Tcp)
.WithTlsOptions(o => o.UseTls(false));
break; break;
case "mqtts": case "mqtts":
WithTcpServer(uri.Host, port) WithTcpServer(uri.Host, port)
.WithTlsOptions( .WithAddressFamily(AddressFamily.Unspecified)
o => .WithProtocolType(ProtocolType.Tcp)
{ .WithTlsOptions(o => o.UseTls(true));
});
break; break;
case "ws": case "ws":
WithWebSocketServer(o => o.WithUri(uri.ToString()))
.WithAddressFamily(AddressFamily.Unspecified)
.WithProtocolType(ProtocolType.Tcp)
.WithTlsOptions(o => o.UseTls(false));
break;
case "wss": case "wss":
WithWebSocketServer(o => o.WithUri(uri.ToString())); WithWebSocketServer(o => o.WithUri(uri.ToString()))
.WithAddressFamily(AddressFamily.Unspecified)
.WithProtocolType(ProtocolType.Tcp)
.WithTlsOptions(o => o.UseTls(true));
break;
// unix:///path/to/socket
case "unix":
WithEndPoint(new UnixDomainSocketEndPoint(uri.AbsolutePath))
.WithAddressFamily(AddressFamily.Unix)
.WithProtocolType(ProtocolType.Unspecified)
.WithTlsOptions(o => o.UseTls(false));
break; break;
default: default:
@@ -66,6 +66,12 @@ public sealed class MqttClientWebSocketOptionsBuilder
public MqttClientWebSocketOptionsBuilder WithUri(string uri) public MqttClientWebSocketOptionsBuilder WithUri(string uri)
{ {
var webSocketUri = new Uri(uri, UriKind.Absolute);
if (webSocketUri.Scheme != Uri.UriSchemeWs && webSocketUri.Scheme != Uri.UriSchemeWss)
{
throw new ArgumentException("The scheme of the WebSocket Uri must be ws or wss.");
}
_webSocketOptions.Uri = uri; _webSocketOptions.Uri = uri;
return this; return this;
} }