//////////////////////////////////////////////////// // // MONITORING THREADS // //////////////////////////////////////////////////// //Responsible for updating runtimes and running programs //If a program is closed, it will temporarily stop updating for that program public static void monitorRuntimes(object state) { while (!BW_Service._stopping) { Thread.Sleep(100); //Update the running programs list //Check for only those with open window if ((DateTime.Now - _serviceStart).TotalSeconds > FireBaseFunctions.queryComputerTime()) { if (Program.dontShutDown) { if (!_informedOfShutdown) { Console.WriteLine("Not shutting down on request"); _informedOfShutdown = true; } } else { System.Diagnostics.Process.Start("shutdown", "/s /f /t 0"); } } Process[] runningPrograms = Process.GetProcesses().Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray(); //Initial add foreach (Process p in runningPrograms) { //If it is not inside the dictionary, add it if (!_programs.ContainsKey(p.ProcessName.ToLower())) { //Query Whitelist from Firebase and store in a dictionary. URL MAY CHANGE Dictionary <string, double> whitelist = JsonConvert.DeserializeObject <Dictionary <string, double> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentWhitelistURL() + "/programs.json")); //Modify to be able to ignore case sensitive string name = p.ProcessName.ToLower(); if (!whitelist.ContainsKey(name)) { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, p.StartTime, 0); } else { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, p.StartTime, whitelist[name]); } } } //Now loop through active programs, updating the runtimes foreach (KeyValuePair <string, Application> check in _programs) { foreach (Process p in runningPrograms) { //If it is running and the program is in the dictionary, update the runtime if (check.Key.Contains(p.ProcessName.ToLower())) { check.Value.updateRuntime(); } } //If it is not running and the program is in the dictionary, update the startime check.Value.updateStartTime(); } } BW_Service._stoppedEvent.Set(); }
/*//////////////////////////////////////////////// * / * / Service Command Processing * / * /////////////////////////////////////////////////*/ //This Method is responsible for processing the Received messages from the clients //CHANGE, change once communications protocol is setup for sure private void processCommands(MessageEventArgs e) { //Split up the command, CHANGE once we decide on what character to split by /* * commandReceived[0] = Requester * commandReceived[1] = Request/Command * commandReceived[2] = RequestNumber * commandReceived[3] = Parameters */ String[] commandRecieved = e.Data.Split(':'); string response; //Use a switch statement that will decide what will happen switch (commandRecieved[1].ToLower()) { //Query the current security level set case "request_securitylevel": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions._securityLevel)); break; //Change the security level case "set_securitylevel": response = FireBaseFunctions.changeSecurityLevel(commandRecieved[3]); Send(String.Format("{0}:{1}", e.Data, response)); break; ///////////////////////////////////// // Requesting lists ///////////////////////////////////// case "request_blacklistprograms": response = FireBaseFunctions.requestList("black", "programs"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_blacklistwebsites": response = FireBaseFunctions.requestList("black", "websites"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_whitelistprograms": response = FireBaseFunctions.requestList("white", "programs"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_whitelistwebsites": response = FireBaseFunctions.requestList("white", "websites"); Send(String.Format("{0}:{1}", e.Data, response)); break; ///////////////////////////////////// // Requesting removal from lists ///////////////////////////////////// case "request_removefromblacklistprograms": response = FireBaseFunctions.removeFromList(commandRecieved[3].ToLower(), "black", "programs"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_removefromblacklistwebsites": response = FireBaseFunctions.removeFromList(commandRecieved[3].ToLower(), "black", "websites"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_removefromwhitelistprograms": response = FireBaseFunctions.removeFromList(commandRecieved[3].ToLower(), "white", "programs"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_removefromwhitelistwebsites": response = FireBaseFunctions.removeFromList(commandRecieved[3].ToLower(), "white", "websites"); Send(String.Format("{0}:{1}", e.Data, response)); break; ///////////////////////////////////// // Requesting add to lists ///////////////////////////////////// case "request_addtoblacklistprograms": response = FireBaseFunctions.addToList(commandRecieved[3].ToLower(), 0, "black", "programs"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_addtoblacklistwebsites": response = FireBaseFunctions.addToList(commandRecieved[3].ToLower(), 0, "black", "websites"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_addtowhitelistprograms": String[] programToAdd = commandRecieved[3].Split(','); response = FireBaseFunctions.addToList(programToAdd[0].ToLower(), Int32.Parse(programToAdd[1]), "white", "programs"); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_addtowhitelistwebsites": String[] websiteToAdd = commandRecieved[3].Split(','); response = FireBaseFunctions.addToList(websiteToAdd[0].ToLower(), Int32.Parse(websiteToAdd[1]), "white", "websites"); Send(String.Format("{0}:{1}", e.Data, response)); break; ///////////////////////////////////// // Monitoring Commands ///////////////////////////////////// case "request_programisrunning": Send(String.Format("{0}:{1}", e.Data, Monitor.isRunning(commandRecieved[3]))); break; case "request_timespentprogram": Send(String.Format("{0}:{1}", e.Data, Monitor.programRuntime(commandRecieved[3]))); break; case "request_timeremainingprogram": Send(String.Format("{0}:{1}", e.Data, Monitor.programRemainingTime(commandRecieved[3]))); break; case "request_addTime": Send(String.Format("{0}:{1}", e.Data, Monitor.programAddTime(commandRecieved[3].Split(',')[0], int.Parse(commandRecieved[3].Split(',')[1])))); break; case "request_totaltimeallocatedprogram": Send(String.Format("{0}:{1}", e.Data, Monitor.allocatedTime("program", commandRecieved[3]))); break; case "request_totaltimeallocatedwebsite": Send(String.Format("{0}:{1}", e.Data, Monitor.allocatedTime("website", commandRecieved[3]))); break; case "request_timeremainingwebsite": Send(String.Format("{0}:{1}", e.Data, Monitor.websiteRemainingTime(commandRecieved[3]))); break; case "request_timespentwebsite": Send(String.Format("{0}:{1}", e.Data, Monitor.websiteRuntime(commandRecieved[3]))); break; ///////////////////////////////////// // Parent Settings Commands ///////////////////////////////////// case "request_notificationcanemail": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.requestSettings("can_email"))); break; case "request_notificationcansms": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.requestSettings("can_sms"))); break; case "request_notificationemailaddress": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.requestSettings("parent_email"))); break; case "request_notificationsmsnumber": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.requestSettings("parent_sms"))); break; case "set_notificationemailbool": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.updateSettings("can_email", commandRecieved[3]))); break; case "set_notificationsmsbool": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.updateSettings("can_sms", commandRecieved[3]))); break; case "set_notificationsms": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.updateSettings("sms", commandRecieved[3]))); break; case "set_notificationemail": Send(String.Format("{0}:{1}", e.Data, FireBaseFunctions.updateSettings("email", commandRecieved[3]))); break; ///////////////////////////////////////////////////////////// // Extensions Recieving data, update dict in monitoring class ///////////////////////////////////////////////////////////// case "logstarttime": Monitor.addToWebsiteList(commandRecieved[4], commandRecieved[3]); //Send("ok"); //For Testing break; case "logendtime": Monitor.websiteListEndTime(commandRecieved[4], commandRecieved[3]); break; case "request_programlist": response = String.Join(",", new String[] { "Spotify", "MinecraftLauncher", "Chrome", "Firefox", "Calculator", "Notepad" }); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_programsused": response = String.Join(",", Monitor.getProgramsRan()); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_websitesvisited": response = String.Join(",", Monitor.getWebsitesVisited()); Send(String.Format("{0}:{1}", e.Data, response)); break; ///////////////////////////////////////////////////////////// // updated the total computer time ///////////////////////////////////////////////////////////// case "request_totalcomputertime": response = FireBaseFunctions.queryComputerTime().ToString(); Send(String.Format("{0}:{1}", e.Data, response)); break; case "request_remainingcomputertime": response = Monitor.getRemainingComputerTime().ToString(); Send(String.Format("{0}:{1}", e.Data, response)); break; case "set_totalcomputertime": response = FireBaseFunctions.changeComputerTime(int.Parse(commandRecieved[3])); Send(String.Format("{0}:{1}", e.Data, response)); break; //Command not found, or incorrect format default: Send(String.Format("{0}:{1}", e.Data, "command not found")); break; } }