Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

170 linhas
5.4 KiB

  1. #if NET452 || NET461 || NETSTANDARD1_3 || NETSTANDARD2_0
  2. using System;
  3. using System.Net.Security;
  4. using System.Net.Sockets;
  5. using System.Security.Authentication;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using MQTTnet.Channel;
  12. using MQTTnet.Client;
  13. namespace MQTTnet.Implementations
  14. {
  15. public class MqttTcpChannel : IMqttChannel
  16. {
  17. private readonly MqttClientTcpOptions _options;
  18. private Socket _socket;
  19. private Stream _stream;
  20. /// <summary>
  21. /// called on client sockets are created in connect
  22. /// </summary>
  23. public MqttTcpChannel(MqttClientTcpOptions options)
  24. {
  25. _options = options ?? throw new ArgumentNullException(nameof(options));
  26. }
  27. /// <summary>
  28. /// called on server, sockets are passed in
  29. /// connect will not be called
  30. /// </summary>
  31. public MqttTcpChannel(Socket socket, SslStream sslStream)
  32. {
  33. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  34. CreateStream(sslStream);
  35. }
  36. public static Func<X509Certificate, X509Chain, SslPolicyErrors, MqttClientTcpOptions, bool> CustomCertificateValidationCallback { get; set; }
  37. public string Endpoint => _socket?.RemoteEndPoint?.ToString();
  38. public async Task ConnectAsync(CancellationToken cancellationToken)
  39. {
  40. if (_socket == null)
  41. {
  42. _socket = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true };
  43. }
  44. #if NET452 || NET461
  45. await Task.Factory.FromAsync(_socket.BeginConnect, _socket.EndConnect, _options.Server, _options.GetPort(), null).ConfigureAwait(false);
  46. #else
  47. await _socket.ConnectAsync(_options.Server, _options.GetPort()).ConfigureAwait(false);
  48. #endif
  49. SslStream sslStream = null;
  50. if (_options.TlsOptions.UseTls)
  51. {
  52. sslStream = new SslStream(new NetworkStream(_socket, true), false, InternalUserCertificateValidationCallback);
  53. await sslStream.AuthenticateAsClientAsync(_options.Server, LoadCertificates(), SslProtocols.Tls12, _options.TlsOptions.IgnoreCertificateRevocationErrors).ConfigureAwait(false);
  54. }
  55. CreateStream(sslStream);
  56. }
  57. public Task DisconnectAsync()
  58. {
  59. Dispose();
  60. return Task.FromResult(0);
  61. }
  62. public Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  63. {
  64. return _stream.ReadAsync(buffer, offset, count, cancellationToken);
  65. }
  66. public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  67. {
  68. return _stream.WriteAsync(buffer, offset, count, cancellationToken);
  69. }
  70. public void Dispose()
  71. {
  72. TryDispose(_stream, () => _stream = null);
  73. TryDispose(_socket, () => _socket = null);
  74. }
  75. private bool InternalUserCertificateValidationCallback(object sender, X509Certificate x509Certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  76. {
  77. if (CustomCertificateValidationCallback != null)
  78. {
  79. return CustomCertificateValidationCallback(x509Certificate, chain, sslPolicyErrors, _options);
  80. }
  81. if (sslPolicyErrors == SslPolicyErrors.None)
  82. {
  83. return true;
  84. }
  85. if (chain.ChainStatus.Any(c => c.Status == X509ChainStatusFlags.RevocationStatusUnknown || c.Status == X509ChainStatusFlags.Revoked || c.Status == X509ChainStatusFlags.RevocationStatusUnknown))
  86. {
  87. if (!_options.TlsOptions.IgnoreCertificateRevocationErrors)
  88. {
  89. return false;
  90. }
  91. }
  92. if (chain.ChainStatus.Any(c => c.Status == X509ChainStatusFlags.PartialChain))
  93. {
  94. if (!_options.TlsOptions.IgnoreCertificateChainErrors)
  95. {
  96. return false;
  97. }
  98. }
  99. return _options.TlsOptions.AllowUntrustedCertificates;
  100. }
  101. private X509CertificateCollection LoadCertificates()
  102. {
  103. var certificates = new X509CertificateCollection();
  104. if (_options.TlsOptions.Certificates == null)
  105. {
  106. return certificates;
  107. }
  108. foreach (var certificate in _options.TlsOptions.Certificates)
  109. {
  110. certificates.Add(new X509Certificate2(certificate));
  111. }
  112. return certificates;
  113. }
  114. private void CreateStream(Stream stream)
  115. {
  116. if (stream != null)
  117. {
  118. _stream = stream;
  119. }
  120. else
  121. {
  122. _stream = new NetworkStream(_socket, true);
  123. }
  124. }
  125. private static void TryDispose(IDisposable disposable, Action afterDispose)
  126. {
  127. try
  128. {
  129. disposable?.Dispose();
  130. }
  131. catch (ObjectDisposedException)
  132. {
  133. }
  134. catch (NullReferenceException)
  135. {
  136. }
  137. finally
  138. {
  139. afterDispose();
  140. }
  141. }
  142. }
  143. }
  144. #endif