//Method that handles monitoring Whitelist public static void monitorWhitelist(object state) { //Run a loop to continue running service every 15 seconds while (!BW_Service._stopping) { Thread.Sleep(100); List <KeyValuePair <string, Application> > toRemovePrograms = null; List <KeyValuePair <string, Website> > toRemoveWebsites = null; //Check Whitelist with current running programs foreach (KeyValuePair <string, Application> p in _programs) { //Compare the running times to the time limit. If Over the time limit, remove from whitelist and add to blacklist if (p.Value.getLimit() != 0 && p.Value.getRuntime().TotalSeconds > p.Value.getLimit()) { FireBaseFunctions.addToList(p.Key.ToLower(), 0, "black", "programs"); FireBaseFunctions.removeFromList(p.Key.ToLower(), "white", "programs"); if (toRemovePrograms == null) { toRemovePrograms = new List <KeyValuePair <string, Application> >(); toRemovePrograms.Add(p); } } } if (toRemovePrograms != null) { foreach (KeyValuePair <string, Application> p in toRemovePrograms) { _programs.Remove(p.Key); } } //Check Whitelist with current running websites foreach (KeyValuePair <string, Website> w in _websites) { if (w.Value.getTimeLimit() != -600 && w.Value.getRuntime().TotalSeconds > w.Value.getTimeLimit()) { string website = w.Value.getWebURL().ToLower().Replace("www.", "").Replace('.', '*'); FireBaseFunctions.addToList(website, 0, "black", "websites"); FireBaseFunctions.removeFromList(website, "white", "websites"); if (toRemoveWebsites == null) { toRemoveWebsites = new List <KeyValuePair <string, Website> >(); toRemoveWebsites.Add(w); } } } if (toRemoveWebsites != null) { foreach (KeyValuePair <string, Website> w in toRemoveWebsites) { _websites.Remove(w.Key); } } } BW_Service._stoppedEvent.Set(); //END monitorWhitelist method }
//////////////////////////////////////////////////// // // 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(); }
//Method responsible for returning allocated time in whitelist public static string allocatedTime(string type, string name) { //Check what we are querying, program or website string url; if (type == "website") { url = "/websites.json"; name = name.ToLower().Replace("www.", ""); name = name.Replace('.', '*'); } else { url = "/programs.json"; name = name.ToLower(); } //Query Dictionary <string, string> whitelist = JsonConvert.DeserializeObject <Dictionary <string, string> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentWhitelistURL() + url)); if (!whitelist.ContainsKey(name)) { return("no limit set"); } else { return(whitelist[name]); } }
//Static constructor that will have initial running programs static Monitor() { _serviceStart = DateTime.Now; _informedOfShutdown = false; //Initial look at running programs _programs = new Dictionary <string, Application>(); _websites = new Dictionary <string, Website>(); //Check for only those with open window Process[] runningPrograms = Process.GetProcesses().Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray(); foreach (Process p in runningPrograms) { //If it is not already inside the list, 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 remove case sensitive when adding to dictionary string name = p.ProcessName.ToLower(); DateTime startTime; try { startTime = p.StartTime; } catch (Exception ex) { startTime = DateTime.Now; } if (!whitelist.ContainsKey(name)) { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, startTime, 0); } else { _programs[p.ProcessName.ToLower()] = new Application(p.ProcessName, p.MainWindowTitle, startTime, whitelist[name]); } } } }
//Method that handles monitoring Blacklist public static void monitorBlacklist(object state) { //Run a loop to continue running service every 15 seconds. CHANGE TIMING LATER while (!BW_Service._stopping) { Thread.Sleep(100); //Query Blacklist Programs from Firebase and store values in a dictionary. URL MAY CHANGE Dictionary <string, double> blacklist = JsonConvert.DeserializeObject <Dictionary <string, double> >(FireBaseFunctions.queryList(FireBaseFunctions.getCurrentBlacklistURL() + "/programs.json")); //Monitor the blacklist //Check for only those with open window Process[] runningPrograms = Process.GetProcesses().Where(p => !String.IsNullOrEmpty(p.MainWindowTitle)).ToArray(); foreach (Process p in runningPrograms) { //Check if a running program is in the blacklist foreach (KeyValuePair <string, double> bs in blacklist) { //If program name is in the blacklist and it is running, kill it if (p.ProcessName.ToLower().Contains(bs.Key)) { p.Kill(); } } } } BW_Service._stoppedEvent.Set(); //END monitorBlacklist Method }
/*//////////////////////////////////////////////// * / * / 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; } }