Esempio n. 1
0
        /// <summary>
        /// Runs the agent
        /// </summary>
        public void Run()
        {
            try
            {
                // Check we have an address to connect to.
                if (string.IsNullOrEmpty(ApplicationSettings.Default.XmrNetworkAddress))
                {
                    throw new Exception("Empty XMR Network Address");
                }

                // Get the Private Key
                AsymmetricCipherKeyPair rsaKey = _hardwareKey.getXmrKey();

                // Connect to XMR
                using (SubscriberSocket socket = new SubscriberSocket())
                {
                    // Bind
                    socket.Connect(ApplicationSettings.Default.XmrNetworkAddress);
                    socket.Subscribe("H");
                    socket.Subscribe(_hardwareKey.Channel);

                    // Notify
                    _clientInfoForm.XmrSubscriberStatus = "Connected to " + ApplicationSettings.Default.XmrNetworkAddress;

                    while (!_forceStop)
                    {
                        lock (_locker)
                        {
                            try
                            {
                                NetMQMessage message = socket.ReceiveMultipartMessage();

                                // Update status
                                string statusMessage = "Connected (" + ApplicationSettings.Default.XmrNetworkAddress + "), last activity: " + DateTime.Now.ToString();

                                // Write this out to a log
                                _clientInfoForm.XmrSubscriberStatus = statusMessage;
                                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", statusMessage), LogType.Audit.ToString());

                                // Deal with heart beat
                                if (message[0].ConvertToString() == "H")
                                {
                                    LastHeartBeat = DateTime.Now;
                                    continue;
                                }

                                // Decrypt the message
                                string opened = OpenSslInterop.decrypt(message[2].ConvertToString(), message[1].ConvertToString(), rsaKey.Private);

                                // Decode into a JSON string
                                PlayerAction action = JsonConvert.DeserializeObject <PlayerAction>(opened);

                                // Make sure the TTL hasn't expired
                                if (DateTime.Now > action.createdDt.AddSeconds(action.ttl))
                                {
                                    Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Expired Message: " + action.action), LogType.Info.ToString());
                                    continue;
                                }

                                // Decide what to do with the message, probably raise events according to the type of message we have
                                switch (action.action)
                                {
                                case "commandAction":

                                    // Create a schedule command out of the message
                                    Dictionary <string, string> obj     = JsonConvert.DeserializeObject <Dictionary <string, string> >(opened);
                                    ScheduleCommand             command = new ScheduleCommand();
                                    string code;
                                    obj.TryGetValue("commandCode", out code);
                                    command.Code = code;

                                    new Thread(new ThreadStart(command.Run)).Start();
                                    break;

                                case "collectNow":
                                case RevertToSchedulePlayerAction.Name:
                                    if (OnAction != null)
                                    {
                                        OnAction(action);
                                    }
                                    break;

                                case LayoutChangePlayerAction.Name:

                                    LayoutChangePlayerAction changeLayout = JsonConvert.DeserializeObject <LayoutChangePlayerAction>(opened);

                                    if (OnAction != null)
                                    {
                                        OnAction(changeLayout);
                                    }

                                    break;

                                case OverlayLayoutPlayerAction.Name:
                                    OverlayLayoutPlayerAction overlayLayout = JsonConvert.DeserializeObject <OverlayLayoutPlayerAction>(opened);

                                    if (OnAction != null)
                                    {
                                        OnAction(overlayLayout);
                                    }
                                    break;

                                case "screenShot":
                                    ScreenShot.TakeAndSend();
                                    _clientInfoForm.notifyStatusToXmds();
                                    break;

                                default:
                                    Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unknown Message: " + action.action), LogType.Info.ToString());
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                // Log this message, but dont abort the thread
                                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString());
                                _clientInfoForm.XmrSubscriberStatus = "Error. " + ex.Message;
                            }
                        }
                    }
                }

                // Update status
                _clientInfoForm.XmrSubscriberStatus = "Not Running, last activity: " + LastHeartBeat.ToString();

                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Subscriber Stopped"), LogType.Info.ToString());
            }
            catch (Exception e)
            {
                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unable to Subscribe to XMR: " + e.Message), LogType.Info.ToString());
                _clientInfoForm.XmrSubscriberStatus = e.Message;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Wait for a Message
        /// </summary>
        private void processMessage(NetMQMessage message, AsymmetricCipherKeyPair rsaKey)
        {
            // Update status
            string statusMessage = "Connected (" + ApplicationSettings.Default.XmrNetworkAddress + "), last activity: " + DateTime.Now.ToString();

            // Write this out to a log
            ClientInfo.Instance.XmrSubscriberStatus = statusMessage;
            Trace.WriteLine(new LogMessage("XmrSubscriber - Run", statusMessage), LogType.Audit.ToString());

            // Deal with heart beat
            if (message[0].ConvertToString() == "H")
            {
                LastHeartBeat = DateTime.Now;
                return;
            }

            // Decrypt the message
            string opened;

            try
            {
                opened = OpenSslInterop.decrypt(message[2].ConvertToString(), message[1].ConvertToString(), rsaKey.Private);
            }
            catch (Exception e)
            {
                Trace.WriteLine(new LogMessage("XmrSubscriber - processMessage", "Unopenable Message: " + e.Message), LogType.Error.ToString());
                Trace.WriteLine(new LogMessage("XmrSubscriber - processMessage", e.ToString()), LogType.Audit.ToString());
                return;
            }

            // Decode into a JSON string
            PlayerAction action = JsonConvert.DeserializeObject <PlayerAction>(opened);

            // Make sure the TTL hasn't expired
            if (DateTime.Now > action.createdDt.AddSeconds(action.ttl))
            {
                Trace.WriteLine(new LogMessage("XmrSubscriber - processMessage", "Expired Message: " + action.action), LogType.Info.ToString());
                return;
            }

            // Decide what to do with the message, probably raise events according to the type of message we have
            switch (action.action)
            {
            case "commandAction":

                // Create a schedule command out of the message
                Dictionary <string, string> obj     = JsonConvert.DeserializeObject <Dictionary <string, string> >(opened);
                ScheduleCommand             command = new ScheduleCommand();
                string code;
                obj.TryGetValue("commandCode", out code);
                command.Code = code;

                new Thread(new ThreadStart(command.Run)).Start();
                break;

            case "collectNow":
            case RevertToSchedulePlayerAction.Name:
                if (OnAction != null)
                {
                    OnAction(action);
                }
                break;

            case LayoutChangePlayerAction.Name:

                LayoutChangePlayerAction changeLayout = JsonConvert.DeserializeObject <LayoutChangePlayerAction>(opened);

                if (OnAction != null)
                {
                    OnAction(changeLayout);
                }

                break;

            case OverlayLayoutPlayerAction.Name:
                OverlayLayoutPlayerAction overlayLayout = JsonConvert.DeserializeObject <OverlayLayoutPlayerAction>(opened);

                if (OnAction != null)
                {
                    OnAction(overlayLayout);
                }
                break;

            case "screenShot":
                ScreenShot.TakeAndSend();
                ClientInfo.Instance.NotifyStatusToXmds();
                break;

            default:
                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unknown Message: " + action.action), LogType.Info.ToString());
                break;
            }
        }