Skip to content

Commit

Permalink
Fix wrong usage of TLS options
Browse files Browse the repository at this point in the history
  • Loading branch information
chkr1011 committed Sep 6, 2023
1 parent 0d3dd96 commit 888714c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public Task ConnectAsync(CancellationToken cancellationToken)
var uri = _webSocketOptions.Uri;
if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase))
{
if (_webSocketOptions.TlsOptions?.UseTls == false)
if (_webSocketOptions.TlsOptions?.UseTls == true)
{
uri = "ws://" + uri;
uri = "wss://" + uri;
}
else
{
uri = "wss://" + uri;
uri = "ws://" + uri;
}
}

Expand Down
7 changes: 6 additions & 1 deletion Source/MQTTnet/Client/Options/MqttClientOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public MqttClientOptions Build()
throw new InvalidOperationException("A channel must be set.");
}

var tlsOptions = _tlsOptions;
// The user can specify the TCP options with already configured TLS options
// or start with TLS settings not knowing which transport will be used (depending
// on the order of called methods from the builder).
// The builder prefers the explicitly set TLS options!
var tlsOptions = _tlsOptions ?? _tcpOptions?.TlsOptions;

if (_tlsParameters != null)
{
if (_tlsParameters?.UseTls == true)
Expand Down
6 changes: 3 additions & 3 deletions Source/MQTTnet/Implementations/MqttWebSocketChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public async Task ConnectAsync(CancellationToken cancellationToken)
var uri = _options.Uri;
if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase))
{
if (_options.TlsOptions?.UseTls == false)
if (_options.TlsOptions?.UseTls == true)
{
uri = "ws://" + uri;
uri = "wss://" + uri;
}
else
{
uri = "wss://" + uri;
uri = "ws://" + uri;
}
}

Expand Down

0 comments on commit 888714c

Please sign in to comment.