-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalServer.java
More file actions
355 lines (322 loc) · 12.7 KB
/
TerminalServer.java
File metadata and controls
355 lines (322 loc) · 12.7 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.io.RandomAccessFile;
public class TerminalServer {
private static final List<ClientInfo> clients = Collections.synchronizedList(new ArrayList<ClientInfo>());
private static final String CONFIG_FILE = "server_config.properties";
private static Properties config = new Properties();
private static ServerWindow serverWindow;
private static final String LOCK_FILE = "server.lock";
private static FileLock lock;
private static FileChannel lockChannel;
private static RandomAccessFile lockFileStream;
public static void main(String[] args) {
if (!acquireLock()) {
String message = "Another instance of TerminalServer may already be running.";
System.err.println(message);
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
releaseLock();
System.out.println("Server shut down.");
}));
// Initialize and show the server window
SwingUtilities.invokeLater(() -> {
serverWindow = new ServerWindow();
serverWindow.setVisible(true);
log("INFO", "Server starting...");
});
ServerSocket serverSocket = null;
try {
while (true) {
loadConfig(); // Reload config before each connection
int port = getConfigInt("port", 8080);
if (serverSocket == null || serverSocket.isClosed() || serverSocket.getLocalPort() != port) {
if (serverSocket != null && !serverSocket.isClosed()) {
serverSocket.close();
}
serverSocket = new ServerSocket(port);
log("INFO", "Server is listening on port " + port);
}
Socket socket = serverSocket.accept();
log("INFO", "New connection from: " + socket.getInetAddress().getHostAddress());
new ServerThread(socket).start();
}
} catch (IOException e) {
log("ERROR", "Server error: " + e.getMessage());
e.printStackTrace();
} finally {
if (serverSocket != null && !serverSocket.isClosed()) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static boolean acquireLock() {
try {
File file = new File(LOCK_FILE);
lockFileStream = new RandomAccessFile(file, "rw");
lockChannel = lockFileStream.getChannel();
lock = lockChannel.tryLock();
if (lock == null) {
// Lock is held by another process
lockChannel.close();
lockFileStream.close();
return false;
}
// Lock acquired
return true;
} catch (OverlappingFileLockException e) {
return false;
} catch (IOException e) {
System.err.println("Could not acquire lock on " + LOCK_FILE + ": " + e.getMessage());
e.printStackTrace();
return false;
}
}
private static void releaseLock() {
try {
if (lock != null) {
lock.release();
}
if (lockChannel != null) {
lockChannel.close();
}
if (lockFileStream != null) {
lockFileStream.close();
}
new File(LOCK_FILE).delete();
} catch (IOException e) {
System.err.println("Failed to release file lock: " + e.getMessage());
e.printStackTrace();
}
}
private static class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket) {
this.socket = socket;
}
public void run() {
BufferedReader reader = null;
PrintWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
String clientName = reader.readLine();
String clientIp = socket.getInetAddress().getHostAddress();
ClientInfo clientInfo = new ClientInfo(clientName, clientIp);
synchronized (clients) {
clients.add(clientInfo);
// Update GUI
if (serverWindow != null) {
serverWindow.updateConnectionCount(clients.size());
serverWindow.addUser(clientName, clientIp);
}
}
log("INFO", "Client connected: " + clientName);
String command;
while ((command = reader.readLine()) != null) {
log("INFO", "Received from " + clientName + ": " + command);
clientInfo.addCommand(command);
if ("exit".equalsIgnoreCase(command.trim())) {
log("INFO", "Exit command received from " + clientName);
writer.println("Goodbye!");
break; // Exit loop to stop reading further commands
}
try {
if (command.startsWith("-i ")) {
handleInfoCommand(command.substring(3).trim(), writer);
} else if ("-h".equals(command)) {
handleHelpCommand(writer);
} else {
writer.println(executeCommand(command));
}
} catch (Exception e) {
writer.println("Error processing command: " + e.getMessage());
}
}
} catch (IOException e) {
log("ERROR", "Error: " + e.getMessage());
} finally {
cleanup();
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log("ERROR", "Error closing reader: " + e.getMessage());
}
}
if (writer != null) {
writer.close();
}
}
}
private void handleInfoCommand(String targetName, PrintWriter writer) {
synchronized (clients) {
boolean clientFound = false;
for (ClientInfo ci : clients) {
if (ci.name.equals(targetName)) {
writer.println(ci);
clientFound = true;
break;
}
}
if (!clientFound) {
writer.println("No client found with name: " + targetName);
}
}
writer.println("END_OF_INFO");
}
private void handleHelpCommand(PrintWriter writer) {
StringBuilder clientNames = new StringBuilder("Client names: ");
synchronized (clients) {
for (ClientInfo ci : clients) {
clientNames.append(ci.name).append(", ");
}
}
writer.println(clientNames.substring(0, clientNames.length() - 2));
}
private String executeCommand(String command) throws IOException {
ProcessBuilder pb;
if (System.getProperty("os.name").toLowerCase().contains("win")) {
if (command.endsWith(".sh")) {
return "Shell scripts (.sh) are not supported on Windows.";
}
pb = new ProcessBuilder("cmd", "/c", command);
} else {
if (new File(command).exists() && command.endsWith(".sh")) {
if (new File("/usr/bin/gnome-terminal").exists()) {
pb = new ProcessBuilder("gnome-terminal", "--", "/bin/bash", command);
} else {
return "gnome-terminal not found.";
}
} else {
pb = new ProcessBuilder("/bin/sh", "-c", command);
}
}
pb.redirectErrorStream(true);
Process process = pb.start();
return readProcessOutput(process);
}
private String readProcessOutput(Process process) throws IOException {
StringBuilder output = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
} finally {
if (reader != null) {
reader.close();
}
}
return output.toString();
}
private void cleanup() {
synchronized (clients) {
Iterator<ClientInfo> iterator = clients.iterator();
while (iterator.hasNext()) {
ClientInfo ci = iterator.next();
if (ci.ip.equals(socket.getInetAddress().getHostAddress())) {
iterator.remove();
log("INFO", "Client disconnected: " + ci.name);
// Update GUI
if (serverWindow != null) {
serverWindow.updateConnectionCount(clients.size());
serverWindow.removeUser(ci.name, ci.ip);
}
break;
}
}
}
try {
socket.close();
} catch (IOException e) {
log("ERROR", "Error closing socket: " + e.getMessage());
}
}
}
private static class ClientInfo implements Serializable {
String name;
String ip;
List<String> commands = new ArrayList<String>();
public ClientInfo(String name, String ip) {
this.name = name;
this.ip = ip;
}
public void addCommand(String command) {
commands.add(command);
}
@Override
public String toString() {
return "Client[name=" + name + ", ip=" + ip + ", commands=" + commands + "]";
}
}
private static void loadConfig() {
File configFile = new File(CONFIG_FILE);
if (!configFile.exists()) {
createDefaultConfig();
}
FileInputStream in = null;
try {
in = new FileInputStream(configFile);
config.clear(); // Clear existing properties
config.load(in);
} catch (IOException e) {
log("ERROR", "Error loading config file: " + e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
log("ERROR", "Error closing config file: " + e.getMessage());
}
}
}
}
private static void createDefaultConfig() {
config.setProperty("port", "8080");
saveConfig();
}
private static void saveConfig() {
FileOutputStream out = null;
try {
out = new FileOutputStream(CONFIG_FILE);
config.store(out, "Server Configuration");
} catch (IOException e) {
log("ERROR", "Error saving config file: " + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log("ERROR", "Error closing config file: " + e.getMessage());
}
}
}
}
private static String getConfigString(String key, String defaultValue) {
return config.getProperty(key, defaultValue);
}
private static int getConfigInt(String key, int defaultValue) {
return Integer.parseInt(config.getProperty(key, String.valueOf(defaultValue)));
}
// Add this helper method for logging
private static void log(String level, String message) {
System.out.println(message);
if (serverWindow != null) {
serverWindow.log(level, message);
}
}
}