-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.java
145 lines (133 loc) · 6.99 KB
/
Server.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* Iterative Server
*/
public class Server {
public static void main(String[] args) {
//Setup and prep
System.out.println("Booting Server...");
Scanner userInput = new Scanner(System.in);
Process process = null;
BufferedReader processOut = null;
String currentStreamline = "";
//Get port to observe
System.out.println("Enter port to observe. Range 1025-4998: ");
int observedPort = userInput.nextInt();
try {
//Create socket server
System.out.println("Creating socket for port " + observedPort + "...");
ServerSocket serverSocket = new ServerSocket(observedPort);
System.out.println("Socket created.");
while (true) {
//Take in input from observed port
System.out.println("Waiting for client requests...");
Socket currentRequest = serverSocket.accept();
long requestStartTime = System.currentTimeMillis();
String requestClientInfo = currentRequest.getInetAddress().getHostAddress() + ":" + currentRequest.getPort();
//Determine data request type
BufferedReader reader = new BufferedReader(new InputStreamReader(currentRequest.getInputStream()));
int dataRequest = Integer.parseInt(reader.readLine());
String requestResponse = "";
switch (dataRequest) {
case 0:
//DateTime request
System.out.println("Date and time request received from: " + requestClientInfo);
process = Runtime.getRuntime().exec(args = new String[]{"date"});
processOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
requestResponse = "Date/Time: \n";
currentStreamline = "";
while ((currentStreamline = processOut.readLine()) != null) {
requestResponse += currentStreamline + "\n";
}
processOut.close();
break;
case 1:
//Uptime request
System.out.println("Uptime request received from: " + requestClientInfo);
process = Runtime.getRuntime().exec(args = new String[]{"uptime"});
processOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
requestResponse = "Uptime: \n";
currentStreamline = "";
while ((currentStreamline = processOut.readLine()) != null) {
requestResponse += currentStreamline + "\n";
}
processOut.close();
break;
case 2:
//Memory request
System.out.println("Memory usage request received from: " + requestClientInfo);
process = Runtime.getRuntime().exec(args = new String[]{"free", "-m"});
processOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
requestResponse = "Memory Usage: \n";
currentStreamline = "";
while ((currentStreamline = processOut.readLine()) != null) {
requestResponse += currentStreamline + "\n";
}
processOut.close();
break;
case 3:
//Netstat request
System.out.println("Netstat request received from: " + requestClientInfo);
process = Runtime.getRuntime().exec(args = new String[]{"netstat"});
processOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
requestResponse = "Network Connections: \n";
currentStreamline = "";
while ((currentStreamline = processOut.readLine()) != null) {
requestResponse += currentStreamline + "\n";
}
processOut.close();
break;
case 4:
//User list request
System.out.println("User list request received from: " + requestClientInfo);
process = Runtime.getRuntime().exec(args = new String[]{"who"});
processOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
requestResponse = "Current Users: \n";
currentStreamline = "";
while ((currentStreamline = processOut.readLine()) != null) {
requestResponse += currentStreamline + "\n";
}
processOut.close();
break;
case 5:
//Process list request
System.out.println("Process list request received from: " + requestClientInfo);
process = Runtime.getRuntime().exec(args = new String[]{"ps"});
processOut = new BufferedReader(new InputStreamReader(process.getInputStream()));
requestResponse = "Current Processes: \n";
currentStreamline = "";
while ((currentStreamline = processOut.readLine()) != null) {
requestResponse += currentStreamline + "\n";
}
processOut.close();
break;
default:
//Invalid request
System.out.println("Invalid request received from: " + requestClientInfo);
requestResponse = "Invalid request sent.";
break;
}
//Send response
PrintWriter writer = new PrintWriter(currentRequest.getOutputStream(), true);
writer.println(requestResponse);
//Close request
currentRequest.close();
//Report response time
long requestEndTime = System.currentTimeMillis() - requestStartTime;
System.out.println("Response time: " + requestEndTime + " ms");
}
} catch (IOException e) {
System.out.println("Error occurred. Could not create socket.");
e.printStackTrace();
}
//Shutdown server
System.out.println("Shutting down server...");
userInput.close();
}
}