| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Amqp |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Net; |
| | 8 | | using Microsoft.Azure.Amqp; |
| | 9 | | using Microsoft.Azure.Amqp.Sasl; |
| | 10 | | using Microsoft.Azure.Amqp.Transport; |
| | 11 | | using Microsoft.Azure.ServiceBus.Primitives; |
| | 12 | |
|
| | 13 | | internal class AmqpConnectionHelper |
| | 14 | | { |
| | 15 | | const string CbsSaslMechanismName = "MSSBCBS"; |
| | 16 | |
|
| | 17 | | public static AmqpSettings CreateAmqpSettings( |
| | 18 | | Version amqpVersion, |
| | 19 | | bool useSslStreamSecurity, |
| | 20 | | bool hasTokenProvider, |
| | 21 | | string sslHostName = null, |
| | 22 | | bool useWebSockets = false, |
| | 23 | | bool sslStreamUpgrade = false, |
| | 24 | | System.Net.NetworkCredential networkCredential = null, |
| | 25 | | bool forceTokenProvider = true) |
| | 26 | | { |
| 0 | 27 | | var amqpSettings = new AmqpSettings(); |
| 0 | 28 | | if (useSslStreamSecurity && !useWebSockets && sslStreamUpgrade) |
| | 29 | | { |
| 0 | 30 | | var tlsSettings = new TlsTransportSettings |
| 0 | 31 | | { |
| 0 | 32 | | TargetHost = sslHostName |
| 0 | 33 | | }; |
| | 34 | |
|
| 0 | 35 | | var tlsProvider = new TlsTransportProvider(tlsSettings); |
| 0 | 36 | | tlsProvider.Versions.Add(new AmqpVersion(amqpVersion)); |
| 0 | 37 | | amqpSettings.TransportProviders.Add(tlsProvider); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | if (hasTokenProvider || networkCredential != null) |
| | 41 | | { |
| 0 | 42 | | var saslTransportProvider = new SaslTransportProvider(); |
| 0 | 43 | | saslTransportProvider.Versions.Add(new AmqpVersion(amqpVersion)); |
| 0 | 44 | | amqpSettings.TransportProviders.Add(saslTransportProvider); |
| | 45 | |
|
| 0 | 46 | | if (forceTokenProvider) |
| | 47 | | { |
| 0 | 48 | | saslTransportProvider.AddHandler(new SaslAnonymousHandler(CbsSaslMechanismName)); |
| | 49 | | } |
| 0 | 50 | | else if (networkCredential != null) |
| | 51 | | { |
| 0 | 52 | | var plainHandler = new SaslPlainHandler |
| 0 | 53 | | { |
| 0 | 54 | | AuthenticationIdentity = networkCredential.UserName, |
| 0 | 55 | | Password = networkCredential.Password |
| 0 | 56 | | }; |
| 0 | 57 | | saslTransportProvider.AddHandler(plainHandler); |
| | 58 | | } |
| | 59 | | else |
| | 60 | | { |
| | 61 | | // old client behavior: keep it for validation only |
| 0 | 62 | | saslTransportProvider.AddHandler(new SaslExternalHandler()); |
| | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | var amqpTransportProvider = new AmqpTransportProvider(); |
| 0 | 67 | | amqpTransportProvider.Versions.Add(new AmqpVersion(amqpVersion)); |
| 0 | 68 | | amqpSettings.TransportProviders.Add(amqpTransportProvider); |
| | 69 | |
|
| 0 | 70 | | return amqpSettings; |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public static TransportSettings CreateTcpTransportSettings( |
| | 74 | | string networkHost, |
| | 75 | | string hostName, |
| | 76 | | int port, |
| | 77 | | bool useSslStreamSecurity, |
| | 78 | | bool sslStreamUpgrade = false, |
| | 79 | | string sslHostName = null) |
| | 80 | | { |
| 0 | 81 | | var tcpTransportSettings = new TcpTransportSettings |
| 0 | 82 | | { |
| 0 | 83 | | Host = networkHost, |
| 0 | 84 | | Port = port < Constants.WellKnownPublicPortsLimit ? AmqpConstants.DefaultSecurePort : port, |
| 0 | 85 | | ReceiveBufferSize = AmqpConstants.TransportBufferSize, |
| 0 | 86 | | SendBufferSize = AmqpConstants.TransportBufferSize |
| 0 | 87 | | }; |
| | 88 | |
|
| 0 | 89 | | TransportSettings tpSettings = tcpTransportSettings; |
| 0 | 90 | | if (useSslStreamSecurity && !sslStreamUpgrade) |
| | 91 | | { |
| 0 | 92 | | var tlsTransportSettings = new TlsTransportSettings(tcpTransportSettings) |
| 0 | 93 | | { |
| 0 | 94 | | TargetHost = sslHostName ?? hostName |
| 0 | 95 | | }; |
| 0 | 96 | | tpSettings = tlsTransportSettings; |
| | 97 | | } |
| | 98 | |
|
| 0 | 99 | | return tpSettings; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | public static TransportSettings CreateWebSocketTransportSettings( |
| | 103 | | string networkHost, |
| | 104 | | string hostName, |
| | 105 | | int port, |
| | 106 | | IWebProxy proxy) |
| | 107 | | { |
| 0 | 108 | | var uriBuilder = new UriBuilder( |
| 0 | 109 | | WebSocketConstants.WebSocketSecureScheme, |
| 0 | 110 | | networkHost, |
| 0 | 111 | | port < Constants.WellKnownPublicPortsLimit ? WebSocketConstants.WebSocketSecurePort : port, |
| 0 | 112 | | WebSocketConstants.WebSocketDefaultPath); |
| 0 | 113 | | var webSocketTransportSettings = new WebSocketTransportSettings |
| 0 | 114 | | { |
| 0 | 115 | | Uri = uriBuilder.Uri, |
| 0 | 116 | | ReceiveBufferSize = AmqpConstants.TransportBufferSize, |
| 0 | 117 | | SendBufferSize = AmqpConstants.TransportBufferSize, |
| 0 | 118 | | Proxy = proxy |
| 0 | 119 | | }; |
| | 120 | |
|
| | 121 | | TransportSettings tpSettings = webSocketTransportSettings; |
| 0 | 122 | | return tpSettings; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | public static AmqpConnectionSettings CreateAmqpConnectionSettings(uint maxFrameSize, string containerId, string |
| | 126 | | { |
| 0 | 127 | | var connectionSettings = new AmqpConnectionSettings |
| 0 | 128 | | { |
| 0 | 129 | | MaxFrameSize = maxFrameSize, |
| 0 | 130 | | ContainerId = containerId, |
| 0 | 131 | | HostName = hostName, |
| 0 | 132 | | IdleTimeOut = (uint)Constants.DefaultOperationTimeout.TotalMilliseconds |
| 0 | 133 | | }; |
| | 134 | |
|
| 0 | 135 | | connectionSettings.AddProperty("product", ClientInfo.Product); |
| 0 | 136 | | connectionSettings.AddProperty("version", ClientInfo.Version); |
| 0 | 137 | | connectionSettings.AddProperty("framework", ClientInfo.Framework); |
| 0 | 138 | | connectionSettings.AddProperty("platform", ClientInfo.Platform); |
| 0 | 139 | | return connectionSettings; |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |