mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 19:13:11 +08:00
Add flag for secure connections.
This commit is contained in:
@@ -20,6 +20,7 @@ namespace MQTTnet.AspNetCore
|
||||
}
|
||||
|
||||
public string Endpoint => Connection.ConnectionId;
|
||||
public bool IsSecureConnection => false; // TODO: Fix detection.
|
||||
public ConnectionContext Connection { get; }
|
||||
public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
|
||||
public event EventHandler ReadingPacketStarted;
|
||||
|
||||
@@ -38,13 +38,14 @@ namespace MQTTnet.AspNetCore
|
||||
if (webSocket == null) throw new ArgumentNullException(nameof(webSocket));
|
||||
|
||||
var endpoint = $"{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}";
|
||||
var channel = new MqttWebSocketChannel(webSocket, endpoint);
|
||||
var clientAdapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(), _logger.CreateChildLogger(nameof(MqttWebSocketServerAdapter)));
|
||||
|
||||
|
||||
var clientCertificate = await httpContext.Connection.GetClientCertificateAsync().ConfigureAwait(false);
|
||||
var isSecureConnection = clientCertificate != null;
|
||||
clientCertificate?.Dispose();
|
||||
|
||||
var channel = new MqttWebSocketChannel(webSocket, endpoint, isSecureConnection);
|
||||
var clientAdapter = new MqttChannelAdapter(channel, new MqttPacketFormatterAdapter(), _logger.CreateChildLogger(nameof(MqttWebSocketServerAdapter)));
|
||||
|
||||
var eventArgs = new MqttServerAdapterClientAcceptedEventArgs(clientAdapter);
|
||||
ClientAcceptedHandler?.Invoke(eventArgs);
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ namespace MQTTnet.Server.Mqtt
|
||||
{
|
||||
{ "client_id", context.ClientId },
|
||||
{ "endpoint", context.Endpoint },
|
||||
{ "is_secure_connection", context.IsSecureConnection },
|
||||
{ "username", context.Username },
|
||||
{ "password", context.Password },
|
||||
{ "result", PythonConvert.Pythonfy(context.ReturnCode) }
|
||||
|
||||
@@ -10,6 +10,8 @@ namespace MQTTnet.Adapter
|
||||
{
|
||||
string Endpoint { get; }
|
||||
|
||||
bool IsSecureConnection { get; }
|
||||
|
||||
MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
|
||||
|
||||
event EventHandler ReadingPacketStarted;
|
||||
|
||||
@@ -42,6 +42,8 @@ namespace MQTTnet.Adapter
|
||||
|
||||
public string Endpoint => _channel.Endpoint;
|
||||
|
||||
public bool IsSecureConnection => _channel.IsSecureConnection;
|
||||
|
||||
public MqttPacketFormatterAdapter PacketFormatterAdapter { get; }
|
||||
|
||||
public event EventHandler ReadingPacketStarted;
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace MQTTnet.Channel
|
||||
public interface IMqttChannel : IDisposable
|
||||
{
|
||||
string Endpoint { get; }
|
||||
bool IsSecureConnection { get; }
|
||||
|
||||
Task ConnectAsync(CancellationToken cancellationToken);
|
||||
Task DisconnectAsync(CancellationToken cancellationToken);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -16,6 +16,8 @@ namespace MQTTnet.Internal
|
||||
|
||||
public string Endpoint { get; } = "<Test channel>";
|
||||
|
||||
public bool IsSecureConnection { get; } = false;
|
||||
|
||||
public Task ConnectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
|
||||
@@ -298,7 +298,8 @@ namespace MQTTnet.Server
|
||||
connectPacket.Username,
|
||||
connectPacket.Password,
|
||||
connectPacket.WillMessage,
|
||||
clientAdapter.Endpoint);
|
||||
clientAdapter.Endpoint,
|
||||
clientAdapter.IsSecureConnection);
|
||||
|
||||
var connectionValidator = _options.ConnectionValidator;
|
||||
|
||||
|
||||
@@ -4,13 +4,14 @@ namespace MQTTnet.Server
|
||||
{
|
||||
public class MqttConnectionValidatorContext
|
||||
{
|
||||
public MqttConnectionValidatorContext(string clientId, string username, string password, MqttApplicationMessage willMessage, string endpoint)
|
||||
public MqttConnectionValidatorContext(string clientId, string username, string password, MqttApplicationMessage willMessage, string endpoint, bool isSecureConnection)
|
||||
{
|
||||
ClientId = clientId;
|
||||
Username = username;
|
||||
Password = password;
|
||||
WillMessage = willMessage;
|
||||
Endpoint = endpoint;
|
||||
IsSecureConnection = isSecureConnection;
|
||||
}
|
||||
|
||||
public string ClientId { get; }
|
||||
@@ -23,6 +24,8 @@ namespace MQTTnet.Server
|
||||
|
||||
public string Endpoint { get; }
|
||||
|
||||
public bool IsSecureConnection { get; }
|
||||
|
||||
public MqttConnectReturnCode ReturnCode { get; set; } = MqttConnectReturnCode.ConnectionAccepted;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user