-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
425 lines (365 loc) · 13.8 KB
/
Copy pathserver.js
File metadata and controls
425 lines (365 loc) · 13.8 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
'use strict';
const http = require('http');
const path = require('path');
const fs = require('fs');
const express = require('express');
const { WebSocketServer } = require('ws');
const serverManager = require('./src/serverManager');
const processManager = require('./src/processManager');
const serverDetector = require('./src/serverDetector');
const serverCreator = require('./src/serverCreator');
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.json({ limit: '2mb' }));
app.use(express.static(path.join(__dirname, 'public')));
// ============================================================
// Notification broadcasting (notifications WebSocket)
// ============================================================
const notificationClients = new Set();
function broadcast(data) {
const msg = JSON.stringify(data);
for (const ws of notificationClients) {
if (ws.readyState === 1) {
try { ws.send(msg); } catch (_) {}
}
}
}
// Forward process status changes to browser
processManager.on('statusChange', (serverId, status) => {
broadcast({ type: 'server_status', serverId, status });
});
// ============================================================
// REST API
// ============================================================
// --- List servers ---
app.get('/api/servers', (req, res) => {
const servers = serverManager.getAll().map(s => ({
...s,
status: processManager.getStatus(s.id)
}));
res.json({ servers });
});
// --- Attach existing server ---
app.post('/api/servers', (req, res) => {
try {
const { name, serverPath } = req.body;
if (!name || !serverPath) return res.status(400).json({ error: 'name and serverPath are required' });
const detected = serverDetector.detect(serverPath);
if (!detected.valid) return res.status(400).json({ error: detected.error });
const server = serverManager.add({
name,
serverPath,
type: detected.type,
version: detected.version,
jarFile: detected.jarFile,
// Use explicitly supplied script, or fall back to auto-detected one
startupScript: req.body.startupScript !== undefined
? (req.body.startupScript || null)
: (detected.scriptFile || null),
minRam: req.body.minRam || 512,
maxRam: req.body.maxRam || 1024,
javaPath: req.body.javaPath || 'java',
javaArgs: req.body.javaArgs || ''
});
res.json({ server: { ...server, status: 'stopped' } });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- Get one server ---
app.get('/api/servers/:id', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
res.json({ server: { ...s, status: processManager.getStatus(s.id) } });
});
// --- Update server config ---
app.put('/api/servers/:id', (req, res) => {
try {
if (!serverManager.get(req.params.id)) return res.status(404).json({ error: 'Server not found' });
const updated = serverManager.update(req.params.id, req.body);
res.json({ server: updated });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- Remove server from manager (no file deletion) ---
app.delete('/api/servers/:id', (req, res) => {
if (!serverManager.get(req.params.id)) return res.status(404).json({ error: 'Server not found' });
const status = processManager.getStatus(req.params.id);
if (status !== 'stopped' && status !== 'crashed') {
return res.status(400).json({ error: 'Stop the server before removing it' });
}
serverManager.remove(req.params.id);
res.json({ success: true });
});
// --- Start ---
app.post('/api/servers/:id/start', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
const status = processManager.getStatus(req.params.id);
if (status !== 'stopped' && status !== 'crashed') {
return res.status(400).json({ error: `Server is already ${status}` });
}
try {
processManager.start(req.params.id, s);
res.json({ success: true, status: 'starting' });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- Graceful stop ---
app.post('/api/servers/:id/stop', (req, res) => {
if (!serverManager.get(req.params.id)) return res.status(404).json({ error: 'Server not found' });
const ok = processManager.stop(req.params.id);
if (!ok) return res.status(400).json({ error: 'Server is not running' });
res.json({ success: true });
});
// --- Force kill ---
app.post('/api/servers/:id/kill', (req, res) => {
if (!serverManager.get(req.params.id)) return res.status(404).json({ error: 'Server not found' });
const ok = processManager.kill(req.params.id);
if (!ok) return res.status(400).json({ error: 'Server is not running' });
res.json({ success: true });
});
// --- Restart ---
app.post('/api/servers/:id/restart', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
const status = processManager.getStatus(req.params.id);
if (status === 'stopped' || status === 'crashed') {
// Just start it
try {
processManager.start(req.params.id, s);
return res.json({ success: true });
} catch (err) {
return res.status(500).json({ error: err.message });
}
}
// Listen for the stop, then start again
const onStatusChange = (id, newStatus) => {
if (id !== req.params.id) return;
if (newStatus === 'stopped' || newStatus === 'crashed') {
processManager.removeListener('statusChange', onStatusChange);
setTimeout(() => {
try { processManager.start(req.params.id, s); } catch (_) {}
}, 1500);
}
};
processManager.on('statusChange', onStatusChange);
processManager.stop(req.params.id);
res.json({ success: true });
});
// --- Status ---
app.get('/api/servers/:id/status', (req, res) => {
if (!serverManager.get(req.params.id)) return res.status(404).json({ error: 'Server not found' });
res.json({ status: processManager.getStatus(req.params.id) });
});
// --- Read properties ---
app.get('/api/servers/:id/properties', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
try {
res.json({ properties: serverDetector.readProperties(s.serverPath) });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- Write properties ---
app.put('/api/servers/:id/properties', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
try {
serverDetector.writeProperties(s.serverPath, req.body.properties || {});
res.json({ success: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- File browser ---
app.get('/api/servers/:id/files', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
const subDir = req.query.dir || '';
const base = path.resolve(s.serverPath);
const target = path.resolve(base, subDir);
// Prevent path traversal outside server directory
if (!target.startsWith(base)) return res.status(403).json({ error: 'Access denied' });
try {
const entries = fs.readdirSync(target, { withFileTypes: true });
const files = entries.map(e => {
let size = null;
try {
if (!e.isDirectory()) size = fs.statSync(path.join(target, e.name)).size;
} catch (_) {}
return {
name: e.name,
isDirectory: e.isDirectory(),
relativePath: subDir ? path.join(subDir, e.name).replace(/\\/g, '/') : e.name,
size
};
}).sort((a, b) => {
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
return a.name.localeCompare(b.name);
});
res.json({ files, currentDir: subDir });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- File content viewer (read-only) ---
app.get('/api/servers/:id/files/content', (req, res) => {
const s = serverManager.get(req.params.id);
if (!s) return res.status(404).json({ error: 'Server not found' });
const filePath = req.query.path || '';
const base = path.resolve(s.serverPath);
const target = path.resolve(base, filePath);
// Prevent path traversal outside server directory
if (!target.startsWith(base)) return res.status(403).json({ error: 'Access denied' });
try {
const stat = fs.statSync(target);
if (stat.isDirectory()) return res.status(400).json({ error: 'Path is a directory' });
// Size limit: 512 KB
const MAX_SIZE = 512 * 1024;
if (stat.size > MAX_SIZE) {
return res.json({
content: null,
reason: `File too large to preview (${(stat.size / 1024).toFixed(0)} KB, limit is 512 KB)`
});
}
// Known binary extensions - skip reading entirely
const BINARY_EXT = new Set([
'jar', 'zip', 'gz', 'tar', '7z', 'rar', 'bz2', 'xz',
'class', 'bin', 'exe', 'dll', 'so', 'dylib',
'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'ico',
'mp4', 'mp3', 'ogg', 'wav', 'flac',
'pdf', 'mca', 'nbt', 'lck'
]);
const ext = path.extname(target).toLowerCase().slice(1);
if (BINARY_EXT.has(ext)) {
return res.json({ content: null, reason: 'Binary file - cannot display as text' });
}
// Read as buffer and check for null bytes (binary detection)
const buf = fs.readFileSync(target);
const checkLen = Math.min(buf.length, 8192);
for (let i = 0; i < checkLen; i++) {
if (buf[i] === 0) {
return res.json({ content: null, reason: 'Binary file - cannot display as text' });
}
}
res.json({ content: buf.toString('utf8'), size: stat.size });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- Detect server type without adding ---
app.post('/api/detect', (req, res) => {
const { serverPath } = req.body;
if (!serverPath) return res.status(400).json({ error: 'serverPath is required' });
const result = serverDetector.detect(serverPath);
res.json(result);
});
// --- List jar files in a directory ---
app.post('/api/list-jars', (req, res) => {
const { serverPath } = req.body;
if (!serverPath) return res.status(400).json({ error: 'serverPath is required' });
res.json({ jars: serverDetector.listJars(serverPath) });
});
// --- List startup scripts in a directory ---
app.post('/api/list-scripts', (req, res) => {
const { serverPath } = req.body;
if (!serverPath) return res.status(400).json({ error: 'serverPath is required' });
res.json({ scripts: serverDetector.listScripts(serverPath) });
});
// --- Minecraft version list ---
app.get('/api/minecraft/versions', async (req, res) => {
try {
const includeSnapshots = req.query.snapshots === 'true';
const versions = await serverCreator.getVersionList(includeSnapshots);
res.json({ versions });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// --- Create vanilla server (async, progress via notifications WS) ---
app.post('/api/minecraft/create', (req, res) => {
const { name, serverPath, version, minRam, maxRam, acceptEula } = req.body;
if (!name || !serverPath || !version) {
return res.status(400).json({ error: 'name, serverPath, and version are required' });
}
const tempId = `create_${Date.now()}`;
res.json({ success: true, tempId });
serverCreator.createServer({
serverPath,
version,
acceptEula: acceptEula !== false,
onProgress: (message, progress) => {
broadcast({ type: 'creation_progress', tempId, message, progress });
}
}).then((result) => {
if (result.success) {
const server = serverManager.add({
name, serverPath,
type: 'vanilla',
version,
jarFile: result.jarFile,
startupScript: null,
minRam: Number(minRam) || 512,
maxRam: Number(maxRam) || 1024,
javaPath: 'java',
javaArgs: ''
});
broadcast({ type: 'creation_done', tempId, server: { ...server, status: 'stopped' } });
}
}).catch((err) => {
broadcast({ type: 'creation_error', tempId, error: err.message });
});
});
// ============================================================
// HTTP Server + WebSocket setup
// ============================================================
const server = http.createServer(app);
const wss = new WebSocketServer({ noServer: true });
server.on('upgrade', (req, socket, head) => {
const url = new URL(req.url, `http://${req.headers.host}`);
const parts = url.pathname.split('/').filter(Boolean);
if (parts[0] !== 'ws') { socket.destroy(); return; }
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req, parts);
});
});
wss.on('connection', (ws, req, parts) => {
if (!parts) {
const url = new URL(req.url, `http://${req.headers.host}`);
parts = url.pathname.split('/').filter(Boolean);
}
const channel = parts[1];
if (channel === 'console') {
const serverId = parts[2];
if (!serverId) { ws.close(); return; }
processManager.subscribe(serverId, ws);
ws.on('message', (msg) => {
processManager.sendCommand(serverId, msg.toString().trim());
});
ws.on('close', () => {
processManager.unsubscribe(serverId, ws);
});
} else if (channel === 'notifications') {
notificationClients.add(ws);
ws.on('close', () => {
notificationClients.delete(ws);
});
} else {
ws.close();
}
});
server.listen(PORT, () => {
console.log('');
console.log(' Minecraft Server Manager');
console.log(` Running at http://localhost:${PORT}`);
console.log('');
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n[MSM] Shutting down...');
server.close(() => process.exit(0));
});