protected override DescribedList OnCommand(DescribedList command)
            {
                if (this.innerProfile == null)
                {
                    if (command.Descriptor.Code == Codec.SaslInit.Code)
                    {
                        var init = (SaslInit)command;
                        for (int i = 0; i < this.listener.saslMechanisms.Length; i++)
                        {
                            if (this.listener.saslMechanisms[i].Name == (string)init.Mechanism)
                            {
                                this.innerProfile = this.listener.saslMechanisms[i].CreateProfile();
                                break;
                            }
                        }

                        if (this.innerProfile == null)
                        {
                            throw new AmqpException(ErrorCode.NotImplemented, init.Mechanism);
                        }
                    }
                    else
                    {
                        throw new AmqpException(ErrorCode.NotAllowed, command.Descriptor.Name);
                    }
                }

                return(this.innerProfile.OnCommandInternal(command));
            }
 /// <summary>
 /// Initializes a connection with SASL profile, open and open callback.
 /// </summary>
 /// <param name="address">The address.</param>
 /// <param name="saslProfile">The SASL profile to do authentication (optional).</param>
 /// <param name="open">The open frame to send (optional).</param>
 /// <param name="onOpened">The callback to handle remote open frame (optional).</param>
 public Connection(Address address, SaslProfile saslProfile, Open open, OnOpened onOpened)
     : this(DefaultMaxSessions)
 {
     this.address  = address;
     this.onOpened = onOpened;
     this.Connect(saslProfile, open);
 }
Пример #3
0
        /// <summary>
        /// Initializes a connection with SASL profile, open and open callback.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="saslProfile">The SASL profile to do authentication (optional). If it is
        /// null and address has user info, SASL PLAIN profile is used.</param>
        /// <param name="open">The open frame to send (optional). If not null, all mandatory
        /// fields must be set. Ensure that other fields are set to desired values.</param>
        /// <param name="onOpened">The callback to handle remote open frame (optional).</param>
        /// <remarks>
        /// The connection initialization includes establishing the underlying transport,
        /// which typically has blocking network I/O. Depending on the current synchronization
        /// context, it may cause deadlock or UI freeze. Please use the ConnectionFactory.CreateAsync
        /// method instead.
        /// </remarks>
        public Connection(Address address, SaslProfile saslProfile, Open open, OnOpened onOpened)
            : this(DefaultMaxSessions, DefaultMaxFrameSize)
        {
            this.address  = address;
            this.onOpened = onOpened;
            if (open != null)
            {
                this.maxFrameSize = open.MaxFrameSize;
                this.channelMax   = open.ChannelMax;
            }
            else
            {
                open = new Open()
                {
                    ContainerId  = Guid.NewGuid().ToString(),
                    HostName     = this.address.Host,
                    MaxFrameSize = this.maxFrameSize,
                    ChannelMax   = this.channelMax
                };
            }

            if (open.IdleTimeOut > 0)
            {
                this.heartBeat = new HeartBeat(this, open.IdleTimeOut);
            }

            this.Connect(saslProfile, open);
        }
Пример #4
0
        internal static async Task <IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
                                                               IBufferManager bufferManager, IAsyncTransport transport)
        {
            // if transport is closed, pump reader should throw exception
            TransportWriter writer = new TransportWriter(transport, e => { });

            ProtocolHeader myHeader = saslProfile.Start(hostname, writer);

            AsyncPump pump = new AsyncPump(bufferManager, transport);

            await pump.PumpAsync(
                header =>
            {
                saslProfile.OnHeader(myHeader, header);
                return(true);
            },
                buffer =>
            {
                SaslCode code;
                return(saslProfile.OnFrame(writer, buffer, out code));
            });

            await writer.FlushAsync();

            return((IAsyncTransport)saslProfile.UpgradeTransportInternal(transport));
        }
Пример #5
0
        void Connect(SaslProfile saslProfile, Open open)
        {
            ITransport   transport;
            TcpTransport tcpTransport = new TcpTransport();

            tcpTransport.Connect(this, this.address, DisableServerCertValidation);
            transport = tcpTransport;

            if (saslProfile != null)
            {
                transport = saslProfile.Open(this.address.Host, transport);
            }
            else if (this.address.User != null)
            {
                transport = new SaslPlainProfile(this.address.User, this.address.Password).Open(this.address.Host, transport);
            }

            this.transport = transport;

            // after getting the transport, move state to open pipe before starting the pump
            this.SendHeader();
            this.SendOpen(open);
            this.state = State.OpenPipe;

            this.reader = new Pump(this);
            this.reader.Start();
        }
