public NemesisController()
        {
            var env = ServiceProvider.GetService<EnvironmentConfig>();

            _log.Debug("Loading nemesis config file \"{0}\"...", env.NemesisConfigFile);
            config = NemesisConfig.LoadFromFile(env.NemesisConfigFile);

            _log.Debug("Host: {0}, Send port: {1}, Receive port: {4}, Encryption: {2}, GUID: {3}",
                config.Hostname, config.SendPort, config.UseEncryption, config.Id, config.ReceivePort);

            node = new NemesisNode(config.Id, new int[] { config.ReceivePort, config.SendPort }, config.Hostname, false);
            if (config.UseEncryption && string.Empty != env.NemesisKeyFile) {
                _log.Debug("Loading encryption keys from \"{0}\"...", env.NemesisKeyFile);
                keystore = new XMLFileKeyStore(env.NemesisKeyFile);

                var coreKeystore = new XMLFileKeyStore(env.NemesisKeyFile.Replace("agent", "core"));
                node.HubPublicKey = coreKeystore.PublicKey;

                node.EnableEncryption(keystore);

                _log.Debug("Encryption is enabled.");
            }

            node.Connect();

            //_log.Debug("Starting hearbeat worker thread...");
            //heartbeatworker = new NemesisHeartbeatWorker(config, node);
            //heartbeatworker.Start();
        }
        private static void threadProcNodeC(object testData_)
        {
            var testData = (TestData)testData_;
            var _log     = NLog.LogManager.GetLogger("NodeCThread");

            _log.Info("Creating node C...");

            var nodeC = new Nemesis.NemesisNode(testData.NodeCId, testData.Ports, "localhost", false);

            nodeC.SetLogName("nNodeC");
            nodeC.PermitPublicKeyUpload = true;

            nodeC.CommandReceived += NodeC_CommandReceived;

            nodeC.Connect();


            var cmdTest = "n2h";

            _log.Info($"Sending command \"{cmdTest}\" (1/2) from Node C to Hub");

            var response = nodeC.SendCommand(cmdTest);

            _log.Info("Got response: {0}", response.Result);



            _log.Info($"Sending command \"{cmdTest}\" (2/2) from Node C to Hub");
            response = nodeC.SendCommand(cmdTest);
            _log.Info("Got response: {0}", response.Result);
        }
示例#3
0
        private static void threadProcNodeC(object testData_)
        {
            var testData = (TestData)testData_;
            var _log = NLog.LogManager.GetLogger("NodeCThread");

            _log.Info("Creating node C...");

            var nodeC = new Nemesis.NemesisNode(testData.NodeCId, testData.Ports, "localhost", false);

            nodeC.SetLogName("nNodeC");
            nodeC.PermitPublicKeyUpload = true;

            nodeC.CommandReceived += NodeC_CommandReceived;

            nodeC.Connect();


            var cmdTest = "n2h";
            _log.Info($"Sending command \"{cmdTest}\" (1/2) from Node C to Hub");

            var response = nodeC.SendCommand(cmdTest);
            _log.Info("Got response: {0}", response.Result);



            _log.Info($"Sending command \"{cmdTest}\" (2/2) from Node C to Hub");
            response = nodeC.SendCommand(cmdTest);
            _log.Info("Got response: {0}", response.Result);



        }
