This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
142 lines (117 loc) · 5.22 KB
/
app.ts
File metadata and controls
142 lines (117 loc) · 5.22 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
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const tail = require('tail').Tail;
const fs = require("fs");
const { exec } = require("child_process");
// set active watcher to default false
// TODO: remove this!!
let activeWatcher = false;
// Create server folder if doesn't exists
// For easier handeling in the script the log file will be created before the server
if (!fs.existsSync(__dirname + "/server")) {
fs.mkdir(__dirname + "/server", { recursive: true }, (err: any) => {
if (err) throw err;
});
}
if (!fs.existsSync(__dirname + "/server/logs")) {
fs.mkdir(__dirname + "/server/logs", { recursive: true }, (err: any) => {
if (err) throw err;
fs.writeFileSync(__dirname + "/server/logs/latest.log", "");
});
}
// setting listener for port 3000
http.listen(3000, () => {
console.log("Server running on: localhost:3000");
});
// executing given command as shell command
//TODO: command check to filter harmfull attacks
function executeCommand(socket: { emit: (arg0: string, arg1: string) => void; }, cmd: String) {
exec(cmd, (error: { message: any; }, stdout: any, stderr: any) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
});
}
// #####################################################################
// ## Routes section ##
// #####################################################################
app.get("/", (req: any, res: any) => {
res.sendFile(__dirname + "/assets/website/index.html");
});
app.get("/console", (req: any, res: any) => {
res.sendFile(__dirname + "/assets/website/console.html");
});
app.get("/styles", (req: any, res: any) => {
const style = req.query.style;
res.sendFile(__dirname + "/assets/website/assets/style/" + style + ".css");
});
app.get("/scripts", (req: any, res: any) => {
const script = req.query.script;
res.sendFile(__dirname + "/assets/website/assets/scripts/" + script + ".js");
});
// #####################################################################
// ## Socket Section ##
// #####################################################################
// listening for connection
// if connection, server listens for changes in the latest.log file
// and returns the newest change via socket.emit
io.on('connection', (socket: any) => {
var xtail = new tail(__dirname + "/server/logs/latest.log");
xtail.watch()
activeWatcher = true;
xtail.on("line", (data: any) => {
socket.emit("joined", data);
});
})
// listening for connection
// if connection, server is listening for incomming commands
io.on('connection', (socket: any) => {
// command send by commandfield on the website
// server is fusing the command form the received message and a template
socket.on('command', (message:any) => {
executeCommand(socket, 'docker exec -t mctest screen -S Minecraft-Server -p 0 -X stuff "' + message +'^M"');
socket.emit("action_result", `Remote command executed: ${message}`);
});
// action command received
// server is executing the command from a commandset
socket.on('action', (message:any) => {
// action server start
// this action is also creating a new server
// if the plugin folder doesn't exists
if (message == "start") {
if (fs.existsSync(__dirname + "/server/plugins")) {
executeCommand(socket, 'docker start mctest');
socket.emit("action_result", "Starting Server");
} else {
executeCommand(socket, 'docker run -tid --name mctest --volume=' + __dirname + "/server/:/server -p 25565:25565 -e RAM=4G zombymediaic/papermc:1.16.5-java16");
socket.emit("action_result", "Creating Server please wait...");
}
}
// action server restart
// this action is restarting the server and saving all progress
// docker container is restarted as well
if (message == "restart") {
executeCommand(socket, 'docker exec -t mctest screen -S Minecraft-Server -p 0 -X stuff "save-all^M"');
executeCommand(socket, 'docker exec -t mctest screen -S Minecraft-Server -p 0 -X stuff "stop^M"');
// delaying the docker restart command to avoid problems with the saving of chunks
// will be changed in the future to calculate the delay from the amount of plugins
setTimeout(() => {
executeCommand(socket, 'docker restart mctest');
socket.emit("action_result", "Restarting Server");
}, 4000);
}
// action server stop
// this action is stopping the server and saving all progress
if (message == "stop") {
executeCommand(socket, 'docker exec -t mctest screen -S Minecraft-Server -p 0 -X stuff "save-all^M"');
executeCommand(socket, 'docker exec -t mctest screen -S Minecraft-Server -p 0 -X stuff "stop^M"');
socket.emit("action_result", "Stopping Server");
}
});
})