Пример #1
0
        public Task <AmqpConnection> OpenConnectionAsync(Uri addressUri, TimeSpan timeout)
        {
            SaslHandler saslHandler = null;

            if (!string.IsNullOrEmpty(addressUri.UserInfo))
            {
                string[] parts = addressUri.UserInfo.Split(':');
                if (parts.Length > 2)
                {
                    throw new ArgumentException("addressUri.UserInfo " + addressUri.UserInfo);
                }

                string userName = Uri.UnescapeDataString(parts[0]);
                string password = parts.Length > 1 ? Uri.UnescapeDataString(parts[1]) : string.Empty;

#if !PCL
                saslHandler = new SaslPlainHandler()
                {
                    AuthenticationIdentity = userName, Password = password
                };
#endif
            }

            return(OpenConnectionAsync(addressUri, saslHandler, timeout));
        }
Пример #2
0
        private async Task <StreamFeatures> DoAuthenicateAsync(Mechanisms mechanisms, CancellationToken cancellationToken)
        {
            XmppSessionState.Value = SessionState.Authenticating;
            var res = await SaslHandler.AuthenticateAsync(mechanisms, this, cancellationToken);

            if (res is Success)
            {
                XmppSessionState.Value = SessionState.Authenticated;
                return(await ResetStreamAsync(cancellationToken));
            }
            else //if (res is Failure)
            {
                throw new AuthenticationException(res);
            }
        }
Пример #3
0
        public void ValidateSettingsTest()
        {
            // Arrange
            string iotHubHostName      = "foo.azure-devices.net";
            var    authenticator       = Mock.Of <IAuthenticator>();
            var    identityFactory     = Mock.Of <IClientCredentialsFactory>();
            var    linkHandlerProvider = Mock.Of <ILinkHandlerProvider>();
            var    connectionProvider  = Mock.Of <IConnectionProvider>();

            // Act
            AmqpSettings settings = AmqpSettingsProvider.GetDefaultAmqpSettings(iotHubHostName, authenticator, identityFactory, linkHandlerProvider, connectionProvider, new NullCredentialsCache());

            // Assert
            Assert.NotNull(settings);
            Assert.Equal(2, settings.TransportProviders.Count);

            var saslTransportProvider = settings.GetTransportProvider <SaslTransportProvider>();

            Assert.NotNull(saslTransportProvider);

            SaslHandler anonHandler = saslTransportProvider.GetHandler("ANONYMOUS", false);

            Assert.NotNull(anonHandler);

            SaslHandler plainHandler = saslTransportProvider.GetHandler("PLAIN", false);

            Assert.NotNull(plainHandler);

            SaslHandler cbsHandler = saslTransportProvider.GetHandler(Amqp.Constants.ServiceBusCbsSaslMechanismName, false);

            Assert.NotNull(cbsHandler);

            var amqpTransportProvider = settings.GetTransportProvider <AmqpTransportProvider>();

            Assert.NotNull(amqpTransportProvider);

            Assert.Equal(Amqp.Constants.AmqpVersion100, amqpTransportProvider.Versions[0]);
        }
Пример #4
0
        public static AmqpConnection CreateConnection(Uri uri, string sslHost, bool doSslUpgrade, SaslHandler saslHandler, int maxFrameSize, uint?idleTimeoutMs = null)
        {
            if (uri.IsSecureTransport() && sslHost == null)
            {
                sslHost = uri.Host;
            }

            return(AmqpUtils.CreateConnection(uri.Host, uri.Port, sslHost, doSslUpgrade, saslHandler, maxFrameSize, idleTimeoutMs));
        }
Пример #5
0
        public static AmqpConnection CreateConnection(string host, int port, string sslHost, bool doSslUpgrade, SaslHandler saslHandler, int maxFrameSize,
                                                      uint?idleTimeoutMs = null)
        {
            AmqpSettings           settings     = GetAmqpSettings(true, sslHost, doSslUpgrade, saslHandler);
            TransportBase          transport    = CreateTransport(host, port, sslHost, doSslUpgrade, saslHandler);
            AmqpConnectionSettings connSettings = GetConnectionSettings(maxFrameSize);

            if (idleTimeoutMs != null)
            {
                connSettings.IdleTimeOut = idleTimeoutMs;
            }
            connSettings.HostName = host;
            return(new AmqpConnection(transport, settings, connSettings));
        }
Пример #6
0
        public static TransportBase CreateTransport(string host, int port, string sslHost, bool doSslUpgrade, SaslHandler saslHandler)
        {
            AmqpSettings settings = GetAmqpSettings(true, sslHost, doSslUpgrade, saslHandler);

            TransportSettings transportSettings = GetTcpSettings(host, port, false);

            if (!doSslUpgrade && sslHost != null)
            {
                TlsTransportSettings tlsSettings = new TlsTransportSettings(transportSettings);
                tlsSettings.TargetHost = sslHost;
                tlsSettings.CertificateValidationCallback = (s, c, h, e) => { return(true); };
                transportSettings = tlsSettings;
            }

            ManualResetEvent           complete  = new ManualResetEvent(false);
            AmqpTransportInitiator     initiator = new AmqpTransportInitiator(settings, transportSettings);
            TransportAsyncCallbackArgs args      = new TransportAsyncCallbackArgs();

            args.CompletedCallback = (a) => { complete.Set(); };
            initiator.ConnectAsync(TimeSpan.FromSeconds(120), args);

            complete.WaitOne();
            complete.Dispose();

            if (args.Exception != null)
            {
                throw args.Exception;
            }

            return(args.Transport);
        }
Пример #7
0
        public static AmqpTransportListener CreateListener(string host, int port, string certFindValue, bool doSslUpgrade, SaslHandler saslHandler)
        {
            AmqpSettings settings = GetAmqpSettings(false, certFindValue, doSslUpgrade, saslHandler);

            TransportSettings transportSettings = GetTcpSettings(host, port, true);

            if (!doSslUpgrade && certFindValue != null)
            {
                TlsTransportSettings tlsSettings = new TlsTransportSettings(transportSettings, false);
                tlsSettings.Certificate = GetCertificate(certFindValue);
                transportSettings       = tlsSettings;
            }

            TransportListener listener = transportSettings.CreateListener();

            return(new AmqpTransportListener(new TransportListener[] { listener }, settings));
        }
Пример #8
0
 public static AmqpConnection CreateConnection(Uri uri, string sslHost, bool doSslUpgrade, SaslHandler saslHandler, int maxFrameSize, uint? idleTimeoutMs = null)
 {
     return AmqpUtils.CreateConnection(uri.Host, uri.Port, sslHost, doSslUpgrade, saslHandler, maxFrameSize, idleTimeoutMs);
 }
Пример #9
0
 public static AmqpConnection CreateConnection(string host, int port, string sslHost, bool doSslUpgrade, SaslHandler saslHandler, int maxFrameSize,
     uint? idleTimeoutMs = null)
 {
     AmqpSettings settings = GetAmqpSettings(true, sslHost, doSslUpgrade, saslHandler);
     TransportBase transport = CreateTransport(host, port, sslHost, doSslUpgrade, saslHandler);
     AmqpConnectionSettings connSettings = GetConnectionSettings(maxFrameSize);
     if (idleTimeoutMs != null)
     {
         connSettings.IdleTimeOut = idleTimeoutMs;
     }
     connSettings.HostName = host;
     return new AmqpConnection(transport, settings, connSettings);
 }
Пример #10
0
        public static TransportBase CreateTransport(string host, int port, string sslHost, bool doSslUpgrade, SaslHandler saslHandler)
        {
            AmqpSettings settings = GetAmqpSettings(true, sslHost, doSslUpgrade, saslHandler);

            TransportSettings transportSettings = GetTcpSettings(host, port, false);
            if (!doSslUpgrade && sslHost != null)
            {
                TlsTransportSettings tlsSettings = new TlsTransportSettings(transportSettings);
                tlsSettings.TargetHost = sslHost;
                tlsSettings.CertificateValidationCallback = (s, c, h, e) => { return true; };
                transportSettings = tlsSettings;
            }

            ManualResetEvent complete = new ManualResetEvent(false);
            AmqpTransportInitiator initiator = new AmqpTransportInitiator(settings, transportSettings);
            TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
            args.CompletedCallback = (a) => { complete.Set(); };
            initiator.ConnectAsync(TimeSpan.FromSeconds(120), args);

            complete.WaitOne();
            complete.Dispose();

            if (args.Exception != null)
            {
                throw args.Exception;
            }

            return args.Transport;
        }
Пример #11
0
        public static AmqpTransportListener CreateListener(string host, int port, string certFindValue, bool doSslUpgrade, SaslHandler saslHandler)
        {
            AmqpSettings settings = GetAmqpSettings(false, certFindValue, doSslUpgrade, saslHandler);

            TransportSettings transportSettings = GetTcpSettings(host, port, true);
            if (!doSslUpgrade && certFindValue != null)
            {
                TlsTransportSettings tlsSettings = new TlsTransportSettings(transportSettings, false);
                tlsSettings.Certificate = GetCertificate(certFindValue);
                transportSettings = tlsSettings;
            }

            TransportListener listener = transportSettings.CreateListener();
            return new AmqpTransportListener(new TransportListener[] { listener }, settings);
        }
Пример #12
0
        /// <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;
            }
        }
Пример #13
0
        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;
            }
        }