示例#4
0
        private static void threadProcNodeB(object testData_)
        {
            var testData = (TestData)testData_;
            var _log = NLog.LogManager.GetLogger("NodeBThread");

            _log.Info("Creating node B...");

            var ke = new RSA();

            var badKeyStore = new MemoryKeyStore(ke);
            badKeyStore.Load(testData.NodeAKeys); // Note: The WRONG keystore

            var nodeBKeyStore = new MemoryKeyStore(ke);
            nodeBKeyStore.Load(testData.NodeAKeys);

            var nodeB = new Nemesis.NemesisNode(testData.NodeBId, testData.Ports, testData.Host.ToString(), false);
            nodeB.SetLogName("nNodeB");

            nodeB.CommandReceived += NodeB_CommandReceived;

            //var hubKeyStore = new MemoryKeyStore();
            //hubKeyStore.Load(testData.HubKeys);
            //nodeB.HubPublicKey = hubKeyStore.PublicKey;
            nodeB.HubPublicKey = badKeyStore.PublicKey; // Note: Again; the wrong key

            nodeB.EnableEncryption(badKeyStore);

            nodeB.Connect();

            var cmdTest = "n2h";
            _log.Info($"Sending command \"{cmdTest}\" with WRONG ENCODING from Node B to Hub");

            try {
                var response = nodeB.SendCommand(cmdTest);
                var r = response.Result; // Note: Will fail
            }
            catch (Exception x)
            {
                _log.Warn("Oh no! " + x.Message);
            }

            nodeB.EnableEncryption(nodeBKeyStore);

            _log.Info($"Sending command \"{cmdTest}\" with the RIGHT ENCODING from Node B to Hub");

            try {
                var response = nodeB.SendCommand(cmdTest);
                _log.Info("Got result: " + response.Result); // Note: Will fail
            }
            catch (Exception x)
            {
                _log.Warn("Oh no! " + x.Message);
            }

            //var hubKeyStore = new MemoryKeyStore();
            //hubKeyStore.Load(testData.HubKeys);
            //nodeB.HubPublicKey = hubKeyStore.PublicKey;

        }
示例#5
0
        private static void threadProcNodeA(object testData_)
        {
            var testData = (TestData)testData_;
            var _log     = NLog.LogManager.GetLogger("NodeAThread");

            _log.Info("Creating node A...");

            var keyStore = new MemoryKeyStore(RSA.Default);

            keyStore.Load(testData.NodeAKeys);


            var nodeA = new Nemesis.NemesisNode(testData.NodeAId, testData.Ports, testData.Host.ToString(), false);

            nodeA.SetLogName("nNodeA");

            nodeA.CommandReceived += NodeA_CommandReceived;

            var hubKeyStore = new MemoryKeyStore(RSA.Default);

            hubKeyStore.Load(testData.HubKeys);
            nodeA.HubPublicKey = hubKeyStore.PublicKey;

            nodeA.EnableEncryption(keyStore);

            nodeA.Connect();


            var cmdTest = "n2h";

            _log.Info($"Sending command \"{cmdTest}\" (1/2) from Node A to Hub");

            var response = nodeA.SendCommand(cmdTest);

            _log.Info("Got response: {0}", response.Result);



            _log.Info($"Sending command \"{cmdTest}\" (2/2) from Node A to Hub");
            response = nodeA.SendCommand(cmdTest);
            _log.Info("Got response: {0}", response.Result);
        }
