示例#1
0
        private static async Task UpgradeToWebSockets(RavenDBOptions options, IOwinContext context, Func <Task> next)
        {
            var accept = context.Get <Action <IDictionary <string, object>, Func <IDictionary <string, object>, Task> > >("websocket.Accept");

            if (accept == null)
            {
                // Not a websocket request
                await next();

                return;
            }

            WebSocketsTransport webSocketsTrasport = WebSocketTransportFactory.CreateWebSocketTransport(options, context);

            if (webSocketsTrasport != null)
            {
                if (await webSocketsTrasport.TrySetupRequest())
                {
                    accept(new Dictionary <string, object>()
                    {
                        { "websocket.ReceiveBufferSize", 256 },
                        { "websocket.Buffer", webSocketsTrasport.PreAllocatedBuffer },
                        { "websocket.KeepAliveInterval", WebSocket.DefaultKeepAliveInterval }
                    }, webSocketsTrasport.Run);
                }
            }
        }
        async Task SendReceiveAsync(int count)
        {
            // it is also possible to create the Address object form a Uri string as follows,
            //   wss://[sas-policy]:[sas-key]@[ns].servicebus.windows.net/$servicebus/websocket
            // note that [sas-policy] and [sas-key] should be URL encoded
            Address wsAddress = new Address(this.Namespace, 443, this.KeyName, this.KeyValue, "/$servicebus/websocket", "wss");
            WebSocketTransportFactory wsFactory = new WebSocketTransportFactory("AMQPWSB10");

            Trace.WriteLine(TraceLevel.Information, "Establishing a connection...");
            ConnectionFactory connectionFactory = new ConnectionFactory(new TransportProvider[] { wsFactory });
            Connection        connection        = await connectionFactory.CreateAsync(wsAddress);

            Trace.WriteLine(TraceLevel.Information, "Creating a session...");
            Session session = new Session(connection);

            Trace.WriteLine(TraceLevel.Information, "Creating a sender link...");
            SenderLink sender = new SenderLink(session, "websocket-sender-link", this.Entity);

            Trace.WriteLine(TraceLevel.Information, "Sending {0} messages...", count);
            for (int i = 0; i < count; i++)
            {
                Message message = new Message("testing");
                message.Properties = new Properties()
                {
                    MessageId = "websocket-test-" + i
                };
                await sender.SendAsync(message);
            }

            Trace.WriteLine(TraceLevel.Information, "Closing sender...");
            await sender.CloseAsync();

            Trace.WriteLine(TraceLevel.Information, "Receiving messages...");
            ReceiverLink receiver = new ReceiverLink(session, "websocket-receiver-link", this.Entity);

            for (int i = 0; i < count; i++)
            {
                Message message = await receiver.ReceiveAsync(30000);

                if (message == null)
                {
                    break;
                }

                receiver.Accept(message);
            }

            Trace.WriteLine(TraceLevel.Information, "Closing receiver...");
            await receiver.CloseAsync();

            Trace.WriteLine(TraceLevel.Information, "Shutting down...");
            await session.CloseAsync();

            await connection.CloseAsync();
        }
示例#3
0
        private static async Task UpgradeToWebSockets(RavenDBOptions options, IOwinContext context, Func <Task> next)
        {
            var accept = context.Get <Action <IDictionary <string, object>, Func <IDictionary <string, object>, Task> > >("websocket.Accept");

            if (accept == null)
            {
                // Not a websocket request
                await next();

                return;
            }

            WebSocketsTransport webSocketsTrasport = WebSocketTransportFactory.CreateWebSocketTransport(options, context);

            if (webSocketsTrasport != null)
            {
                if (await webSocketsTrasport.TrySetupRequest())
                {
                    accept(null, webSocketsTrasport.Run);
                }
            }
        }