Пример #6
0
        internal static async Task <IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname,
                                                               IBufferManager bufferManager, IAsyncTransport transport, DescribedList command)
        {
            // if transport is closed, pump reader should throw exception
            TransportWriter writer = new TransportWriter(transport, e => { });

            ProtocolHeader myHeader = saslProfile.Start(writer, command);

            AsyncPump pump = new AsyncPump(bufferManager, transport);
            SaslCode  code = SaslCode.Auth;

            await pump.PumpAsync(
                SaslProfile.MaxFrameSize,
                header =>
            {
                saslProfile.OnHeader(myHeader, header);
                return(true);
            },
                buffer =>
            {
                return(saslProfile.OnFrame(hostname, writer, buffer, out code));
            }).ConfigureAwait(false);

            await writer.FlushAsync().ConfigureAwait(false);

            if (code != SaslCode.Ok)
            {
                throw new AmqpException(ErrorCode.UnauthorizedAccess,
                                        Fx.Format(SRAmqp.SaslNegoFailed, code));
            }

            return((IAsyncTransport)saslProfile.UpgradeTransportInternal(transport));
        }
Пример #7
0
        void Connect(SaslProfile saslProfile, Open open)
        {
            if (open != null)
            {
                this.maxFrameSize = open.MaxFrameSize;
                this.channelMax   = open.ChannelMax;
            }
            else
            {
                open = new Open()
                {
                    ContainerId  = MakeAmqpContainerId(),
                    HostName     = this.address.Host,
                    MaxFrameSize = this.maxFrameSize,
                    ChannelMax   = this.channelMax
                };
            }

            if (open.IdleTimeOut > 0)
            {
                this.heartBeat = new HeartBeat(this, open.IdleTimeOut * 2);
            }

            ITransport transport;

            {
                TcpTransport tcpTransport = new TcpTransport();
                tcpTransport.Connect(this, this.address, DisableServerCertValidation);
                transport = tcpTransport;
            }

            try
            {
                if (saslProfile != null)
                {
                    transport = saslProfile.Open(this.address.Host, transport);
                }
                else if (this.address.User != null)
                {
                    transport = new SaslPlainProfile(this.address.User, this.address.Password).Open(this.address.Host, transport);
                }
            }
            catch
            {
                transport.Close();
                throw;
            }

            this.writer = new Writer(transport);

            // after getting the transport, move state to open pipe before starting the pump
            this.SendHeader();
            this.SendOpen(open);
            this.state = ConnectionState.OpenPipe;

            this.reader = new Pump(this, transport);
            this.reader.Start();
        }
Пример #8
0
        internal static async Task <IAsyncTransport> OpenAsync(this SaslProfile saslProfile, string hostname, IAsyncTransport transport)
        {
            ProtocolHeader header = saslProfile.Start(hostname, transport);

            AsyncPump pump = new AsyncPump(transport);

            await pump.PumpAsync(
                h => { saslProfile.OnHeader(header, h); return(true); },
                b => { SaslCode code; return(saslProfile.OnFrame(transport, b, out code)); });

            return((IAsyncTransport)saslProfile.UpgradeTransportInternal(transport));
        }
Пример #9
0
        void Connect(SaslProfile saslProfile, Open open)
        {
            ITransport transport;

#if NETFX
            if (WebSocketTransport.MatchScheme(address.Scheme))
            {
                WebSocketTransport wsTransport = new WebSocketTransport();
                wsTransport.ConnectAsync(address, null).ConfigureAwait(false).GetAwaiter().GetResult();
                transport = wsTransport;
            }
            else
#endif
            {
                TcpTransport tcpTransport = new TcpTransport();
                tcpTransport.Connect(this, this.address, DisableServerCertValidation);
                transport = tcpTransport;
            }

            try
            {
                if (saslProfile != null)
                {
                    transport = saslProfile.Open(this.address.Host, transport);
                }
                else if (this.address.User != null)
                {
                    transport = new SaslPlainProfile(this.address.User, this.address.Password).Open(this.address.Host, transport);
                }
            }
            catch
            {
                transport.Close();
                throw;
            }

            this.writer = new Writer(transport);

            // after getting the transport, move state to open pipe before starting the pump
            this.SendHeader();
            this.SendOpen(open);
            this.state = State.OpenPipe;

            this.reader = new Pump(this, transport);
            this.reader.Start();
        }
