Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package me.nzxter.Hackhost;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10. private static Scanner scanner;
  11. private static Process process;
  12. private static boolean running;
  13.  
  14. public static void main(String[] args) {
  15. running = true;
  16. System.out.println("For help, type 'help' or '?'");
  17. Runtime.getRuntime().addShutdownHook(new Thread(() -> {
  18. if (process != null) {
  19. process.destroy();
  20. }
  21.  
  22. scanner.close();
  23. }));
  24. scanner = new Scanner(System.in);
  25.  
  26. while(running) {
  27. System.out.print("> ");
  28. handleCommand(scanner.nextLine().split(" "));
  29. }
  30.  
  31. }
  32.  
  33. private static void handleCommand(String[] command) {
  34. if (!command[0].equalsIgnoreCase("help") && !command[0].equalsIgnoreCase("?")) {
  35. if (command[0].equalsIgnoreCase("run")) {
  36. if (command.length > 2) {
  37. try {
  38. String[] args = new String[]{"java", "-Xmx" + command[2], "-Xms" + command[2], "-jar", command[1]};
  39. process = Runtime.getRuntime().exec(args);
  40. System.out.println("Hackhost > The process was executed!");
  41. handleProcess();
  42. } catch (IOException var2) {
  43. var2.printStackTrace();
  44. }
  45. } else {
  46. System.out.println("Hackhost > Syntax: run <path> <ram>");
  47. }
  48. } else if (command[0].equalsIgnoreCase("stop")) {
  49. System.exit(0);
  50. }
  51. } else {
  52. System.out.println(">>> Help");
  53. System.out.println(">> Hackhost command");
  54. System.out.println("> run <path> <ram> : Allows you to start an jar.");
  55. System.out.println("> stop : Allows you to stop Hackhost.");
  56. System.out.println(">> Console Hackhost access");
  57. System.out.println("> Hackhost kill : Allows you to kill the server process.");
  58. System.out.println("");
  59. }
  60.  
  61. }
  62.  
  63. private static void handleProcess() {
  64. BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  65. Thread outputThread = new Thread(() -> {
  66. while(true) {
  67. try {
  68. String outputLine;
  69. if ((outputLine = outputReader.readLine()) != null) {
  70. System.out.println(outputLine);
  71. continue;
  72. }
  73. } catch (IOException var3) {
  74. var3.printStackTrace();
  75. }
  76.  
  77. return;
  78. }
  79. });
  80. outputThread.start();
  81. Thread errorThread = new Thread(() -> {
  82. BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  83.  
  84. String errorLine;
  85. try {
  86. while((errorLine = errorReader.readLine()) != null) {
  87. System.out.println(errorLine);
  88. }
  89. } catch (IOException var3) {
  90. var3.printStackTrace();
  91. }
  92.  
  93. });
  94. errorThread.start();
  95. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(process.getOutputStream());
  96.  
  97. while(process.isAlive()) {
  98. try {
  99. String line = scanner.nextLine();
  100. if (line.startsWith("Hackhost")) {
  101. String[] command = line.split(" ");
  102. if (command.length > 1 && command[1].equalsIgnoreCase("kill")) {
  103. process.destroy();
  104. process = null;
  105. System.out.println("Hackhost > The process was killed!");
  106. return;
  107. }
  108.  
  109. System.out.println("Hackhost > Unknown command");
  110. } else {
  111. outputStreamWriter.write(line + "\n");
  112. outputStreamWriter.flush();
  113. }
  114. } catch (Exception var6) {
  115. var6.printStackTrace();
  116. }
  117. }
  118.  
  119. process = null;
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement