Add flag for secure connections.

This commit is contained in:
Christian Kratky
2019-03-10 19:52:55 +01:00
parent 1e697a94a1
commit 562a5304f6
16 changed files with 45 additions and 9 deletions
@@ -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)