MqttWebSocketChannel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using MQTTnet.Channel;
  2. using MQTTnet.Client.Options;
  3. using MQTTnet.Internal;
  4. using System;
  5. using System.Net;
  6. using System.Net.WebSockets;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MQTTnet.Implementations
  11. {
  12. public sealed class MqttWebSocketChannel : IMqttChannel
  13. {
  14. readonly MqttClientWebSocketOptions _options;
  15. AsyncLock _sendLock = new AsyncLock();
  16. WebSocket _webSocket;
  17. public MqttWebSocketChannel(MqttClientWebSocketOptions options)
  18. {
  19. _options = options ?? throw new ArgumentNullException(nameof(options));
  20. }
  21. public MqttWebSocketChannel(WebSocket webSocket, string endpoint, bool isSecureConnection, X509Certificate2 clientCertificate)
  22. {
  23. _webSocket = webSocket ?? throw new ArgumentNullException(nameof(webSocket));
  24. Endpoint = endpoint;
  25. IsSecureConnection = isSecureConnection;
  26. ClientCertificate = clientCertificate;
  27. }
  28. public string Endpoint { get; }
  29. public bool IsSecureConnection { get; private set; }
  30. public X509Certificate2 ClientCertificate { get; private set; }
  31. public async Task ConnectAsync(CancellationToken cancellationToken)
  32. {
  33. var uri = _options.Uri;
  34. if (!uri.StartsWith("ws://", StringComparison.OrdinalIgnoreCase) && !uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase))
  35. {
  36. if (_options.TlsOptions?.UseTls == false)
  37. {
  38. uri = "ws://" + uri;
  39. }
  40. else
  41. {
  42. uri = "wss://" + uri;
  43. }
  44. }
  45. var clientWebSocket = new ClientWebSocket();
  46. try
  47. {
  48. SetupClientWebSocket(clientWebSocket);
  49. await clientWebSocket.ConnectAsync(new Uri(uri), cancellationToken).ConfigureAwait(false);
  50. }
  51. catch (Exception)
  52. {
  53. // Prevent a memory leak when always creating new instance which will fail while connecting.
  54. clientWebSocket.Dispose();
  55. throw;
  56. }
  57. _webSocket = clientWebSocket;
  58. IsSecureConnection = uri.StartsWith("wss://", StringComparison.OrdinalIgnoreCase);
  59. }
  60. public async Task DisconnectAsync(CancellationToken cancellationToken)
  61. {
  62. if (_webSocket == null)
  63. {
  64. return;
  65. }
  66. if (_webSocket.State == WebSocketState.Open || _webSocket.State == WebSocketState.Connecting)
  67. {
  68. await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, cancellationToken).ConfigureAwait(false);
  69. }
  70. Cleanup();
  71. }
  72. public async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  73. {
  74. var response = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken).ConfigureAwait(false);
  75. return response.Count;
  76. }
  77. public async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  78. {
  79. // The lock is required because the client will throw an exception if _SendAsync_ is
  80. // called from multiple threads at the same time. But this issue only happens with several
  81. // framework versions.
  82. if (_sendLock == null)
  83. {
  84. return;
  85. }
  86. using (await _sendLock.WaitAsync(cancellationToken).ConfigureAwait(false))
  87. {
  88. await _webSocket.SendAsync(new ArraySegment<byte>(buffer, offset, count), WebSocketMessageType.Binary, true, cancellationToken).ConfigureAwait(false);
  89. }
  90. }
  91. public void Dispose()
  92. {
  93. Cleanup();
  94. }
  95. void SetupClientWebSocket(ClientWebSocket clientWebSocket)
  96. {
  97. if (_options.ProxyOptions != null)
  98. {
  99. clientWebSocket.Options.Proxy = CreateProxy();
  100. }
  101. if (_options.RequestHeaders != null)
  102. {
  103. foreach (var requestHeader in _options.RequestHeaders)
  104. {
  105. clientWebSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value);
  106. }
  107. }
  108. if (_options.SubProtocols != null)
  109. {
  110. foreach (var subProtocol in _options.SubProtocols)
  111. {
  112. clientWebSocket.Options.AddSubProtocol(subProtocol);
  113. }
  114. }
  115. if (_options.CookieContainer != null)
  116. {
  117. clientWebSocket.Options.Cookies = _options.CookieContainer;
  118. }
  119. if (_options.TlsOptions?.UseTls == true && _options.TlsOptions?.Certificates != null)
  120. {
  121. clientWebSocket.Options.ClientCertificates = new X509CertificateCollection();
  122. foreach (var certificate in _options.TlsOptions.Certificates)
  123. {
  124. #if WINDOWS_UWP
  125. clientWebSocket.Options.ClientCertificates.Add(new X509Certificate(certificate));
  126. #else
  127. clientWebSocket.Options.ClientCertificates.Add(certificate);
  128. #endif
  129. }
  130. }
  131. var certificateValidationHandler = _options.TlsOptions?.CertificateValidationHandler;
  132. #if NETSTANDARD2_1
  133. if (certificateValidationHandler != null)
  134. {
  135. clientWebSocket.Options.RemoteCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors) =>
  136. {
  137. // TODO: Find a way to add client options to same callback. Problem is that they have a different type.
  138. var context = new MqttClientCertificateValidationCallbackContext
  139. {
  140. Certificate = certificate,
  141. Chain = chain,
  142. SslPolicyErrors = sslPolicyErrors,
  143. ClientOptions = _options
  144. };
  145. return certificateValidationHandler(context);
  146. });
  147. }
  148. #else
  149. if (certificateValidationHandler != null)
  150. {
  151. throw new NotSupportedException("The remote certificate validation callback for Web Sockets is only supported for netstandard 2.1+");
  152. }
  153. #endif
  154. }
  155. void Cleanup()
  156. {
  157. _sendLock?.Dispose();
  158. _sendLock = null;
  159. try
  160. {
  161. _webSocket?.Dispose();
  162. }
  163. catch (ObjectDisposedException)
  164. {
  165. }
  166. finally
  167. {
  168. _webSocket = null;
  169. }
  170. }
  171. IWebProxy CreateProxy()
  172. {
  173. if (string.IsNullOrEmpty(_options.ProxyOptions?.Address))
  174. {
  175. return null;
  176. }
  177. #if WINDOWS_UWP
  178. throw new NotSupportedException("Proxies are not supported in UWP.");
  179. #elif NETSTANDARD1_3
  180. throw new NotSupportedException("Proxies are not supported in netstandard 1.3.");
  181. #else
  182. var proxyUri = new Uri(_options.ProxyOptions.Address);
  183. if (!string.IsNullOrEmpty(_options.ProxyOptions.Username) && !string.IsNullOrEmpty(_options.ProxyOptions.Password))
  184. {
  185. var credentials = new NetworkCredential(_options.ProxyOptions.Username, _options.ProxyOptions.Password, _options.ProxyOptions.Domain);
  186. return new WebProxy(proxyUri, _options.ProxyOptions.BypassOnLocal, _options.ProxyOptions.BypassList, credentials);
  187. }
  188. return new WebProxy(proxyUri, _options.ProxyOptions.BypassOnLocal, _options.ProxyOptions.BypassList);
  189. #endif
  190. }
  191. }
  192. }