|
@@ -0,0 +1,232 @@
|
|
|
|
|
+// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
|
|
|
|
|
+// then press Enter. You can now see whitespace characters in your code.
|
|
|
|
|
+
|
|
|
|
|
+package au.pfip;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * The code below will print the words Hello World
|
|
|
|
|
+ * to the screen, and it is amazing
|
|
|
|
|
+ */
|
|
|
|
|
+public class Main {
|
|
|
|
|
+
|
|
|
|
|
+ private static final double version = 4.0;
|
|
|
|
|
+ public static final String GamesURL = "https://overlord.pfip.au/e107_plugins/timekeeper/timekeeperClientServices/games.json";
|
|
|
|
|
+ public static final String PlaytimeURL = "https://overlord.pfip.au/e107_plugins/timekeeper/timekeeperClientServices/timer.php";
|
|
|
|
|
+ private static String runningGame = "";
|
|
|
|
|
+ private static String activeUser = "";
|
|
|
|
|
+ private static int playTimeRemaining = 0;
|
|
|
|
|
+ private static Boolean shutdownFlag = false;
|
|
|
|
|
+ public static String[] GamesList;
|
|
|
|
|
+ private static String[] Users;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param args Commandline arguments
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+
|
|
|
|
|
+ // If we couldn't get/make settings just exit as no config available
|
|
|
|
|
+ if(!settings.initialiseSettings())
|
|
|
|
|
+ {
|
|
|
|
|
+ System.out.println("Settings engine failed to initialise, exiting now.");
|
|
|
|
|
+ burnTime(600);
|
|
|
|
|
+ System.exit(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // If we can't make log files just go on without them
|
|
|
|
|
+ if(!logging.initialiseLogging())
|
|
|
|
|
+ {
|
|
|
|
|
+ System.out.println("Logging engine failed to initialise, we can live without this.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logging.information("Starting Timekeeper " + version);
|
|
|
|
|
+ boolean timekeeperActive = Boolean.parseBoolean(settings.getSetting("TimekeeperActive"));
|
|
|
|
|
+
|
|
|
|
|
+ if(!timekeeperActive)
|
|
|
|
|
+ {
|
|
|
|
|
+ logging.information("Timekeeper is set to not active, bailing out after a long delay.");
|
|
|
|
|
+ System.out.println("Timekeeper is set to not active, bailing out after a long delay.");
|
|
|
|
|
+ burnTime(600);
|
|
|
|
|
+ System.exit(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Users = settings.getSetting("UsersToMonitor").split(",");
|
|
|
|
|
+
|
|
|
|
|
+ if(Shell.check("/usr/bin/nmcli | /usr/bin/grep \"connected to \"",settings.getSetting("ExemptNetwork")))
|
|
|
|
|
+ {
|
|
|
|
|
+ logging.information("Network " + settings.getSetting("ExemptNetwork") + "detected.");
|
|
|
|
|
+ burnTime(600);
|
|
|
|
|
+ System.exit(0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Get list of games to be monitored.
|
|
|
|
|
+ communications.getGamesListFromServer();
|
|
|
|
|
+
|
|
|
|
|
+ // Inform users that the Timekeeper is starting up.
|
|
|
|
|
+ for (String user : Users) {
|
|
|
|
|
+ communications.msgPopup("Starting up version " + version + ".",user);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ registerShutdownHook();
|
|
|
|
|
+ while(!shutdownFlag) {
|
|
|
|
|
+
|
|
|
|
|
+ startChecks();
|
|
|
|
|
+
|
|
|
|
|
+ checkPlayTime();
|
|
|
|
|
+
|
|
|
|
|
+ killGames();
|
|
|
|
|
+
|
|
|
|
|
+ burnTime(60);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Inform users that the Timekeeper is shutting down.
|
|
|
|
|
+ for (String user : Users) {
|
|
|
|
|
+ communications.msgPopup("Shutting down.",user);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logging.information("Shutting down.");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Kill running games if there is no playtime left.
|
|
|
|
|
+ */
|
|
|
|
|
+ private static void killGames()
|
|
|
|
|
+ {
|
|
|
|
|
+ if(!Objects.equals(runningGame, "") && playTimeRemaining <= 0) {
|
|
|
|
|
+ System.out.println("Killing " + runningGame + " for user " + activeUser);
|
|
|
|
|
+ communications.msgPopup("Killing: " + runningGame + " as you are out of play time.",activeUser);
|
|
|
|
|
+ killGame(runningGame,activeUser);
|
|
|
|
|
+ //Shell.check("ps aux | grep " + user + " | grep " + game + " | grep -v \"grep\"", game)
|
|
|
|
|
+ }else if(!Objects.equals(runningGame, "") && playTimeRemaining == 30){
|
|
|
|
|
+ communications.msgPopup("Notice: You have 30 minutes remaining.",activeUser);
|
|
|
|
|
+ }else if(!Objects.equals(runningGame, "") && playTimeRemaining == 5){
|
|
|
|
|
+ communications.msgPopup("Notice: You have 5 minutes remaining.",activeUser);
|
|
|
|
|
+ }else if(!Objects.equals(runningGame, "") && playTimeRemaining == 4){
|
|
|
|
|
+ communications.msgPopup("Notice: You have 4 minutes remaining.",activeUser);
|
|
|
|
|
+ }else if(!Objects.equals(runningGame, "") && playTimeRemaining == 3){
|
|
|
|
|
+ communications.msgPopup("Notice: You have 3 minutes remaining.",activeUser);
|
|
|
|
|
+ }else if(!Objects.equals(runningGame, "") && playTimeRemaining == 2){
|
|
|
|
|
+ communications.msgPopup("Notice: You have 2 minutes remaining.",activeUser);
|
|
|
|
|
+ }else if(!Objects.equals(runningGame, "") && playTimeRemaining == 1){
|
|
|
|
|
+ communications.msgPopup("Notice: You have 1 minute remaining.",activeUser);
|
|
|
|
|
+ }else if(!Objects.equals(activeUser, "")){
|
|
|
|
|
+ System.out.println(activeUser + " still has playtime remaining (" + playTimeRemaining + " minutes).");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Get remaining playtime from the server
|
|
|
|
|
+ */
|
|
|
|
|
+ private static void checkPlayTime()
|
|
|
|
|
+ {
|
|
|
|
|
+ if(!Objects.equals(runningGame, "")) {
|
|
|
|
|
+ System.out.println(runningGame);
|
|
|
|
|
+ playTimeRemaining = communications.getPlaytimeFromServer(activeUser, runningGame);
|
|
|
|
|
+ System.out.println(playTimeRemaining);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ System.out.println("No games running.");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void startChecks()
|
|
|
|
|
+ {
|
|
|
|
|
+ for (String user : Users) {
|
|
|
|
|
+ System.out.println("Checking running games for " + user);
|
|
|
|
|
+ checkRunningGames(user);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void checkRunningGames(String user)
|
|
|
|
|
+ {
|
|
|
|
|
+ for (String game : GamesList) {
|
|
|
|
|
+ // ps aux | grep gregory | grep minecraft | grep -v grep
|
|
|
|
|
+ if (Shell.check("/usr/bin/ps aux | /usr/bin/grep " + user + " | /usr/bin/grep " + game + " | /usr/bin/grep -v grep", game)) {
|
|
|
|
|
+ runningGame = game;
|
|
|
|
|
+ activeUser = user;
|
|
|
|
|
+ //System.out.println("Dis true---------");
|
|
|
|
|
+ System.out.println(game + " detected running.");
|
|
|
|
|
+ return;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //System.out.println(game + " not running.");
|
|
|
|
|
+ runningGame = "";
|
|
|
|
|
+ activeUser = "";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Burn 60 seconds while watching for shutdown signal.
|
|
|
|
|
+ */
|
|
|
|
|
+ private static void burnTime(int time)
|
|
|
|
|
+ {
|
|
|
|
|
+ System.out.println("burning 60s.");
|
|
|
|
|
+ int timer = time;
|
|
|
|
|
+ while(!shutdownFlag && timer!=0) {
|
|
|
|
|
+ timer--;
|
|
|
|
|
+ try {
|
|
|
|
|
+ //System.out.println("burning " + timer + "s.");
|
|
|
|
|
+ //noinspection BusyWait
|
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ logging.error("RuntimeException(e) ~ " + e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private static void registerShutdownHook() {
|
|
|
|
|
+ final Thread mainThread = Thread.currentThread();
|
|
|
|
|
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ System.out.println("Shutting down.");
|
|
|
|
|
+ logging.information("Sigterm received.");
|
|
|
|
|
+ Main.setShutdownProcess();
|
|
|
|
|
+ mainThread.join();
|
|
|
|
|
+ } catch (InterruptedException ex) {
|
|
|
|
|
+ logging.error("RuntimeException(e) ~ " + ex);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static public void setShutdownProcess() {
|
|
|
|
|
+ shutdownFlag = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static void killGame(String game, String user)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i <= 10; i++) {
|
|
|
|
|
+ if (Shell.check("/usr/bin/ps aux | /usr/bin/grep " + user + " | /usr/bin/grep " + game + " | /usr/bin/grep -v grep", game)) {
|
|
|
|
|
+ try{
|
|
|
|
|
+ if(i >= 8) {
|
|
|
|
|
+ Shell.runCmd("/usr/bin/pkill -9 -f " + game);
|
|
|
|
|
+ logging.information("/usr/bin/pkill -9 -f " + game);
|
|
|
|
|
+ logging.information("Killing " + game + " forcefully.");
|
|
|
|
|
+ }else{
|
|
|
|
|
+ Shell.runCmd("/usr/bin/pkill -15 -f " + game);
|
|
|
|
|
+ logging.information("/usr/bin/pkill -15 -f " + game);
|
|
|
|
|
+ logging.information("Killing " + game + " gracefully.");
|
|
|
|
|
+ }
|
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ logging.error("InterruptedException(e) ~ " + e);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //System.out.println(game + " not running.");
|
|
|
|
|
+ runningGame = "";
|
|
|
|
|
+ activeUser = "";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// check for users logged in now.
|
|
|
|
|
+//ps -eo user,uid | awk 'NR>1 && $2 >= 1000 && ++seen[$2]==1{print $1}'
|