Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.nzxter.Hackhost;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.Scanner;
- public class Main {
- private static Scanner scanner;
- private static Process process;
- private static boolean running;
- public static void main(String[] args) {
- running = true;
- System.out.println("For help, type 'help' or '?'");
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
- if (process != null) {
- process.destroy();
- }
- scanner.close();
- }));
- scanner = new Scanner(System.in);
- while(running) {
- System.out.print("> ");
- handleCommand(scanner.nextLine().split(" "));
- }
- }
- private static void handleCommand(String[] command) {
- if (!command[0].equalsIgnoreCase("help") && !command[0].equalsIgnoreCase("?")) {
- if (command[0].equalsIgnoreCase("run")) {
- if (command.length > 2) {
- try {
- String[] args = new String[]{"java", "-Xmx" + command[2], "-Xms" + command[2], "-jar", command[1]};
- process = Runtime.getRuntime().exec(args);
- System.out.println("Hackhost > The process was executed!");
- handleProcess();
- } catch (IOException var2) {
- var2.printStackTrace();
- }
- } else {
- System.out.println("Hackhost > Syntax: run <path> <ram>");
- }
- } else if (command[0].equalsIgnoreCase("stop")) {
- System.exit(0);
- }
- } else {
- System.out.println(">>> Help");
- System.out.println(">> Hackhost command");
- System.out.println("> run <path> <ram> : Allows you to start an jar.");
- System.out.println("> stop : Allows you to stop Hackhost.");
- System.out.println(">> Console Hackhost access");
- System.out.println("> Hackhost kill : Allows you to kill the server process.");
- System.out.println("");
- }
- }
- private static void handleProcess() {
- BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
- Thread outputThread = new Thread(() -> {
- while(true) {
- try {
- String outputLine;
- if ((outputLine = outputReader.readLine()) != null) {
- System.out.println(outputLine);
- continue;
- }
- } catch (IOException var3) {
- var3.printStackTrace();
- }
- return;
- }
- });
- outputThread.start();
- Thread errorThread = new Thread(() -> {
- BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- String errorLine;
- try {
- while((errorLine = errorReader.readLine()) != null) {
- System.out.println(errorLine);
- }
- } catch (IOException var3) {
- var3.printStackTrace();
- }
- });
- errorThread.start();
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter(process.getOutputStream());
- while(process.isAlive()) {
- try {
- String line = scanner.nextLine();
- if (line.startsWith("Hackhost")) {
- String[] command = line.split(" ");
- if (command.length > 1 && command[1].equalsIgnoreCase("kill")) {
- process.destroy();
- process = null;
- System.out.println("Hackhost > The process was killed!");
- return;
- }
- System.out.println("Hackhost > Unknown command");
- } else {
- outputStreamWriter.write(line + "\n");
- outputStreamWriter.flush();
- }
- } catch (Exception var6) {
- var6.printStackTrace();
- }
- }
- process = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement