Пример #1
0
        public void ShouldBeVerifiable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                PrivateKey wrongKey = key.CKD(key.Bytes);
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());

                Assert.True(received.Verify(key.Address));
                Assert.False(received.Verify(wrongKey.Address));

                // verify failed if body has changed
                received.Body.Set("rndBytes", RndProvider.GetNonZeroBytes(128));
                Assert.False(received.Verify(key.Address));
            }
        }
Пример #2
0
        public void ShouldBeSerializable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());

                Assert.Equal(message.Hash, received.Hash);
                Assert.Equal(rndBytes, message.Value <byte[]>("rndBytes"));
                Assert.Equal(rndBytes, received.Value <byte[]>("rndBytes"));
                Assert.Equal(message.Value <byte[]>("rndBytes"), received.Value <byte[]>("rndBytes"));
                Assert.Equal(message.Headers.Hash, received.Headers.Hash);
                Assert.Equal(message.Body.Hash, received.Body.Hash);
            }
        }
Пример #3
0
        public void ShouldBeModifiable(int repeats)
        {
            for (int i = 0; i < repeats; i++)
            {
                PrivateKey key      = PrivateKey.CreateKey();
                byte[]     rndBytes = RndProvider.GetNonZeroBytes(1024);

                Payload body = new Payload.Builder()
                               .Value("rndBytes", rndBytes)
                               .Build();

                Message message = new Message.Builder()
                                  .WithBody(body)
                                  .Build(key);

                // serialize & deserialize
                Message received = Message.Parse(message.ToBytes());
                Assert.True(received.Verify(key.Address));

                // verify ok if header changed
                received.Headers.Set("timestamp", DateTime.Now);
                Assert.True(received.Verify(key.Address));

                // verify failed if body changed
                received.Body.Set("id", RndProvider.Next(100));
                Assert.False(received.Verify(key.Address));
            }
        }
        private void OnTcpMessagePeek(ITcpSession session, Message message)
        {
            ElasticAddress address = message.Value <string>("enode");

            // verify message
            PrivateKey nodeKey = MasterKey.CKD($"{address.Host}:{address.Port}");

            if (!message.Verify(nodeKey.Address))
            {
                Logger.warning("Unverified message");
                return;
            }

            // valid address?
            if (nodeKey.Address != address.Address)
            {
                Logger.warning("Invalid address");
                return;
            }

            Message msg = new Message.Builder()
                          .Body(ACTION, ACTION_PEEK)
                          .Body("peers", Peers.Count)
                          .Build(MasterKey);

            session?.Write(msg.ToBytes());
        }
Пример #5
0
        public void RequestSync()
        {
            Message message = new Message.Builder()
                              .Body(ACTION, ACTION_SYNC)
                              .Build(MasterKey);

            SendMessage(message.ToBytes());
        }
        public void SyncAllPeers(string[] peers)
        {
            Message message = new Message.Builder()
                              .Body(ACTION, ACTION_UPDATE)
                              .Body("peers", peers)
                              .Build(MasterKey);

            Server.SendAll(message.ToBytes());
        }
        public void SyncPeers(ITcpSession session, string[] catalogs)
        {
            Message message = new Message.Builder()
                              .Body(ACTION, ACTION_UPDATE)
                              .Body("peers", catalogs)
                              .Build(MasterKey);

            session?.Write(message.ToBytes());
        }
Пример #8
0
        private void Register(ITcpSession session)
        {
            Message message = new Message.Builder()
                              .Body(ACTION, ACTION_REGISTER)
                              .Body("enode", NodeUrl.ToString())
                              .Build(NodeKey);

            session?.Write(message.ToBytes());
        }
        public void DateTimeTest()
        {
            PrivateKey key = PrivateKey.CreateKey();

            Message ping = new Message.Builder()
                           .Body("rndBytes", RndProvider.GetNonZeroBytes(32))
                           .Build(key);

            Message received = Message.Parse(ping.ToBytes());

            Assert.Equal(ping.TimeStamp, received.TimeStamp);
        }
Пример #10
0
        static void Main(string[] args)
        {
            ILoggable logger = new BLog.Builder()
                               .WithConsole(true)
                               .WithFilter(LogFilter.All)
                               .Build();

            PrivateKey key = PrivateKey.CreateKey();

            using (CancellationTokenSource cts = new CancellationTokenSource())
            {
                NodeServer server = new NodeServer(logger);
                server.Start(PORT, cts);

                Console.CancelKeyPress += (sender, e) =>
                {
                    e.Cancel = true;
                    cts.Cancel();
                };

                byte[] rndBytes = RndProvider.GetNonZeroBytes(1024 * 1024);

                while (!cts.IsCancellationRequested)
                {
                    Thread.Sleep(10);

                    rndBytes = RndProvider.GetNonZeroBytes(1024 * 1024);
                    Message message = new Message.Builder()
                                      .Body("rndBytes", rndBytes)
                                      .Build(key);

                    byte[] data = message.ToBytes();
                    //                    ElasticNode.SendTo(data, "127.0.0.1", PORT);
                    NodeClient client = new NodeClient(logger);
                    client.Start("127.0.0.1", PORT);

                    Thread.Sleep(1000);

//                    client.Write(data);
                    client.Stop();
                }
            }
        }