Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

133 строки
4.1 KiB

  1. #if WINDOWS_UWP
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices.WindowsRuntime;
  7. using System.Threading.Tasks;
  8. using Windows.Networking;
  9. using Windows.Networking.Sockets;
  10. using Windows.Security.Cryptography.Certificates;
  11. using MQTTnet.Channel;
  12. using MQTTnet.Client;
  13. namespace MQTTnet.Implementations
  14. {
  15. public sealed class MqttTcpChannel : IMqttChannel, IDisposable
  16. {
  17. private readonly MqttClientTcpOptions _options;
  18. private StreamSocket _socket;
  19. public MqttTcpChannel(MqttClientTcpOptions options)
  20. {
  21. _options = options ?? throw new ArgumentNullException(nameof(options));
  22. }
  23. public MqttTcpChannel(StreamSocket socket)
  24. {
  25. _socket = socket ?? throw new ArgumentNullException(nameof(socket));
  26. CreateStreams();
  27. }
  28. public Stream SendStream { get; private set; }
  29. public Stream ReceiveStream { get; private set; }
  30. public static Func<MqttClientTcpOptions, IEnumerable<ChainValidationResult>> CustomIgnorableServerCertificateErrorsResolver { get; set; }
  31. public async Task ConnectAsync()
  32. {
  33. if (_socket == null)
  34. {
  35. _socket = new StreamSocket();
  36. }
  37. if (!_options.TlsOptions.UseTls)
  38. {
  39. await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString());
  40. }
  41. else
  42. {
  43. _socket.Control.ClientCertificate = LoadCertificate(_options);
  44. foreach (var ignorableChainValidationResult in ResolveIgnorableServerCertificateErrors())
  45. {
  46. _socket.Control.IgnorableServerCertificateErrors.Add(ignorableChainValidationResult);
  47. }
  48. await _socket.ConnectAsync(new HostName(_options.Server), _options.GetPort().ToString(), SocketProtectionLevel.Tls12);
  49. }
  50. CreateStreams();
  51. }
  52. public Task DisconnectAsync()
  53. {
  54. Dispose();
  55. return Task.FromResult(0);
  56. }
  57. public void Dispose()
  58. {
  59. SendStream?.Dispose();
  60. SendStream = null;
  61. ReceiveStream?.Dispose();
  62. ReceiveStream = null;
  63. _socket?.Dispose();
  64. _socket = null;
  65. }
  66. private void CreateStreams()
  67. {
  68. SendStream = _socket.OutputStream.AsStreamForWrite();
  69. ReceiveStream = _socket.InputStream.AsStreamForRead();
  70. }
  71. private static Certificate LoadCertificate(MqttClientTcpOptions options)
  72. {
  73. if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any())
  74. {
  75. return null;
  76. }
  77. if (options.TlsOptions.Certificates.Count > 1)
  78. {
  79. throw new NotSupportedException("Only one client certificate is supported for UWP.");
  80. }
  81. return new Certificate(options.TlsOptions.Certificates.First().AsBuffer());
  82. }
  83. private IEnumerable<ChainValidationResult> ResolveIgnorableServerCertificateErrors()
  84. {
  85. if (CustomIgnorableServerCertificateErrorsResolver != null)
  86. {
  87. return CustomIgnorableServerCertificateErrorsResolver(_options);
  88. }
  89. var result = new List<ChainValidationResult>();
  90. if (_options.TlsOptions.IgnoreCertificateRevocationErrors)
  91. {
  92. result.Add(ChainValidationResult.RevocationInformationMissing);
  93. //_socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Revoked); Not supported.
  94. result.Add(ChainValidationResult.RevocationFailure);
  95. }
  96. if (_options.TlsOptions.IgnoreCertificateChainErrors)
  97. {
  98. result.Add(ChainValidationResult.IncompleteChain);
  99. }
  100. if (_options.TlsOptions.AllowUntrustedCertificates)
  101. {
  102. result.Add(ChainValidationResult.Untrusted);
  103. }
  104. return result;
  105. }
  106. }
  107. }
  108. #endif