forked from urixen-org/Rexon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
403 lines (352 loc) · 10.9 KB
/
Copy pathindex.js
File metadata and controls
403 lines (352 loc) · 10.9 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
import express from "express";
import { createServer } from "node:http";
import { Server } from "socket.io";
import { spawn } from "node-pty"; // FIXED: Proper import for node-pty
import os from "os";
import configRoute from "./controller/settings.js";
import config from "./config.json" assert { type: "json" };
import filesRoute from "./controller/files.js";
import { initFileWatcher } from "./utils/fileWatcher.js";
import expressFileUpload from "express-fileupload";
import pluginRoute from "./controller/plugin.js";
import worldRoute from "./controller/world.js";
import versionRoute from "./controller/version.js";
import systemUsage from "./utils/system-usage.js";
import { PlayItService } from "./utils/PlayitServiceProvider.js";
import MinecraftProperties from "./utils/mcPropertise.js";
import fs from "node:fs";
import { router as ngrokRouter } from "./controller/ngrok.js";
import path from "node:path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
if (!fs.existsSync("./server")) {
fs.mkdir("./server");
}
const app = express();
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: false,
},
pingTimeout: 60000,
pingInterval: 25000,
});
app.use(express.json());
app.use(expressFileUpload());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("./app"));
app.use((req, res, next) => {
console.log(`${req.method} ${req.url} - ${req.ip}`);
next();
});
app.use("/api", filesRoute);
app.use("/api", pluginRoute);
app.use("/api", worldRoute);
app.use("/api", versionRoute);
app.use("/api", ngrokRouter);
initFileWatcher(io);
const playItService = new PlayItService({
minecraftPort:
MinecraftProperties.parse(
fs.readFileSync("./server/server.properties", "utf-8")
)["server-port"] || 25565,
autoRestart: true,
maxRestartAttempts: 3,
useSystemd: config.useSystemd,
sudoPassword: config.sudoPassword || null, // Be careful with storing sudo password in env
});
// Handle PlayIt events
playItService.on("log", (log) => {
// You could store logs or broadcast to connected clients
io.emit("playit:log", { log });
console.log(`[${log.timestamp}] [${log.type}] ${log.message}`);
});
playItService.on("status_change", ({ status }) => {
io.emit("playit:status", { status });
console.log(`PlayIt status changed to: ${status}`);
});
playItService.on("tunnel_created", ({ url }) => {
io.emit("playit:tunnel", { url });
console.log(`🎮 Minecraft server accessible at: ${url}`);
});
playItService.on("auth_required", ({ url }) => {
io.emit("playit:auth_required", { url });
console.log(`⚠️ PlayIt authentication required: ${url}`);
// You could send notification email/SMS here
});
// API endpoints for controlling PlayIt
app.post("/api/playit/start", async (req, res) => {
const result = await playItService.start();
res.json(result);
});
app.post("/api/playit/stop", async (req, res) => {
const result = await playItService.stop();
res.json(result);
});
app.post("/api/playit/restart", async (req, res) => {
await playItService.stop();
const result = await playItService.start();
res.json(result);
});
app.get("/api/playit/status", (req, res) => {
res.json({
status: playItService.tunnelStatus,
tunnelUrl: playItService.tunnelUrl,
uptime: playItService.startTime
? Math.floor((Date.now() - playItService.startTime) / 1000)
: 0,
logs: playItService.logs.slice(-20), // Return last 20 logs
});
});
app.get("/api/playit/check-updates", async (req, res) => {
const updates = await playItService.checkForUpdates();
res.json(updates);
});
// Update PlayIt if requested
app.post("/api/playit/update", async (req, res) => {
// Stop service first
await playItService.stop();
// Force update of binary
const downloadResult = await playItService.ensurePlayItExists(true);
// Restart if it was running
let startResult = { success: false, message: "Update only, not restarting" };
if (req.body.restart) {
startResult = await playItService.start();
}
res.json({
updateSuccess: downloadResult,
restartRequested: !!req.body.restart,
restartResult: startResult,
});
});
let commandHistory = [];
const MAX_HISTORY_LENGTH = 1000;
const serverFolder = "./server/";
const startUpCmd = config.startup;
let mcProcess = null;
const isServerOnline = () => mcProcess !== null;
// History utils
function addToHistory(command, output) {
const entry = { command, output }; // Make sure command is stored
commandHistory.unshift(entry);
if (commandHistory.length > MAX_HISTORY_LENGTH) {
commandHistory.pop();
}
}
function formatHistoryForTerminal() {
if (commandHistory.length === 0) return "";
return commandHistory.map((e) => `\r ${e.output}`).join("");
}
// Start Minecraft server
function startServer() {
if (mcProcess) return false;
const shell = os.platform() === "win32" ? "powershell.exe" : "bash";
console.log("Starting server with command:", startUpCmd);
mcProcess = spawn(shell, ["-c", startUpCmd], {
name: "xterm-color",
cols: 80,
rows: 30,
cwd: serverFolder,
env: process.env,
});
mcProcess.onData((data) => {
io.emit("server:output", data);
addToHistory("", data);
});
mcProcess.onExit(() => {
mcProcess = null;
io.emit("server:status", false);
io.emit("server:output", "\r\n\x1b[31m[Server stopped]\x1b[0m\r\n");
});
mcProcess.on("error", (err) => {
console.error("Minecraft server failed to start:", err);
});
return true;
}
// Execute command in server
function executeCommand(cmd) {
if (mcProcess) {
mcProcess.write(cmd + "\r");
return `\r\n\x1b[97m[\x1b[36mCommand\x1b[97m]\x1b[0m: Sent \x1b[33m${cmd}\x1b[0m to server\r\n`;
} else {
return "\r\n\x1b[97m[\x1b[31mError\x1b[97m]\x1b[0m: Server is offline\r\n";
}
}
// Routes
app.get("/api/health", async (req, res) => {
systemUsage()
.then((data) => {
res.json({
status: "ok",
uptime: process.uptime(),
connections: io.engine.clientsCount,
serverStatus: isServerOnline() ? "online" : "offline",
memoryUsage: process.memoryUsage(),
systemUsage: data,
});
})
.catch((err) => {
console.log(err);
res.json({ error: true, message: err });
});
});
app.use(configRoute);
app.get("*view", (req, res) => {
res.sendFile(path.resolve(__dirname, "app/index.html"));
});
// Socket.IO
io.on("connection", (socket) => {
console.log("Client connected: " + socket.id);
socket.emit("server:status", isServerOnline());
socket.emit("server:history", commandHistory);
// Send initial health data
systemUsage()
.then((data) => {
socket.emit("server:health", {
status: "ok",
uptime: process.uptime(),
connections: io.engine.clientsCount,
serverStatus: isServerOnline() ? "online" : "offline",
memoryUsage: process.memoryUsage(),
systemUsage: data,
});
})
.catch((err) => {
console.log("Error getting system usage:", err);
});
// Set up health update interval - 1000ms (1 second) is a more reasonable interval
const healthInterval = setInterval(() => {
systemUsage()
.then((data) => {
socket.emit("server:health", {
status: "ok",
uptime: process.uptime(),
connections: io.engine.clientsCount,
serverStatus: isServerOnline() ? "online" : "offline",
memoryUsage: process.memoryUsage(),
systemUsage: data,
});
})
.catch((err) => {
console.log("Error getting system usage:", err);
});
// send server status
socket.emit("server:status", isServerOnline());
}, 1000); // Update every second instead of every 10ms
// Add event handler for client requesting health data
socket.on("client:requestHealth", () => {
systemUsage()
.then((data) => {
socket.emit("server:health", {
status: "ok",
uptime: process.uptime(),
connections: io.engine.clientsCount,
serverStatus: isServerOnline() ? "online" : "offline",
memoryUsage: process.memoryUsage(),
systemUsage: data,
});
})
.catch((err) => {
console.log("Error getting system usage:", err);
});
});
socket.on("server:execute", (data) => {
if (!data?.cmd) {
socket.emit(
"server:output",
"\r\n\x1b[97m[\x1b[31mError\x1b[97m]\x1b[0m: Invalid command format\r\n\x1b[36m> \x1b[0m"
);
return;
}
const output = executeCommand(data.cmd);
socket.emit("server:output", output);
});
socket.on("server:action", async ({ action }) => {
let msg = "";
switch (action) {
case "start":
if (!isServerOnline()) {
startServer();
msg = "Server is starting...";
} else {
msg = "Server is already running.";
}
break;
case "stop":
if (mcProcess) {
mcProcess.write("stop\r");
msg = "Sent 'stop' command to server.";
} else {
msg = "Server is already offline.";
}
break;
case "kill":
if (mcProcess) {
mcProcess.kill();
mcProcess = null;
io.emit("server:status", false);
msg = "Server process killed.";
} else {
msg = "Server is not running.";
}
break;
case "restart":
if (mcProcess) {
mcProcess.write("stop\r");
msg = "Restarting server...";
setTimeout(() => {
if (!mcProcess) startServer();
}, 8000);
} else {
msg = "Server is offline. Starting...";
startServer();
}
break;
default:
msg = "Unknown action.";
break;
}
socket.emit("server:output", `\r\n${msg}\r\n\x1b[36m> \x1b[0m`);
socket.emit("server:status", isServerOnline());
});
socket.on("disconnect", (reason) => {
console.log(`Client disconnected (${socket.id}): ${reason}`);
});
socket.on("error", (err) => {
console.error(`Socket error from ${socket.id}:`, err);
});
});
// Start HTTP server
const PORT = process.env.PORT || 3000;
server.listen(PORT, "0.0.0.0", () => {
console.log(`Minecraft Manager running at http://0.0.0.0:${PORT}`);
console.log(`Socket.IO server with CORS enabled`);
});
// Graceful shutdown
process.on("SIGINT", async () => {
console.log("Shutting down...");
if (playItService.tunnelStatus !== "stopped") {
console.log("Stopping PlayIt service...");
try {
await playItService.stop();
} catch (err) {
console.error("Error stopping PlayIt:", err);
}
}
if (mcProcess) {
console.log("Stopping Minecraft server...");
mcProcess.write("stop\r");
setTimeout(() => {
if (mcProcess) {
console.log("Force killing server...");
mcProcess.kill();
}
process.exit(0);
}, 10000);
} else {
process.exit(0);
}
});