Пример #10
0
        async Task <IAsyncTransport> CreateTransportAsync(Address address, SaslProfile saslProfile, IHandler handler)
        {
            IAsyncTransport   transport;
            TransportProvider provider;

            if (this.transportFactories != null && this.transportFactories.TryGetValue(address.Scheme, out provider))
            {
                transport = await provider.CreateAsync(address).ConfigureAwait(false);
            }
            else if (TcpTransport.MatchScheme(address.Scheme))
            {
                TcpTransport tcpTransport = new TcpTransport(this.BufferManager);
                await tcpTransport.ConnectAsync(address, this, handler).ConfigureAwait(false);

                transport = tcpTransport;
            }
#if NETFX
            else if (WebSocketTransport.MatchScheme(address.Scheme))
            {
                WebSocketTransport wsTransport = new WebSocketTransport();
                await wsTransport.ConnectAsync(address, null).ConfigureAwait(false);

                transport = wsTransport;
            }
#endif
            else
            {
                throw new NotSupportedException(address.Scheme);
            }

            if (saslProfile != null)
            {
                try
                {
                    transport = await saslProfile.OpenAsync(address.Host, this.BufferManager, transport, null).ConfigureAwait(false);
                }
                catch
                {
                    transport.Close();
                    throw;
                }
            }

            return(transport);
        }
Пример #11
0
        async Task <IAsyncTransport> CreateTransportAsync(Address address, SaslProfile saslProfile, IHandler handler)
        {
            IAsyncTransport transport;

#if !WINDOWS_PHONE
            if (WebSocketTransport.MatchScheme(address.Scheme))
            {
                WebSocketTransport wsTransport = new WebSocketTransport();
                await wsTransport.ConnectAsync(address).ConfigureAwait(false);

                transport = wsTransport;
            }
            else
#endif
            if (string.Equals(address.Scheme, Address.Amqp, StringComparison.OrdinalIgnoreCase) ||
                string.Equals(address.Scheme, Address.Amqps, StringComparison.OrdinalIgnoreCase))
            {
                TcpTransport tcpTransport = new TcpTransport();
                await tcpTransport.ConnectAsync(address, this).ConfigureAwait(false);

                transport = tcpTransport;
            }
            else
            {
                throw new NotSupportedException(address.Scheme);
            }

            if (saslProfile != null)
            {
                try
                {
                    transport = await saslProfile.OpenAsync(address.Host, null, transport, null).ConfigureAwait(false);
                }
                catch
                {
                    transport.Close();
                    throw;
                }
            }

            return(transport);
        }
Пример #12
0
        internal async Task ConnectAsync(Address address, SaslProfile saslProfile, Open open, Connection connection)
        {
            if (saslProfile == null)
            {
                if (address.User != null)
                {
                    saslProfile = new SaslPlainProfile(address.User, address.Password);
                }
                else if (this.saslSettings != null && this.saslSettings.Profile != null)
                {
                    saslProfile = this.saslSettings.Profile;
                }
            }

            IAsyncTransport transport = await this.CreateTransportAsync(address, saslProfile, connection.Handler).ConfigureAwait(false);

            connection.Init(this.BufferManager, this.AMQP, transport, open);

            AsyncPump pump = new AsyncPump(this.BufferManager, transport);

            pump.Start(connection);
        }
Пример #13
0
            protected override DescribedList OnCommand(DescribedList command)
            {
                if (this.innerProfile == null)
                {
                    if (command.Descriptor.Code == Codec.SaslInit.Code)
                    {
                        var           init = (SaslInit)command;
                        SaslMechanism saslMechanism;
                        if (!this.listener.saslSettings.TryGetMechanism(init.Mechanism, out saslMechanism))
                        {
                            throw new AmqpException(ErrorCode.NotImplemented, init.Mechanism);
                        }

                        this.innerProfile = saslMechanism.CreateProfile();
                    }
                    else
                    {
                        throw new AmqpException(ErrorCode.NotAllowed, command.Descriptor.Name);
                    }
                }

                return(this.innerProfile.OnCommandInternal(command));
            }
Пример #14
0
        async Task <Connection> CreateAsync(Address address, Open open, OnOpened onOpened, IHandler handler)
        {
            SaslProfile saslProfile = null;

            if (address.User != null)
            {
                saslProfile = new SaslPlainProfile(address.User, address.Password);
            }
            else if (this.saslSettings != null && this.saslSettings.Profile != null)
            {
                saslProfile = this.saslSettings.Profile;
            }

            IAsyncTransport transport = await this.CreateTransportAsync(address, saslProfile, handler).ConfigureAwait(false);

            Connection connection = new Connection(this.BufferManager, this.AMQP, address, transport, open, onOpened, handler);

            AsyncPump pump = new AsyncPump(this.BufferManager, transport);

            pump.Start(connection);

            return(connection);
        }
Пример #15
0
        void Connect(SaslProfile saslProfile, Open open)
        {
            if (open != null)
            {
                this.maxFrameSize = open.MaxFrameSize;
                this.channelMax   = open.ChannelMax;
            }
            else
            {
                open = new Open()
                {
                    ContainerId  = Guid.NewGuid().ToString(),
                    HostName     = this.address.Host,
                    MaxFrameSize = this.maxFrameSize,
                    ChannelMax   = this.channelMax
                };
            }

            if (open.IdleTimeOut > 0)
            {
                this.heartBeat = new HeartBeat(this, open.IdleTimeOut);
            }

            ITransport transport;

#if NETFX
            if (WebSocketTransport.MatchScheme(address.Scheme))
            {
                WebSocketTransport wsTransport = new WebSocketTransport();
                wsTransport.ConnectAsync(address, null).ConfigureAwait(false).GetAwaiter().GetResult();
                transport = wsTransport;
            }
            else
#endif
            {
                TcpTransport tcpTransport = new TcpTransport();
                tcpTransport.Connect(this, this.address, DisableServerCertValidation);
                transport = tcpTransport;
            }

            try
            {
                if (saslProfile != null)
                {
                    transport = saslProfile.Open(this.address.Host, transport);
                }
                else if (this.address.User != null)
                {
                    transport = new SaslPlainProfile(this.address.User, this.address.Password).Open(this.address.Host, transport);
                }
            }
            catch
            {
                transport.Close();
                throw;
            }

            this.writer = new Writer(transport);

            // after getting the transport, move state to open pipe before starting the pump
            this.SendHeader();
            this.SendOpen(open);
            this.state = ConnectionState.OpenPipe;

            this.reader = new Pump(this, transport);
            this.reader.Start();
        }
Пример #16
0
 /// <summary>
 /// Initializes a connection with SASL profile, open and open callback.
 /// </summary>
 /// <param name="address">The address.</param>
 /// <param name="saslProfile">The SASL profile to do authentication (optional). If it is
 /// null and address has user info, SASL PLAIN profile is used.</param>
 /// <param name="open">The open frame to send (optional). If not null, all mandatory
 /// fields must be set. Ensure that other fields are set to desired values.</param>
 /// <param name="onOpened">The callback to handle remote open frame (optional).</param>
 /// <remarks>
 /// The connection initialization includes establishing the underlying transport,
 /// which typically has blocking network I/O. Depending on the current synchronization
 /// context, it may cause deadlock or UI freeze. Please use the ConnectionFactory.CreateAsync
 /// method instead.
 /// </remarks>
 public Connection(Address address, SaslProfile saslProfile, Open open, OnOpened onOpened)
     : this(address, DefaultMaxSessions, DefaultMaxFrameSize)
 {
     this.onOpened = onOpened;
     this.Connect(saslProfile, open);
 }
Пример #17
0
 void Connect(SaslProfile saslProfile, Open open)
 {
     Factory.ConnectAsync(this.address, saslProfile, open, this).ConfigureAwait(false).GetAwaiter().GetResult();
 }