mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 03:24:33 +08:00
Add flag for secure connections.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using MQTTnet.Adapter;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Diagnostics;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace MQTTnet.Implementations
|
||||
_bufferSize = serverOptions.DefaultEndpointOptions.BufferSize;
|
||||
|
||||
CreateStreams();
|
||||
|
||||
IsSecureConnection = socket.Information.ProtectionLevel >= SocketProtectionLevel.Tls12;
|
||||
}
|
||||
|
||||
public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
|
||||
@@ -53,6 +55,8 @@ namespace MQTTnet.Implementations
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsSecureConnection { get; }
|
||||
|
||||
public async Task ConnectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_socket == null)
|
||||
|
||||
@@ -27,6 +27,8 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
_clientOptions = clientOptions ?? throw new ArgumentNullException(nameof(clientOptions));
|
||||
_options = (MqttClientTcpOptions)clientOptions.ChannelOptions;
|
||||
|
||||
IsSecureConnection = clientOptions.ChannelOptions?.TlsOptions?.UseTls == true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,6 +39,8 @@ namespace MQTTnet.Implementations
|
||||
{
|
||||
_socket = socket ?? throw new ArgumentNullException(nameof(socket));
|
||||
|
||||
IsSecureConnection = sslStream != null;
|
||||
|
||||
CreateStream(sslStream);
|
||||
}
|
||||
|
||||
@@ -45,6 +49,8 @@ namespace MQTTnet.Implementations
|
||||
|
||||
public string Endpoint => _socket?.RemoteEndPoint?.ToString();
|
||||
|
||||
public bool IsSecureConnection { get; }
|
||||
|
||||
public async Task ConnectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (_socket == null)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Net;
|
||||
using System.Net.WebSockets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
@@ -21,14 +22,18 @@ namespace MQTTnet.Implementations
|
||||
_options = options ?? throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
public MqttWebSocketChannel(WebSocket webSocket, string endpoint)
|
||||
public MqttWebSocketChannel(WebSocket webSocket, string endpoint, bool isSecureConnection)
|
||||
{
|
||||
_webSocket = webSocket ?? throw new ArgumentNullException(nameof(webSocket));
|
||||
|
||||
Endpoint = endpoint;
|
||||
IsSecureConnection = isSecureConnection;
|
||||
}
|
||||
|
||||
public string Endpoint { get; }
|
||||
|
||||
public bool IsSecureConnection { get; private set; }
|
||||
|
||||
public async Task ConnectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var uri = _options.Uri;
|
||||
@@ -83,6 +88,8 @@ namespace MQTTnet.Implementations
|
||||
|
||||
await clientWebSocket.ConnectAsync(new Uri(uri), cancellationToken).ConfigureAwait(false);
|
||||
_webSocket = clientWebSocket;
|
||||
|
||||
IsSecureConnection = uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public async Task DisconnectAsync(CancellationToken cancellationToken)
|
||||
|
||||
Reference in New Issue
Block a user