private void txtCertificate_Click(object sender, EventArgs e)
        {
            String value = txtWill.Text;

            if (InputBox("Editing Certificate", ref value) == DialogResult.OK)
            {
                try
                {
                    if (value.Length > 0)
                    {
                        CertificatesHelper.load(value, txtSecurityPassword.Text);
                    }

                    txtCertificate.Text = value;
                }
                catch (Exception)
                {
                    MessageBox.Show("Can not load certificate , please verify that its in pem encoded format. In case password is required please provide it first");
                    return;
                }
            }
        }
        public Boolean Init(ConnectionListener <MQMessage> listener)
        {
            if (channel == null)
            {
                bootstrap = new Bootstrap();
                loopGroup = new MultithreadEventLoopGroup(workerThreads);
                bootstrap.Group(loopGroup);
                bootstrap.Channel <TcpSocketChannel>();
                bootstrap.Option(ChannelOption.TcpNodelay, true);
                bootstrap.Option(ChannelOption.SoKeepalive, true);

                X509Certificate2 cert = null;
                if (certificate != null && certificate.Length > 0)
                {
                    cert = CertificatesHelper.load(certificate, certificatePassword);
                }

                WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.NewHandshaker(this.getUri(), WebSocketVersion.V13, null, false, EmptyHttpHeaders.Default, 1280000);
                WebSocketClientHandler    handler    = new WebSocketClientHandler(this, handshaker, listener);

                bootstrap.Handler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    if (isSecured)
                    {
                        if (cert != null)
                        {
                            pipeline.AddLast("ssl", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true, (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => cert), new ClientTlsSettings(address.Host, new List <X509Certificate>()
                            {
                                cert
                            })));
                        }
                        else
                        {
                            pipeline.AddLast("ssl", TlsHandler.Client(address.Host));
                        }
                    }

                    pipeline.AddLast("http - codec", new HttpClientCodec());
                    pipeline.AddLast("aggregator", new HttpObjectAggregator(65536));
                    pipeline.AddLast("handler", handler);
                    pipeline.AddLast(new ExceptionHandler());
                }));

                bootstrap.RemoteAddress(address);

                try
                {
                    Task <IChannel> task = bootstrap.ConnectAsync();
                    task.GetAwaiter().OnCompleted(() =>
                    {
                        try
                        {
                            channel = task.Result;
                        }
                        catch (Exception)
                        {
                            listener.ConnectFailed();
                            return;
                        }

                        if (channel != null)
                        {
                            listener.Connected();
                        }
                        else
                        {
                            listener.ConnectFailed();
                        }
                    });
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #3
0
        public Boolean Init(ConnectionListener <AMQPHeader> listener)
        {
            if (channel == null)
            {
                bootstrap = new Bootstrap();
                loopGroup = new MultithreadEventLoopGroup(workerThreads);
                bootstrap.Group(loopGroup);
                bootstrap.Channel <TcpSocketChannel>();
                bootstrap.Option(ChannelOption.TcpNodelay, true);
                bootstrap.Option(ChannelOption.SoKeepalive, true);

                X509Certificate2 cert = null;
                if (certificate != null && certificate.Length > 0)
                {
                    cert = CertificatesHelper.load(certificate, certificatePassword);
                }

                bootstrap.Handler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    if (isSecured)
                    {
                        if (cert != null)
                        {
                            pipeline.AddLast("ssl", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true, (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => cert), new ClientTlsSettings(address.Host, new List <X509Certificate>()
                            {
                                cert
                            })));
                        }
                        else
                        {
                            pipeline.AddLast("ssl", TlsHandler.Client(address.Host));
                        }
                    }

                    pipeline.AddLast(new AMQPDecoder());
                    pipeline.AddLast("handler", new AMQPHandler(listener));
                    pipeline.AddLast(new AMQPEncoder());
                    pipeline.AddLast(new ExceptionHandler());
                }));

                bootstrap.RemoteAddress(address);

                try
                {
                    Task <IChannel> task = bootstrap.ConnectAsync();
                    task.GetAwaiter().OnCompleted(() =>
                    {
                        try
                        {
                            channel = task.Result;
                        }
                        catch (Exception)
                        {
                            listener.ConnectFailed();
                            return;
                        }

                        if (channel != null)
                        {
                            listener.Connected();
                        }
                        else
                        {
                            listener.ConnectFailed();
                        }
                    });
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }