-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainThread.java
More file actions
128 lines (125 loc) · 3.98 KB
/
MainThread.java
File metadata and controls
128 lines (125 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Write a description of class MainThread here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.JOptionPane;
public class MainThread extends Thread{
private Socket sock;
private DataOutputStream out;
private DataInputStream in;
public boolean running=false;
private String password;
private Robot robot;
/*
* constructor. The password should be hashed!
*/
public MainThread(Socket s, String password){
//System.out.println("mainthreadsdfsfsfsdfdf"+password);
try{
this.sock=s;
this.password=password;
out=new DataOutputStream(s.getOutputStream());
in=new DataInputStream(s.getInputStream());
robot=new Robot();
Runtime.getRuntime().addShutdownHook(new MouseReleaseThread());
//System.out.println("Password: " + password);
}
catch(AWTException ae){
System.err.println("Robot init failed: " + ae);
}
catch(Exception e){
System.err.println("Error during MainThread init: " + e);
}
if(!authenticate()){
try{
sock.close();
JOptionPane.showMessageDialog(null, "The password was incorrect.");
System.exit(0);
}
catch(Exception e){
e.printStackTrace();
System.err.println(e);
System.exit(42);
}
}
}
public boolean authenticate(){
//System.out.println(password);
try{
if(in.read()!=0){
return false;
}
if(password.equals(in.readUTF())){
return true;
}
return false;
}
catch(Exception e){
e.printStackTrace();
System.out.println("Auth fail:" + e);
return false;
}
}
public void run(){
running=true;
System.out.println("MainThread running..");
while(running&&!sock.isClosed()){
try{
int id=in.read();
System.out.println(id);
switch(id){
case 1: //keyboard keydown
robot.keyPress((int)in.readShort());
break;
case 2: //keyboard keyUp
robot.keyRelease((int)in.readShort());
break;
case 3:
robot.mousePress((int)in.readByte());
break;
case 4:
robot.mouseRelease((int)in.readByte());
break;
case 5:
Point curPoint=MouseInfo.getPointerInfo().getLocation();
robot.mouseMove(curPoint.x + in.readInt(), curPoint.y + in.readInt());
break;
case 6: //mouseWheel
robot.mouseWheel(in.readByte());
break;
case -1:
System.out.println("Disconnected");
running=false;
System.exit(0);
break;
default:
System.err.println("unrecognized packet: " + id);
running=false;
break;
}
}
catch(IOException ie){
System.err.println("IOException: " + ie);
break;
}
}
try{
sock.close();
}
catch(Exception e){
}
running=false;
}
private class MouseReleaseThread extends Thread{
public void run(){
System.out.println("releasing mouse buttons");
robot.mouseRelease(16|8|4);
System.out.println("released mouse buttons");
}
}
}