Esempio n. 1
0
        public async Task NotAuthenticatedIntegrationTest()
        {
            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground
                    (new WampCraUserDbAuthenticationFactory
                        (new MyAuthenticationProvider(),
                        new MyUserDb()));

            SetupHost(playground);

            IWampClientAuthenticator authenticator =
                new WampCraClientAuthenticator(authenticationId: "peter", secret: "SECRET");

            IWampChannel channel =
                playground.CreateNewChannel("realm1", authenticator);

            IWampRealmProxy realmProxy = channel.RealmProxy;

            WampConnectionBrokenException openException = null;

            try
            {
                await channel.Open().ConfigureAwait(false);
            }
            catch (WampConnectionBrokenException ex)
            {
                openException = ex;
            }

            Assert.That(openException, Is.Not.Null);
            Assert.That(openException.CloseType, Is.EqualTo(SessionCloseType.Abort));
            Assert.That(openException.Reason, Is.EqualTo(WampErrors.NotAuthorized));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var apiKey = "PUT_YOUR_API_KEY_HERE";

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WampChannelFactory factory =
                new WampChannelFactory();

            const string serverAddress = "wss://live.prohashing.com:443/ws";

            IWampClientAuthenticator myAuthenticator = new WampCraClientAuthenticator(authenticationId: "web", secret: "web");

            IWampChannel channel =
                factory.ConnectToRealm("mining")
                .WebSocketTransport(serverAddress)
                .SetSecurityOptions(o =>
            {
                o.EnabledSslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
                //o.Certificates.Add(new X509Certificate2(@"server_cert.pem"));
                o.AllowNameMismatchCertificate = true;
                o.AllowUnstrustedCertificate   = true;
            })
                .JsonSerialization()
                .Authenticator(myAuthenticator)
                .Build();

            ServicePointManager.ServerCertificateValidationCallback = (s, crt, chain, policy) => true;

            channel.Open().Wait(5000);

            Console.WriteLine("Connection Complete ");

            //IWampRealmProxy realmProxy = channel.RealmProxy;   //use this for a subscription in conjunction with commented out below
            IArgumentsService proxy = channel.RealmProxy.Services.GetCalleeProxy <IArgumentsService>();

            Console.WriteLine("Channel Proxy: " + channel.RealmProxy.Name);

            JObject myResults = proxy.f_all_balance_updates(apiKey);
            var     path      = Directory.GetCurrentDirectory();

            path += "\\UserBalance.json";
            File.WriteAllText(path, myResults.ToString());


            myResults = proxy.f_all_profitability_updates();
            path      = Directory.GetCurrentDirectory();
            path     += "\\Profitability.json";
            File.WriteAllText(path, myResults.ToString());


            var myResults2 = proxy.f_all_miner_updates(apiKey);


            path  = Directory.GetCurrentDirectory();
            path += "\\SessionDetails.json";
            File.WriteAllText(path, myResults2.ToString());

            //----------------------------------------
            //int received = 0;
            //IDisposable subscription = null;



            //subscription =
            //    realmProxy.Services.GetSubject("balance_updates_"+apiKey)
            //        .Subscribe(x =>
            //        {
            //            Console.WriteLine("Got Event: " + x);

            //            received++;

            //            if (received > 5)
            //            {
            //                Console.WriteLine("Closing ..");
            //                subscription.Dispose();
            //            }
            //        });

            //Console.ReadLine();
            ////----------------------------------------
        }
Esempio n. 3
0
        public static void ClientCode()
#endif
        {
            DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

            IWampClientAuthenticator authenticator;

            if (false)
            {
                authenticator = new WampCraClientAuthenticator(authenticationId: "joe", secret: "secret2");
            }
            else
            {
                authenticator =
                    new WampCraClientAuthenticator(authenticationId: "peter", secret: "secret1");
            }

            IWampChannel channel =
                channelFactory.ConnectToRealm("realm1")
                .WebSocketTransport("ws://127.0.0.1:8080/ws")
                .JsonSerialization()
                .CraAuthentication("peter", "secret1")
                .Build();

            channel.RealmProxy.Monitor.ConnectionEstablished +=
                (sender, args) =>
            {
                Console.WriteLine("connected session with ID " + args.SessionId);

                dynamic details = args.WelcomeDetails.OriginalValue.Deserialize <dynamic>();

                Console.WriteLine("authenticated using method '{0}' and provider '{1}'", details.authmethod,
                                  details.authprovider);

                Console.WriteLine("authenticated with authid '{0}' and authrole '{1}'", details.authid,
                                  details.authrole);
            };

            channel.RealmProxy.Monitor.ConnectionBroken += (sender, args) =>
            {
                dynamic details = args.Details.OriginalValue.Deserialize <dynamic>();
                Console.WriteLine("disconnected " + args.Reason + " " + details.reason + details);
            };

            IWampRealmProxy realmProxy = channel.RealmProxy;

#if NET45
            await channel.Open().ConfigureAwait(false);
#else
            channel.Open().Wait();
#endif
            // call a procedure we are allowed to call (so this should succeed)
            //
            IAdd2Service proxy = realmProxy.Services.GetCalleeProxy <IAdd2Service>();

            try
            {
#if NET45
                var five = await proxy.Add2Async(2, 3)
                           .ConfigureAwait(false);
#else
                var five = proxy.Add2Async(2, 3).Result;
#endif
                Console.WriteLine("call result {0}", five);
            }
            catch (Exception e)
            {
                Console.WriteLine("call error {0}", e);
            }

            // (try to) register a procedure where we are not allowed to (so this should fail)
            //
            Mul2Service service = new Mul2Service();

            try
            {
#if NET45
                await realmProxy.Services.RegisterCallee(service)
                .ConfigureAwait(false);
#else
                realmProxy.Services.RegisterCallee(service)
                .Wait();
#endif

                Console.WriteLine("huh, function registered!");
            }
#if NET45
            catch (WampException ex)
            {
                Console.WriteLine("registration failed - this is expected: " + ex.ErrorUri);
            }
#else
            catch (AggregateException ex)
            {
                WampException innerException = ex.InnerException as WampException;
                Console.WriteLine("registration failed - this is expected: " + innerException.ErrorUri);
            }
#endif

            // (try to) publish to some topics
            //
            string[] topics =
            {
                "com.example.topic1",
                "com.example.topic2",
                "com.foobar.topic1",
                "com.foobar.topic2"
            };


            foreach (string topic in topics)
            {
                IWampTopicProxy topicProxy = realmProxy.TopicContainer.GetTopicByUri(topic);

                try
                {
#if NET45
                    await topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                             new object[] { "hello" })
                    .ConfigureAwait(false);
#else
                    topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                       new object[] { "hello" })
                    .Wait();
#endif
                    Console.WriteLine("event published to topic " + topic);
                }
#if NET45
                catch (WampException ex)
                {
                    Console.WriteLine("publication to topic " + topic + " failed: " + ex.ErrorUri);
                }
#else
                catch (AggregateException ex)
                {
                    WampException innerException = ex.InnerException as WampException;
                    Console.WriteLine("publication to topic " + topic + " failed: " + innerException.ErrorUri);
                }
#endif
            }
        }
Esempio n. 4
0
        public async Task AuthenticatedIntegrationTest()
        {
            WampAuthenticationPlayground playground =
                new WampAuthenticationPlayground
                    (new WampCraUserDbAuthenticationFactory
                        (new MyAuthenticationProvider(),
                        new MyUserDb()));

            SetupHost(playground);

            IWampClientAuthenticator authenticator =
                new WampCraClientAuthenticator(authenticationId: "peter", secret: "secret1");

            IWampChannel channel =
                playground.CreateNewChannel("realm1", authenticator);

            IWampRealmProxy realmProxy = channel.RealmProxy;

            await channel.Open().ConfigureAwait(false);

            // call a procedure we are allowed to call (so this should succeed)
            //
            IAdd2AsyncService proxy = realmProxy.Services.GetCalleeProxy <IAdd2AsyncService>();

            int five = await proxy.Add2(2, 3).ConfigureAwait(false);

            Assert.That(five, Is.EqualTo(5));

            // (try to) register a procedure where we are not allowed to (so this should fail)
            //
            Mul2Service service = new Mul2Service();

            WampException registerException = null;

            try
            {
                await realmProxy.Services.RegisterCallee(service)
                .ConfigureAwait(false);
            }
            catch (WampException ex)
            {
                registerException = ex;
            }

            Assert.That(registerException, Is.Not.Null);

            // (try to) publish to some topics
            //
            string[] topics =
            {
                "com.example.topic1",
                "com.example.topic2",
                "com.foobar.topic1",
                "com.foobar.topic2"
            };

            List <string> successfulTopics = new List <string>();

            foreach (string topic in topics)
            {
                IWampTopicProxy topicProxy = realmProxy.TopicContainer.GetTopicByUri(topic);

                try
                {
                    await topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                             new object[] { "hello" })
                    .ConfigureAwait(false);

                    successfulTopics.Add(topic);
                }
                catch (WampException ex)
                {
                }
            }

            Assert.That(successfulTopics, Is.EquivalentTo(new string[]
            {
                "com.foobar.topic1",
                "com.example.topic1"
            }));
        }