internal AmqpConnector(AmqpTransportSettings amqpTransportSettings, string hostName) { AmqpTransportSettings = amqpTransportSettings; AmqpSettings = new AmqpSettings(); var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(amqpVersion_1_0_0); AmqpSettings.TransportProviders.Add(amqpTransportProvider); AmqpConnectionSettings = new AmqpConnectionSettings() { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = CommonResources.GetNewStringGuid(), HostName = hostName }; var tcpTransportSettings = new TcpTransportSettings() { Host = hostName, Port = AmqpConstants.DefaultSecurePort }; TlsTransportSettings = new TlsTransportSettings(tcpTransportSettings) { TargetHost = hostName, Certificate = null, CertificateValidationCallback = AmqpTransportSettings.RemoteCertificateValidationCallback ?? OnRemoteCertificateValidation }; if (AmqpTransportSettings.ClientCertificate != null) { TlsTransportSettings.Certificate = AmqpTransportSettings.ClientCertificate; } }
public static AmqpSettings CreateAmqpSettings( Version amqpVersion, bool useSslStreamSecurity, bool hasTokenProvider, string sslHostName = null, bool useWebSockets = false, bool sslStreamUpgrade = false, System.Net.NetworkCredential networkCredential = null, System.Net.Security.RemoteCertificateValidationCallback certificateValidationCallback = null, bool forceTokenProvider = true) { AmqpSettings settings = new AmqpSettings(); if (useSslStreamSecurity && !useWebSockets && sslStreamUpgrade) { var tlsSettings = new TlsTransportSettings { CertificateValidationCallback = certificateValidationCallback, TargetHost = sslHostName }; var tlsProvider = new TlsTransportProvider(tlsSettings); tlsProvider.Versions.Add(new AmqpVersion(amqpVersion)); settings.TransportProviders.Add(tlsProvider); } if (hasTokenProvider || networkCredential != null) { SaslTransportProvider saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(amqpVersion)); settings.TransportProviders.Add(saslProvider); if (forceTokenProvider) { saslProvider.AddHandler(new SaslAnonymousHandler(CbsSaslMechanismName)); } else if (networkCredential != null) { var plainHandler = new SaslPlainHandler { AuthenticationIdentity = networkCredential.UserName, Password = networkCredential.Password }; saslProvider.AddHandler(plainHandler); } else { // old client behavior: keep it for validation only saslProvider.AddHandler(new SaslExternalHandler()); } } AmqpTransportProvider amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(amqpVersion)); settings.TransportProviders.Add(amqpProvider); return(settings); }
public AmqpTransportBindingElement() { AmqpTransportProvider amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(new AmqpVersion(1, 0, 0)); this.AmqpSettings = new Microsoft.ServiceBus.Messaging.Amqp.AmqpSettings(); this.AmqpSettings.TransportProviders.Add(amqpTransportProvider); }
public async Task <AmqpIoTConnection> OpenConnectionAsync(TimeSpan timeout) { if (Logging.IsEnabled) { Logging.Enter(this, timeout, $"{nameof(OpenConnectionAsync)}"); } var amqpSettings = new AmqpSettings(); var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(s_amqpVersion_1_0_0); amqpSettings.TransportProviders.Add(amqpTransportProvider); var amqpConnectionSettings = new AmqpConnectionSettings() { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = CommonResources.GetNewStringGuid(), HostName = _hostName }; TimeSpan idleTimeout = _amqpTransportSettings.IdleTimeout; if (idleTimeout != null) { amqpConnectionSettings.IdleTimeOut = Convert.ToUInt32(idleTimeout.TotalMilliseconds); } var amqpIoTTransport = new AmqpIoTTransport(amqpSettings, _amqpTransportSettings, _hostName, s_disableServerCertificateValidation); TransportBase transportBase = await amqpIoTTransport.InitializeAsync(timeout).ConfigureAwait(false); try { var amqpConnection = new AmqpConnection(transportBase, amqpSettings, amqpConnectionSettings); AmqpIoTConnection amqpIoTConnection = new AmqpIoTConnection(amqpConnection); amqpConnection.Closed += amqpIoTConnection.AmqpConnectionClosed; await amqpConnection.OpenAsync(timeout).ConfigureAwait(false); if (Logging.IsEnabled) { Logging.Exit(this, timeout, $"{nameof(OpenConnectionAsync)}"); } return(amqpIoTConnection); } catch (Exception e) when(!e.IsFatal()) { transportBase?.Close(); throw; } finally { if (Logging.IsEnabled) { Logging.Exit(this, $"{nameof(OpenConnectionAsync)}"); } } }
public static AmqpSettings GetDefaultAmqpSettings( string iotHubHostName, IAuthenticator authenticator, IClientCredentialsFactory identityFactory, ILinkHandlerProvider linkHandlerProvider, IConnectionProvider connectionProvider, ICredentialsCache credentialsCache) { Preconditions.CheckNotNull(authenticator, nameof(authenticator)); Preconditions.CheckNotNull(identityFactory, nameof(identityFactory)); Preconditions.CheckNotNull(linkHandlerProvider, nameof(linkHandlerProvider)); Preconditions.CheckNonWhiteSpace(iotHubHostName, nameof(iotHubHostName)); Preconditions.CheckNotNull(connectionProvider, nameof(connectionProvider)); var settings = new AmqpSettings { AllowAnonymousConnection = true, RequireSecureTransport = true, RuntimeProvider = new AmqpRuntimeProvider(linkHandlerProvider, true, identityFactory, authenticator, iotHubHostName, connectionProvider, credentialsCache) }; // Add all transport providers we want to support. AddSaslProvider(); AddAmqpProvider(); return(settings); void AddSaslProvider() { var saslProvider = new SaslTransportProvider { MaxFrameSize = 65536, }; saslProvider.Versions.Add(Constants.AmqpVersion100); // TODO: Verify if this handler still needs to be added. It looks like at some point in the past // SASL EXTERNAL was used to do CBS. Since then we have moved away from that and are using // SASL ANONYMOUS to do CBS. So this may not be needed anymore depending on back-compat // needs (i.e. old clients that are still using EXTERNAL for CBS). // saslProvider.AddHandler(new SaslExternalHandler()); saslProvider.AddHandler(new SaslAnonymousHandler()); // CBS for other clients // This handler implements SAS key based auth. saslProvider.AddHandler(new SaslPlainHandler(new EdgeHubSaslPlainAuthenticator(authenticator, identityFactory, iotHubHostName))); settings.TransportProviders.Add(saslProvider); } void AddAmqpProvider() { var amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(Constants.AmqpVersion100); settings.TransportProviders.Add(amqpProvider); } }
static AmqpSettings CreateAmqpSettings() { var amqpSettings = new AmqpSettings(); var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(AmqpVersion_1_0_0); amqpSettings.TransportProviders.Add(amqpTransportProvider); return amqpSettings; }
public virtual AmqpClientConnection CreateConnection(Uri uri, string idScope) { AmqpSettings settings = CreateAmqpSettings(idScope); var amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(AmqpConstants.DefaultProtocolVersion); settings.TransportProviders.Add(amqpProvider); return(new AmqpClientConnection(uri, settings)); }
/// <summary> /// Creates a set of dummy settings for testing purposes. /// </summary> /// private static AmqpSettings CreateMockAmqpSettings() { var transportProvider = new AmqpTransportProvider(); transportProvider.Versions.Add(new AmqpVersion(new Version(1, 0, 0, 0))); var amqpSettings = new AmqpSettings(); amqpSettings.TransportProviders.Add(transportProvider); return(amqpSettings); }
private static AmqpSettings CreateAmqpSettings() { var amqpSettings = new AmqpSettings(); var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(s_amqpVersion_1_0_0); amqpSettings.TransportProviders.Add(amqpTransportProvider); Logging.Info(s_amqpVersion_1_0_0, nameof(CreateAmqpSettings)); return(amqpSettings); }
static AmqpTransportListener CreateListener(IEnumerable <string> addresses, AmqpSettings settings) { List <TransportListener> listeners = new List <TransportListener>(); foreach (string address in addresses) { Uri uri = new Uri(address); bool isAmqps = string.Equals(AmqpConstants.SchemeAmqps, uri.Scheme, StringComparison.OrdinalIgnoreCase); TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = uri.Host, Port = uri.Port < 0 ? (isAmqps ? AmqpConstants.DefaultSecurePort : AmqpConstants.DefaultPort) : uri.Port }; TransportListener tpListener; if (isAmqps) { var tlsProvider = settings.GetTransportProvider <TlsTransportProvider>(); if (tlsProvider == null) { throw new InvalidOperationException($"TlsTransportProvider must be in AmqpSettings.TransportProviders for '{address}'."); } if (tlsProvider.Settings.Certificate == null) { throw new InvalidOperationException($"A server certificate is required in TlsTransportProvider for '{address}'."); } TlsTransportSettings tlsSettings = new TlsTransportSettings(tcpSettings, false); tlsSettings.Certificate = tlsProvider.Settings.Certificate; tlsSettings.CertificateValidationCallback = tlsProvider.Settings.CertificateValidationCallback; tlsSettings.CheckCertificateRevocation = tlsProvider.Settings.CheckCertificateRevocation; tpListener = tlsSettings.CreateListener(); } else { tpListener = tcpSettings.CreateListener(); } listeners.Add(tpListener); } if (settings.GetTransportProvider <AmqpTransportProvider>() == null) { var amqpProvider = new AmqpTransportProvider(AmqpVersion.V100); settings.TransportProviders.Add(amqpProvider); } return(new AmqpTransportListener(listeners, settings)); }
private static AmqpSettings CreateAmqpSettings() { AmqpSettings amqpSetting = new AmqpSettings(); SaslTransportProvider saslTransportProvider = new SaslTransportProvider(); saslTransportProvider.Versions.Add(AmqpConstants.DefaultProtocolVersion); saslTransportProvider.AddHandler(new SaslExternalHandler()); amqpSetting.TransportProviders.Add(saslTransportProvider); AmqpTransportProvider amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(AmqpConstants.DefaultProtocolVersion); amqpSetting.TransportProviders.Add(amqpTransportProvider); return(amqpSetting); }
static void OnTransport(TransportAsyncCallbackArgs args) { OpenContainerAsyncResult thisPtr = (OpenContainerAsyncResult)args.UserToken; AmqpSettings settings = new AmqpSettings(); TransportProvider provider = new AmqpTransportProvider(); provider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(provider); thisPtr.connection = new AmqpConnection(args.Transport, settings, new AmqpConnectionSettings() { ContainerId = thisPtr.parent.id }); thisPtr.connection.BeginOpen(TimeSpan.MaxValue, thisPtr.PrepareAsyncCompletion(onConnectionOpen), thisPtr); }
public Listener(Container container) { this.container = container; this.connections = new List <AmqpConnection>(); this.settings = new AmqpSettings() { RuntimeProvider = this }; TransportProvider provider = new AmqpTransportProvider(); provider.Versions.Add(new AmqpVersion(1, 0, 0)); this.settings.TransportProviders.Add(provider); this.nodes = new Dictionary <string, List <LinkAsyncResult> >(StringComparer.OrdinalIgnoreCase); this.syncRoot = new object(); }
public static AmqpSettings CreateAmqpSettings( Version amqpVersion, bool useSslStreamSecurity, string sslHostName = null, bool useWebSockets = false, bool sslStreamUpgrade = false, System.Net.NetworkCredential networkCredential = null) { var amqpSettings = new AmqpSettings(); if (useSslStreamSecurity && !useWebSockets && sslStreamUpgrade) { var tlsSettings = new TlsTransportSettings { TargetHost = sslHostName }; var tlsProvider = new TlsTransportProvider(tlsSettings); tlsProvider.Versions.Add(new AmqpVersion(amqpVersion)); amqpSettings.TransportProviders.Add(tlsProvider); } if (networkCredential != null) { var saslTransportProvider = new SaslTransportProvider(); saslTransportProvider.Versions.Add(new AmqpVersion(amqpVersion)); amqpSettings.TransportProviders.Add(saslTransportProvider); if (networkCredential != null) { var plainHandler = new SaslPlainHandler { AuthenticationIdentity = networkCredential.UserName, Password = networkCredential.Password }; saslTransportProvider.AddHandler(plainHandler); } } var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(new AmqpVersion(amqpVersion)); amqpSettings.TransportProviders.Add(amqpTransportProvider); return(amqpSettings); }
/// <summary> /// Creates the settings to use for AMQP communication. /// </summary> /// /// <param name="amqpVersion">The version of AMQP to be used.</param> /// /// <returns>The settings for AMQP to use for communication with the Service Bus service.</returns> /// private static AmqpSettings CreateAmpqSettings(Version amqpVersion) { var saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(amqpVersion)); saslProvider.AddHandler(new SaslAnonymousHandler(CbsSaslHandlerName)); var amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(amqpVersion)); var settings = new AmqpSettings(); settings.TransportProviders.Add(saslProvider); settings.TransportProviders.Add(amqpProvider); return(settings); }
internal AmqpIoTConnection(AmqpTransportSettings amqpTransportSettings, string hostName, bool disableServerCertificateValidation) { _amqpTransportSettings = amqpTransportSettings; _amqpSettings = new AmqpSettings(); var amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(amqpVersion_1_0_0); _amqpSettings.TransportProviders.Add(amqpTransportProvider); _amqpConnectionSettings = new AmqpConnectionSettings() { MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = CommonResources.GetNewStringGuid(), HostName = hostName }; _amqpIoTTransport = new AmqpIoTTransport(_amqpSettings, _amqpTransportSettings, hostName, disableServerCertificateValidation); }
public static AmqpSettings GetAmqpSettings(bool client, string sslValue, bool doSslUpgrade, params SaslHandler[] saslHandlers) { AmqpSettings settings = new AmqpSettings(); #if !WINDOWS_UWP if ((client && doSslUpgrade) || (!client && sslValue != null)) { TlsTransportSettings tlsSettings = new TlsTransportSettings(); if (client) { tlsSettings.TargetHost = sslValue; tlsSettings.CertificateValidationCallback = (s, c, h, e) => { return(true); }; } else { tlsSettings.IsInitiator = false; tlsSettings.Certificate = GetCertificate(sslValue);; } TlsTransportProvider tlsProvider = new TlsTransportProvider(tlsSettings); tlsProvider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(tlsProvider); } #endif if (saslHandlers != null && saslHandlers.Length >= 1 && saslHandlers[0] != null) { SaslTransportProvider saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(1, 0, 0)); foreach (SaslHandler handler in saslHandlers) { saslProvider.AddHandler(handler); } settings.TransportProviders.Add(saslProvider); } AmqpTransportProvider amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(amqpProvider); return(settings); }
internal AmqpSettings CreateAmqpSettings(string sslHostName) { AmqpSettings amqpSetting = new AmqpSettings(); if (this.SslStreamUpgrade) { TlsTransportSettings tlsTransportSetting = new TlsTransportSettings() { CertificateValidationCallback = this.CertificateValidationCallback, TargetHost = sslHostName }; TlsTransportProvider tlsTransportProvider = new TlsTransportProvider(tlsTransportSetting); tlsTransportProvider.Versions.Add(new Microsoft.ServiceBus.Messaging.Amqp.AmqpVersion(1, 0, 0)); amqpSetting.TransportProviders.Add(tlsTransportProvider); } if (this.TokenProvider != null || this.NetworkCredential != null) { SaslTransportProvider saslTransportProvider = new SaslTransportProvider(); saslTransportProvider.Versions.Add(new Microsoft.ServiceBus.Messaging.Amqp.AmqpVersion(1, 0, 0)); amqpSetting.TransportProviders.Add(saslTransportProvider); if (this.NetworkCredential == null) { saslTransportProvider.AddHandler(new SaslExternalHandler()); } else { SaslPlainHandler saslPlainHandler = new SaslPlainHandler() { AuthenticationIdentity = this.NetworkCredential.UserName, Password = this.NetworkCredential.Password }; saslTransportProvider.AddHandler(saslPlainHandler); } } AmqpTransportProvider amqpTransportProvider = new AmqpTransportProvider(); amqpTransportProvider.Versions.Add(new Microsoft.ServiceBus.Messaging.Amqp.AmqpVersion(this.AmqpVersion)); amqpSetting.TransportProviders.Add(amqpTransportProvider); return(amqpSetting); }
public static AmqpSettings GetAmqpSettings(bool client, string sslValue, bool doSslUpgrade, params SaslHandler[] saslHandlers) { AmqpSettings settings = new AmqpSettings(); if ((client && doSslUpgrade) || (!client && sslValue != null)) { TlsTransportSettings tlsSettings = new TlsTransportSettings(); if (client) { tlsSettings.TargetHost = sslValue; tlsSettings.CertificateValidationCallback = (s, c, h, e) => { return true; }; } else { tlsSettings.IsInitiator = false; tlsSettings.Certificate = GetCertificate(sslValue); ; } TlsTransportProvider tlsProvider = new TlsTransportProvider(tlsSettings); tlsProvider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(tlsProvider); } if (saslHandlers != null && saslHandlers.Length >= 1 && saslHandlers[0] != null) { SaslTransportProvider saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(1, 0, 0)); foreach (SaslHandler handler in saslHandlers) { saslProvider.AddHandler(handler); } settings.TransportProviders.Add(saslProvider); } AmqpTransportProvider amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(amqpProvider); return settings; }
/// <summary> /// Opens a connection to the specified address. /// </summary> /// <param name="addressUri">The address Uri. User info is ignored.</param> /// <param name="saslHandler">The SASL handler which determines the SASL mechanism. Null means no SASL handshake.</param> /// <param name="timeout">The operation timeout.</param> /// <returns>An AMQP connection.</returns> public async Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, SaslHandler saslHandler, TimeSpan timeout) { TransportSettings transportSettings; if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase)) { transportSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultPort }; } else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase)) { TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultSecurePort }; var tls = new TlsTransportSettings(tcpSettings) { TargetHost = addressUri.Host }; TlsTransportProvider tlsProvider = this.settings.GetTransportProvider <TlsTransportProvider>(); if (tlsProvider != null) { tls.CertificateValidationCallback = tlsProvider.Settings.CertificateValidationCallback; tls.CheckCertificateRevocation = tlsProvider.Settings.CheckCertificateRevocation; tls.Certificate = tlsProvider.Settings.Certificate; tls.Protocols = tlsProvider.Settings.Protocols; } transportSettings = tls; } else if (addressUri.Scheme.Equals(WebSocketTransportSettings.WebSockets, StringComparison.OrdinalIgnoreCase) || addressUri.Scheme.Equals(WebSocketTransportSettings.SecureWebSockets, StringComparison.OrdinalIgnoreCase)) { transportSettings = new WebSocketTransportSettings() { Uri = addressUri }; } else { throw new NotSupportedException(addressUri.Scheme); } AmqpSettings settings = this.settings.Clone(); settings.TransportProviders.Clear(); if (saslHandler != null) { // Provider for "AMQP3100" SaslTransportProvider saslProvider = new SaslTransportProvider(AmqpVersion.V100); saslProvider.AddHandler(saslHandler); settings.TransportProviders.Add(saslProvider); } // Provider for "AMQP0100" AmqpTransportProvider amqpProvider = new AmqpTransportProvider(AmqpVersion.V100); settings.TransportProviders.Add(amqpProvider); AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings); TransportBase transport = await Task.Factory.FromAsync( (c, s) => initiator.BeginConnect(timeout, c, s), (r) => initiator.EndConnect(r), null).ConfigureAwait(false); try { AmqpConnectionSettings connectionSettings = new AmqpConnectionSettings() { ContainerId = Guid.NewGuid().ToString(), HostName = addressUri.Host }; AmqpConnection connection = new AmqpConnection(transport, settings, connectionSettings); await connection.OpenAsync(timeout).ConfigureAwait(false); return(connection); } catch { transport.Abort(); throw; } }
public async Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, SaslHandler saslHandler, TimeSpan timeout) { TransportSettings transportSettings; if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase)) { transportSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultPort }; } else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase)) { TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port > -1 ? addressUri.Port : AmqpConstants.DefaultSecurePort }; transportSettings = new TlsTransportSettings(tcpSettings) { TargetHost = addressUri.Host }; } #if NET45 else if (addressUri.Scheme.Equals(WebSocketTransport.WebSockets, StringComparison.OrdinalIgnoreCase) || addressUri.Scheme.Equals(WebSocketTransport.SecureWebSockets, StringComparison.OrdinalIgnoreCase)) { transportSettings = new WebSocketTransportSettings() { Uri = addressUri }; } #endif else { throw new NotSupportedException(addressUri.Scheme); } AmqpSettings settings = new AmqpSettings(); if (saslHandler != null) { // Provider for "AMQP3100" SaslTransportProvider saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(1, 0, 0)); saslProvider.AddHandler(saslHandler); settings.TransportProviders.Add(saslProvider); } // Provider for "AMQP0100" AmqpTransportProvider amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(new Version(1, 0, 0, 0))); settings.TransportProviders.Add(amqpProvider); AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings); TransportBase transport = await Task.Factory.FromAsync( (c, s) => initiator.BeginConnect(timeout, c, s), (r) => initiator.EndConnect(r), null); try { AmqpConnectionSettings connectionSettings = new AmqpConnectionSettings() { ContainerId = Guid.NewGuid().ToString(), HostName = addressUri.Host }; AmqpConnection connection = new AmqpConnection(transport, settings, connectionSettings); await connection.OpenAsync(timeout); return(connection); } catch { transport.Abort(); throw; } }
/// <summary> /// Performs the tasks needed to build and open a connection to the IoT Hub /// service. /// </summary> /// <param name="serviceEndpoint">The endpoint of the IoT Hub service to connect to.</param> /// <param name="iotHubName">The name of the IoT Hub to connect to.</param> /// <param name="sharedAccessKeyName">The name of the shared access key being used for authentication.</param> /// <param name="sharedAccessKey">The shared access key being used for authentication.</param> /// <param name="timeout">The maximum amount of time that establishing the connection should be allowed to take.</param> /// <returns>An <see cref="AmqpConnection" /> to the requested IoT Hub.</returns> /// <seealso href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-amqp-support"/> private static async Task <AmqpConnection> CreateAndOpenConnectionAsync( Uri serviceEndpoint, string iotHubName, string sharedAccessKeyName, string sharedAccessKey, TimeSpan timeout) { var hostName = serviceEndpoint.Host; var userName = $"{ sharedAccessKeyName }@sas.root.{ iotHubName }"; var signature = BuildSignature($"{ hostName }{ serviceEndpoint.AbsolutePath }", sharedAccessKeyName, sharedAccessKey, TimeSpan.FromMinutes(5)); var port = 5671; // Create the layers of settings needed to establish the connection. var amqpVersion = new Version(1, 0, 0, 0); var tcpSettings = new TcpTransportSettings { Host = hostName, Port = port, ReceiveBufferSize = AmqpConstants.TransportBufferSize, SendBufferSize = AmqpConstants.TransportBufferSize }; var transportSettings = new TlsTransportSettings(tcpSettings) { TargetHost = hostName, }; var connectionSettings = new AmqpConnectionSettings { IdleTimeOut = (uint)TimeSpan.FromMinutes(1).TotalMilliseconds, MaxFrameSize = AmqpConstants.DefaultMaxFrameSize, ContainerId = Guid.NewGuid().ToString(), HostName = hostName }; var saslProvider = new SaslTransportProvider(); saslProvider.Versions.Add(new AmqpVersion(amqpVersion)); saslProvider.AddHandler(new SaslPlainHandler { AuthenticationIdentity = userName, Password = signature }); var amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(amqpVersion)); var amqpSettings = new AmqpSettings(); amqpSettings.TransportProviders.Add(saslProvider); amqpSettings.TransportProviders.Add(amqpProvider); // Create and open the connection, respecting the timeout constraint // that was received. var stopWatch = Stopwatch.StartNew(); var initiator = new AmqpTransportInitiator(amqpSettings, transportSettings); var transport = await initiator.ConnectTaskAsync(timeout).ConfigureAwait(false); try { var connection = new AmqpConnection(transport, amqpSettings, connectionSettings); await connection.OpenAsync(timeout.Subtract(stopWatch.Elapsed)).ConfigureAwait(false); return(connection); } catch { transport.Abort(); throw; } finally { stopWatch.Stop(); } }
public void Start() { // create and initialize AmqpSettings AmqpSettings settings = new AmqpSettings(); X509Certificate2 certificate = this.sslValue == null ? null : GetCertificate(this.sslValue); settings.RuntimeProvider = this; SaslHandler saslHandler; if (this.userInfo != null) { string[] creds = this.userInfo.Split(':'); string usernanme = Uri.UnescapeDataString(creds[0]); string password = creds.Length == 1 ? string.Empty : Uri.UnescapeDataString(creds[1]); saslHandler = new SaslPlainHandler(new TestPlainAuthenticator(this.userInfo, password)); } else { saslHandler = new SaslAnonymousHandler(); } SaslTransportProvider saslProvider = new SaslTransportProvider(AmqpVersion.V100); saslProvider.AddHandler(saslHandler); settings.TransportProviders.Add(saslProvider); AmqpTransportProvider amqpProvider = new AmqpTransportProvider(AmqpVersion.V100); settings.TransportProviders.Add(amqpProvider); // create and initialize transport listeners TransportListener[] listeners = new TransportListener[this.endpoints.Count]; for (int i = 0; i < this.endpoints.Count; i++) { Uri addressUri = new Uri(this.endpoints[i]); if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase)) { if (certificate == null) { throw new InvalidOperationException("/cert option was not set when amqps address is specified."); } TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port }; TlsTransportSettings tlsSettings = new TlsTransportSettings(tcpSettings) { Certificate = certificate, IsInitiator = false }; listeners[i] = tlsSettings.CreateListener(); } else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase)) { TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port }; listeners[i] = tcpSettings.CreateListener(); } else if (addressUri.Scheme.Equals("ws", StringComparison.OrdinalIgnoreCase)) { WebSocketTransportSettings wsSettings = new WebSocketTransportSettings() { Uri = addressUri }; listeners[i] = wsSettings.CreateListener(); } else { throw new NotSupportedException(addressUri.Scheme); } } this.settings = settings; this.transportListener = new AmqpTransportListener(listeners, settings); this.transportListener.Listen(this.OnAcceptTransport); }
public TestAmqpBroker(IList <string> endpoints, string userInfo, string sslValue, string[] queues) { this.containerId = "TestAmqpBroker-P" + Process.GetCurrentProcess().Id; this.maxFrameSize = 64 * 1024; this.txnManager = new TxnManager(); this.connections = new Dictionary <SequenceNumber, AmqpConnection>(); this.queues = new Dictionary <string, TestQueue>(); if (queues != null) { foreach (string q in queues) { this.queues.Add(q, new TestQueue(this)); } } else { this.implicitQueue = true; } // create and initialize AmqpSettings AmqpSettings settings = new AmqpSettings(); X509Certificate2 certificate = sslValue == null ? null : GetCertificate(sslValue); settings.RuntimeProvider = this; SaslHandler saslHandler; if (userInfo != null) { string[] creds = userInfo.Split(':'); string usernanme = Uri.UnescapeDataString(creds[0]); string password = creds.Length == 1 ? string.Empty : Uri.UnescapeDataString(creds[1]); saslHandler = new SaslPlainHandler(new TestPlainAuthenticator(userInfo, password)); } else { saslHandler = new SaslAnonymousHandler(); } SaslTransportProvider saslProvider = new SaslTransportProvider(); saslProvider.AddHandler(saslHandler); saslProvider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(saslProvider); AmqpTransportProvider amqpProvider = new AmqpTransportProvider(); amqpProvider.Versions.Add(new AmqpVersion(1, 0, 0)); settings.TransportProviders.Add(amqpProvider); // create and initialize transport listeners TransportListener[] listeners = new TransportListener[endpoints.Count]; for (int i = 0; i < endpoints.Count; i++) { Uri addressUri = new Uri(endpoints[i]); if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqps, StringComparison.OrdinalIgnoreCase)) { if (certificate == null) { throw new InvalidOperationException("/cert option was not set when amqps address is specified."); } TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port }; TlsTransportSettings tlsSettings = new TlsTransportSettings(tcpSettings) { Certificate = certificate, IsInitiator = false }; listeners[i] = tlsSettings.CreateListener(); } else if (addressUri.Scheme.Equals(AmqpConstants.SchemeAmqp, StringComparison.OrdinalIgnoreCase)) { TcpTransportSettings tcpSettings = new TcpTransportSettings() { Host = addressUri.Host, Port = addressUri.Port }; listeners[i] = tcpSettings.CreateListener(); } #if !NETSTANDARD else if (addressUri.Scheme.Equals("ws", StringComparison.OrdinalIgnoreCase)) { WebSocketTransportSettings wsSettings = new WebSocketTransportSettings() { Uri = addressUri }; listeners[i] = wsSettings.CreateListener(); } #endif else { throw new NotSupportedException(addressUri.Scheme); } } this.transportListener = new AmqpTransportListener(listeners, settings); this.settings = settings; }