This repository was archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiver.java
More file actions
250 lines (230 loc) · 8.77 KB
/
Receiver.java
File metadata and controls
250 lines (230 loc) · 8.77 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/********************************
* Author: Bassel Cheaib *
* Date: May 2017 *
********************************/
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.Scanner;
public class Receiver {
public static boolean running = true; //when started, the receiver will be running and waiting for commands
public static String IP = ""; //it gets its IP on launch and saves it
public static final int port = 7000; //and works on port 7000, just like the sender
public static final Scanner input = new Scanner(System.in); //it takes input from user using scanner
public static void main(String[] args){
printHeader(); //The header of the program
try { //save the local IP
IP = InetAddress.getLocalHost().getHostAddress();
} catch (Exception e){}
System.out.println("Your IP is: "+IP); //display it so that it becomes available for the sender
while (running){
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("\nNCT >>> Waiting for commands...");
Socket socket = serverSocket.accept();
System.out.println("\nNCT >>> Connection received from "+socket.getRemoteSocketAddress());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); //read from socket
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); //write to socket
String s = br.readLine();
execute(s, br, out);
serverSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void execute(String s, BufferedReader br, PrintWriter out){
if (s == null || s.equals(""))
return;
String[] toExecute = s.split(" ");
String command = toExecute[0].toLowerCase();
System.out.print("\nNCT >>> Requested command: ");
switch(command){
case "freezemouse":
System.out.println("Freeze mouse for " + toExecute[1]+" seconds");
freezeMouse(Integer.parseInt(toExecute[1]));
break;
case "crazymouse":
System.out.println("Crazy mouse for " + toExecute[1]+" seconds");
crazyMouse(Integer.parseInt(toExecute[1]));
break;
case "funnymessage":
System.out.println("Display surprise message ");
funnyMsg(toExecute[1]);
break;
case "shutdown":
System.out.println("Shutdown computer after " + toExecute[1]+" seconds");
shutdown(Integer.parseInt(toExecute[1]));
break;
case "quit":
quit();
break;
case "runmatrix":
System.out.println("Run matrix");
runMatrix();
break;
case "streamstarwars":
System.out.println("Stream Star Wars");
streamStarWars();
break;
case "sendmessage":
receiveMessage(toExecute[1]);
break;
case "chat":
try {
chat(br, out);
} catch (Exception e) {
e.printStackTrace();
}
break;
case "getdeviceip":
System.out.println("Get this device IP");
getDeviceIP(out);
break;
default:
System.out.println("Command not found, please try again later...");
break;
}
}
public static void freezeMouse(int time){
try {
long to = System.currentTimeMillis() + time*1000;
while (System.currentTimeMillis() < to){
Robot robot = new Robot();
robot.mouseMove(0 , 0);
}
} catch (AWTException e1) {
e1.printStackTrace();
}
}
public static void crazyMouse(int time){
try {
Robot robot = new Robot();
Random random = new Random();
long to = System.currentTimeMillis() + time*1000;
while (System.currentTimeMillis() < to){
robot.mouseMove(random.nextInt(400), random.nextInt(400));
Thread.sleep(300);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void funnyMsg(String msg){
try {
msg = msg.replace('~', ' ');
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c start notepad");
Thread.sleep(1500);
Robot robot = new Robot();
for (int i = 0; i < msg.length(); i++) {
char c = msg.charAt(i);
if (Character.isUpperCase(c)) {
robot.keyPress(KeyEvent.VK_SHIFT);
}
robot.keyPress(Character.toUpperCase(c));
robot.keyRelease(Character.toUpperCase(c));
if (Character.isUpperCase(c)) {
robot.keyRelease(KeyEvent.VK_SHIFT);
}
robot.delay(150);
}
} catch (Exception e){
e.printStackTrace();
}
}
public static void shutdown(int time){
Runtime rt = Runtime.getRuntime();
System.out.println("Your computer will shutdown in "+time+" seconds.");
try {
rt.exec("cmd /c start cmd.exe /K \"c:\\Windows\\System32\\shutdown.exe\" /s /t "+time);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void quit(){
try {
System.out.print("\nProgram will quit in 3 ");
Thread.sleep(800);
System.out.print("2 ");
Thread.sleep(800);
System.out.print("1 ");
Thread.sleep(800);
System.out.print("0 ");
} catch (InterruptedException e) {}
running = false;
}
public static void runMatrix(){
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd /c start cmd.exe /K \"matrix.bat\"");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void streamStarWars(){
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd.exe /c start telnet Towel.blinkenlights.nl");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void receiveMessage(String msg){
System.out.println("\nNew message arrived!");
System.out.println("\tContent: "+msg.replace('~', ' '));
}
public static void chat(BufferedReader br, PrintWriter out) throws Exception{
boolean on = true;
System.out.println("\nChatting started! Type \"exit\" to exit the chat");
//on the receiving side, the sender has already sent the first message so we start by reading it
//then we reply to it
while (on){
String received = br.readLine();
System.out.println("him>>> "+received);
if (received.equalsIgnoreCase("bye!")){
//the sender has ended the chat
on = false;
break;
}
System.out.print("you>>> ");
String reply = input.nextLine();
if (reply.equalsIgnoreCase("exit")){
out.println("Bye!");
on = false; //end the chat
}
out.println(reply);
}
}
public static void getDeviceIP(PrintWriter out){
out.println(IP);
}
public static void printHeader(){
System.out.println();
System.out.println(" ##########################################################################");
System.out.println(" ## ##");
System.out.println(" ## ###### ### ################ ################## ##");
System.out.println(" ## ####### ### ############### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ## ### ### ### ##");
System.out.println(" ## ### ###### ################# ### ##");
System.out.println(" ## ### ##### ################# ### ##");
System.out.println(" ## ##");
System.out.println(" ##########################################################################");
System.out.println("\nThis is the \"NCT\" remote commands executer.");
System.out.println("Please run the Sender on the sending side for the program to work...\n");
}
}