Merge pull request #364 from PitySOFT/webproxy

Add proxy settings for WebSocket connections for .NET 451 and 461 and add option to specify the SSLProtocol and CertificateValidationCallback  in TLS options, SSLProtocol default is TLS1.2
This commit is contained in:
Christian
2018-07-26 10:04:52 +02:00
committed by GitHub
6 changed files with 128 additions and 14 deletions
@@ -63,7 +63,7 @@ namespace MQTTnet.Implementations
if (_options.TlsOptions.UseTls)
{
sslStream = new SslStream(new NetworkStream(_socket, true), false, InternalUserCertificateValidationCallback);
await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), SslProtocols.Tls12, _options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), _options.TlsOptions.SslProtocol, _options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
}
CreateStream(sslStream);
@@ -6,6 +6,10 @@ using System.Threading.Tasks;
using MQTTnet.Channel;
using MQTTnet.Client;
#if NET452 || NET461
using System.Net;
#endif
namespace MQTTnet.Implementations
{
public class MqttWebSocketChannel : IMqttChannel
@@ -14,7 +18,7 @@ namespace MQTTnet.Implementations
private readonly MqttClientWebSocketOptions _options;
private WebSocket _webSocket;
public MqttWebSocketChannel(MqttClientWebSocketOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
@@ -45,6 +49,26 @@ namespace MQTTnet.Implementations
var clientWebSocket = new ClientWebSocket();
#if NET452 || NET461
// use proxy if we have an address
if (string.IsNullOrEmpty(_options.MqttClientWebSocketProxy.Address) == false)
{
var proxyUri = new Uri(_options.MqttClientWebSocketProxy.Address);
// use proxy credentials if we have them
if (string.IsNullOrEmpty(_options.MqttClientWebSocketProxy.Username) == false && string.IsNullOrEmpty(_options.MqttClientWebSocketProxy.Password) == false)
{
var credentials = new NetworkCredential(_options.MqttClientWebSocketProxy.Username, _options.MqttClientWebSocketProxy.Password, _options.MqttClientWebSocketProxy.Domain);
clientWebSocket.Options.Proxy = new WebProxy(proxyUri, _options.MqttClientWebSocketProxy.BypassOnLocal, _options.MqttClientWebSocketProxy.BypassList, credentials);
}
else
{
// use proxy without credentials
clientWebSocket.Options.Proxy = new WebProxy(proxyUri, _options.MqttClientWebSocketProxy.BypassOnLocal, _options.MqttClientWebSocketProxy.BypassList);
}
}
#endif
if (_options.RequestHeaders != null)
{
foreach (var requestHeader in _options.RequestHeaders)