示例#6
0
        private static void threadProcNodeA(object testData_)
        {
            var testData = (TestData)testData_;
            var _log = NLog.LogManager.GetLogger("NodeAThread");

            _log.Info("Creating node A...");

            var keyStore = new MemoryKeyStore(RSA.Default);
            keyStore.Load(testData.NodeAKeys);


            var nodeA = new Nemesis.NemesisNode(testData.NodeAId, testData.Ports, testData.Host.ToString(), false);

            nodeA.SetLogName("nNodeA");

            nodeA.CommandReceived += NodeA_CommandReceived;
            
            var hubKeyStore = new MemoryKeyStore(RSA.Default);
            hubKeyStore.Load(testData.HubKeys);
            nodeA.HubPublicKey = hubKeyStore.PublicKey;

            nodeA.EnableEncryption(keyStore);

            nodeA.Connect();

   
            var cmdTest = "n2h";
            _log.Info($"Sending command \"{cmdTest}\" (1/2) from Node A to Hub");

            var response = nodeA.SendCommand(cmdTest);
            _log.Info("Got response: {0}", response.Result);
            


            _log.Info($"Sending command \"{cmdTest}\" (2/2) from Node A to Hub");
            response = nodeA.SendCommand(cmdTest);
            _log.Info("Got response: {0}", response.Result);



        }
        private static void threadProcNodeB(object testData_)
        {
            var testData = (TestData)testData_;
            var _log     = NLog.LogManager.GetLogger("NodeBThread");

            _log.Info("Creating node B...");

            var ke = new RSA();

            var badKeyStore = new MemoryKeyStore(ke);

            badKeyStore.Load(testData.NodeAKeys); // Note: The WRONG keystore

            var nodeBKeyStore = new MemoryKeyStore(ke);

            nodeBKeyStore.Load(testData.NodeAKeys);

            var nodeB = new Nemesis.NemesisNode(testData.NodeBId, testData.Ports, testData.Host.ToString(), false);

            nodeB.SetLogName("nNodeB");

            nodeB.CommandReceived += NodeB_CommandReceived;

            //var hubKeyStore = new MemoryKeyStore();
            //hubKeyStore.Load(testData.HubKeys);
            //nodeB.HubPublicKey = hubKeyStore.PublicKey;
            nodeB.HubPublicKey = badKeyStore.PublicKey; // Note: Again; the wrong key

            nodeB.EnableEncryption(badKeyStore);

            nodeB.Connect();

            var cmdTest = "n2h";

            _log.Info($"Sending command \"{cmdTest}\" with WRONG ENCODING from Node B to Hub");

            try {
                var response = nodeB.SendCommand(cmdTest);
                var r        = response.Result; // Note: Will fail
            }
            catch (Exception x)
            {
                _log.Warn("Oh no! " + x.Message);
            }

            nodeB.EnableEncryption(nodeBKeyStore);

            _log.Info($"Sending command \"{cmdTest}\" with the RIGHT ENCODING from Node B to Hub");

            try {
                var response = nodeB.SendCommand(cmdTest);
                _log.Info("Got result: " + response.Result); // Note: Will fail
            }
            catch (Exception x)
            {
                _log.Warn("Oh no! " + x.Message);
            }

            //var hubKeyStore = new MemoryKeyStore();
            //hubKeyStore.Load(testData.HubKeys);
            //nodeB.HubPublicKey = hubKeyStore.PublicKey;
        }
        public void SimpleNodeAndHub()
        {
            var answerTimeout = TimeSpan.FromSeconds(10);

            var NodeMessageReceived = new TaskCompletionSource<string>();

            var testData = Shared.GetTestSettings();

            var nodeAkeyStore = new MemoryKeyStore();
            nodeAkeyStore.Load(testData.NodeAKeys);

            var hubKeystore = new MemoryKeyStore();
            hubKeystore.Load(testData.HubKeys);

            var hubThread = new Thread(() =>
            {

                var hub = new NemesisHub(
                    new IPEndPoint(testData.Host, testData.Ports[0]),
                    new IPEndPoint(testData.Host, testData.Ports[1])
                );
                hub.EnableEncryption(hubKeystore);


                hub.NodesPublicKeys.AddOrUpdate(testData.NodeAId, nodeAkeyStore.PublicKey,
                    (a, b) => { return nodeAkeyStore.PublicKey; });

                hub.CommandReceived += (sender, e) =>
                {
                    Trace.WriteLine("Hub command received: " + e.Command);

                    e.ResultSource.SetResult("ok");
                };

                var nodeResponse = hub.SendCommand("h2n", testData.NodeAId);

                NodeMessageReceived.SetResult(nodeResponse.Result);


            });

            hubThread.Start();

            var nodeA = new NemesisNode(testData.NodeAId, testData.Ports, testData.Host.ToString(), false);
            nodeA.HubPublicKey = hubKeystore.PublicKey;
            nodeA.EnableEncryption(nodeAkeyStore);

            nodeA.CommandReceived += (sender, e) =>
            {
                Trace.WriteLine("nodeA command received: " + e.Command);

                e.ResultSource.SetResult("ok");
            };

            nodeA.Connect();

            var hubResponse = nodeA.SendCommand("n2h");

            hubResponse.Wait(answerTimeout);
            var hubResult = hubResponse.IsCompleted ? hubResponse.Result : "timeout";
            Assert.AreEqual("ok", hubResult, "Hub -> Node command error");

            NodeMessageReceived.Task.Wait(answerTimeout);
            var nodeResult = NodeMessageReceived.Task.IsCompleted ? NodeMessageReceived.Task.Result : "timeout";
            Assert.AreEqual("ok", nodeResult, "Node -> Hub command error");

        }