From 6ef6b57064212ad7e99eeb5bc982e605c91336e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sun, 1 Dec 2024 21:10:58 +0800 Subject: [PATCH] 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. --- .../Options/MqttClientOptionsBuilder.cs | 39 ++++++++++++++----- .../MqttClientWebSocketOptionsBuilder.cs | 6 +++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs b/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs index 0afb64de..4d0f486e 100644 --- a/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs +++ b/Source/MQTTnet/Options/MqttClientOptionsBuilder.cs @@ -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. // See the LICENSE file in the project root for more information. +using MQTTnet.Formatter; +using MQTTnet.Packets; +using MQTTnet.Protocol; using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; -using MQTTnet.Formatter; -using MQTTnet.Packets; -using MQTTnet.Protocol; namespace MQTTnet; @@ -138,20 +138,39 @@ public sealed class MqttClientOptionsBuilder { case "tcp": case "mqtt": - WithTcpServer(uri.Host, port); + WithTcpServer(uri.Host, port) + .WithAddressFamily(AddressFamily.Unspecified) + .WithProtocolType(ProtocolType.Tcp) + .WithTlsOptions(o => o.UseTls(false)); break; case "mqtts": WithTcpServer(uri.Host, port) - .WithTlsOptions( - o => - { - }); + .WithAddressFamily(AddressFamily.Unspecified) + .WithProtocolType(ProtocolType.Tcp) + .WithTlsOptions(o => o.UseTls(true)); break; case "ws": + WithWebSocketServer(o => o.WithUri(uri.ToString())) + .WithAddressFamily(AddressFamily.Unspecified) + .WithProtocolType(ProtocolType.Tcp) + .WithTlsOptions(o => o.UseTls(false)); + break; + 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; default: diff --git a/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs b/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs index 59ec023c..6d334e80 100644 --- a/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs +++ b/Source/MQTTnet/Options/MqttClientWebSocketOptionsBuilder.cs @@ -66,6 +66,12 @@ public sealed class MqttClientWebSocketOptionsBuilder 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; return this; }