25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MqttTcpChannel.Uwp.cs 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.Core.Channel;
  12. using MQTTnet.Core.Client;
  13. namespace MQTTnet.Implementations
  14. {
  15. public sealed class MqttTcpChannel : IMqttCommunicationChannel, 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