示例#4
0
        public async Task WebSocketSslMutalAuthTest()
        {
            string testName      = "WebSocketSslMutalAuthTest";
            string listenAddress = "wss://localhost:18081/" + testName + "/";
            Uri    uri           = new Uri(listenAddress);

            X509Certificate2 cert = ContainerHostTests.GetCertificate(StoreLocation.LocalMachine, StoreName.My, "localhost");

            string output;
            int    code = Exec("netsh.exe", string.Format("http show sslcert hostnameport={0}:{1}", uri.Host, uri.Port), out output);

            if (code != 0)
            {
                string args = string.Format("http add sslcert hostnameport={0}:{1} certhash={2} certstorename=MY appid={{{3}}} clientcertnegotiation=enable",
                                            uri.Host, uri.Port, cert.Thumbprint, Guid.NewGuid());
                code = Exec("netsh.exe", args, out output);
                Assert.AreEqual(0, code, "failed to add ssl cert: " + output);
            }

            X509Certificate serviceCert  = null;
            X509Certificate clientCert   = null;
            ListenerLink    listenerLink = null;

            var linkProcessor = new TestLinkProcessor()
            {
                OnLinkAttached = c => listenerLink = c
            };
            var host = new ContainerHost(new List <Uri>()
            {
                uri
            }, null, uri.UserInfo);

            host.Listeners[0].SASL.EnableExternalMechanism            = true;
            host.Listeners[0].SSL.ClientCertificateRequired           = true;
            host.Listeners[0].SSL.CheckCertificateRevocation          = true;
            host.Listeners[0].SSL.RemoteCertificateValidationCallback = (a, b, c, d) => { clientCert = b; return(true); };
            host.RegisterLinkProcessor(linkProcessor);
            host.Open();

            try
            {
                ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { serviceCert = b; return(true); };
                var wssFactory = new WebSocketTransportFactory();
                wssFactory.Options = o =>
                {
                    o.ClientCertificates.Add(ContainerHostTests.GetCertificate(StoreLocation.LocalMachine, StoreName.My, uri.Host));
                };

                ConnectionFactory connectionFactory = new ConnectionFactory(new TransportProvider[] { wssFactory });
                connectionFactory.SASL.Profile = SaslProfile.External;
                Connection connection = await connectionFactory.CreateAsync(new Address(listenAddress));

                Session    session = new Session(connection);
                SenderLink sender  = new SenderLink(session, "sender-" + testName, "q1");
                await sender.SendAsync(new Message("test") { Properties = new Properties()
                                                             {
                                                                 MessageId = testName
                                                             } });

                await connection.CloseAsync();

                Assert.IsTrue(serviceCert != null, "service cert not received");
                Assert.IsTrue(clientCert != null, "client cert not received");
                Assert.IsTrue(listenerLink != null, "link not attached");

                IPrincipal principal = ((ListenerConnection)listenerLink.Session.Connection).Principal;
                Assert.IsTrue(principal != null, "connection pricipal is null");
                Assert.IsTrue(principal.Identity is X509Identity, "identify should be established by client cert");
            }
            finally
            {
                host.Close();
            }
        }
示例#5
0
        async Task SendReceiveAsync(int count)
        {
            // it is also possible to create the Address object form a Uri string as follows,
            //   wss://[sas-policy]:[sas-key]@[ns].servicebus.windows.net/$servicebus/websocket
            // note that [sas-policy] and [sas-key] should be URL encoded
            Address wsAddress = new Address(this.Namespace, 443, this.KeyName, this.KeyValue, "/$servicebus/websocket", "wss");
            WebSocketTransportFactory wsFactory = new WebSocketTransportFactory("AMQPWSB10");

            Trace.WriteLine(TraceLevel.Information, "Establishing a connection...");
            ConnectionFactory connectionFactory = new ConnectionFactory(new TransportProvider[] { wsFactory });
            Connection        connection        = await connectionFactory.CreateAsync(wsAddress);

            Trace.WriteLine(TraceLevel.Information, "Creating a session...");
            Session session = new Session(connection);

            Trace.WriteLine(TraceLevel.Information, "Creating a sender link...");
            SenderLink sender = new SenderLink(session, "websocket-sender-link", this.Entity);

            Trace.WriteLine(TraceLevel.Information, "Sending {0} messages...", count);
            for (int i = 0; i < count; i++)
            {
                Message message = new Message($"testing-{i}");
                message.Properties = new Properties()
                {
                    MessageId = "websocket-test-" + i
                };
                message.MessageAnnotations = new MessageAnnotations();
                //       message.MessageAnnotations.Map.Add("x-opt-scheduled-enqueue-time", DateTime.UtcNow.AddMinutes(1));
                await sender.SendAsync(message);
            }

            Trace.WriteLine(TraceLevel.Information, "Closing sender...");
            await sender.CloseAsync();

            Trace.WriteLine(TraceLevel.Information, "Receiving messages...");
            ReceiverLink receiver = new ReceiverLink(session, "websocket-receiver-link", this.Entity);

            for (int i = 0; i < count; i++)
            {
                Message message = await receiver.ReceiveAsync();

                if (message == null)
                {
                    Console.WriteLine("No more messages received");
                    break;
                }
                var msgpayLoad = message.GetBody <string>();
                Console.WriteLine($"RECEIVED {msgpayLoad}");
                receiver.Accept(message);
            }

            Trace.WriteLine(TraceLevel.Information, "Closing receiver...");
            await receiver.CloseAsync();

            Trace.WriteLine(TraceLevel.Information, "Shutting down...");
            await session.CloseAsync();

            Console.WriteLine("Done");
            Console.ReadLine();
            await connection.CloseAsync();
        }