-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.js
531 lines (486 loc) · 19.1 KB
/
main.js
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
const fs = require('fs-extra');
const assert = require("assert");
const {
Readable,
Writable
} = require("stream");
const readline = require("readline");
let {
spawn
} = require("child_process");
const pidusage = require("pidusage");
const express = require("express");
const {
CONFIG_FILE_PATH,
UNZIPPED_SERVERS_CONTAINER,
UNZIPPED_SERVER_FOLDER_PATH,
BACKUP_TYPES,
MS_IN_MIN,
MS_IN_SEC,
BUCKET_LOCK_FILE_NAME,
formatBytes,
platform
} = require("./utils.js");
if (platform === "win32") {
spawn = require("cross-spawn");
}
const {
downloadServerIfNotExists
} = require("./download-server.js");
const {
createServerProperties
} = require("./create-server-properties.js");
const {
createBackupBucketIfNotExists,
downloadRemoteBackups,
createBackup,
restoreLocalBackup,
restoreLatestLocalBackup,
createUnscheduledBackup,
getBackupList,
getBackupSizeList,
doesLockFileExistOrS3Disabled,
createLockFileIfS3Enabled,
deleteLockFileIfExists,
} = require("./backup.js");
const crypto = require("crypto");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const configFile = fs.readFileSync(CONFIG_FILE_PATH, "utf8");
fs.ensureDirSync(UNZIPPED_SERVERS_CONTAINER);
const config = JSON.parse(configFile);
assert(
config["accept-official-minecraft-server-eula"],
"You must accept the minecraft EULA on https://www.minecraft.net/en-us/download/server/bedrock/ by setting the flag in the config file to true in order to use this software"
);
const backupConfig = config.backup;
assert(backupConfig, `Could not find field 'backup' at root of config`);
const backupFrequencyMS = backupConfig["backup-frequency-minutes"] * MS_IN_MIN;
const minBackupFrequencyMinutes = 10;
assert(
backupFrequencyMS > MS_IN_MIN * minBackupFrequencyMinutes,
`Expected backup['backup-frequency-min'] to be greater than ${minBackupFrequencyMinutes}`
);
let isCurrentlyBackingUp = false;
let hasSentStopCommand = false;
const SAVE_QUERY_FREQUENCY = MS_IN_SEC * 5;
const UI_COMMAND_DELAY = MS_IN_SEC * 1;
let currentBackupType = null;
function sec2time(timeInSeconds) {
var pad = function(num, size) {
return ("000" + num).slice(size * -1);
},
time = parseFloat(timeInSeconds).toFixed(3),
hours = Math.floor(time / 3600),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60),
milliseconds = time.slice(-3);
return (
pad(hours, 2) +
":" +
pad(minutes, 2) +
":" +
pad(seconds, 2) +
"." +
pad(milliseconds, 3)
);
}
const MAX_STORED_LINES = 20;
const consoleLogBuffer = [];
const originalConsoleLog = console.log;
console.log = text => {
originalConsoleLog(text);
consoleLogBuffer.push((text || "").toString().replace(/\n/, "<br>"));
if (consoleLogBuffer.length > MAX_STORED_LINES) {
consoleLogBuffer.shift(); // delete first item.
}
};
let salt = crypto
.randomBytes(32)
.toString("hex")
.toUpperCase();
const uiConfig = config.ui;
if ((uiConfig || {}).enabled) {
console.log("Starting express server");
function clientHashIsValid(clientHashWithSalt) {
return (
(clientHashWithSalt || "").toUpperCase() ===
crypto
.createHash("sha256")
.update(
uiConfig["admin-code-sha256-hash"].toUpperCase() +
salt.toUpperCase()
)
.digest("hex")
.toUpperCase()
);
}
const expressApp = express();
const router = express.Router();
expressApp.use(express.json());
router.get("/terminal-out", (req, res) => {
res.send(
[`${MAX_STORED_LINES} latest lines of terminal output:`]
.concat(consoleLogBuffer)
.join("<br>")
);
});
router.get("/resource-usage", (req, res) => {
if (bs != null) {
pidusage(bs.pid, (err, stats) => {
let result = {};
if (stats != null) {
result = {
cpu: stats.cpu,
elapsed: stats.elapsed,
memory: stats.memory
};
}
res.send(result);
});
} else {
res.send({});
}
});
router.get("/salt", (req, res) => {
// refresh salt anytime anyone asks for it
salt = crypto
.randomBytes(32)
.toString("hex")
.toUpperCase();
res.send(salt);
});
router.get("/is-auth-valid", (req, res) => {
res.send(clientHashIsValid(req.header("Authorization")));
});
router.post("/stop", (req, res) => {
const {
body
} = req;
const {
adminCodeHash
} = {
body
};
setTimeout(() => {
if (clientHashIsValid(req.header("Authorization"))) {
rl.write("stop\n");
res.sendStatus(200);
} else {
console.log("Rejected unauthorized request to stop server");
res.sendStatus(401);
}
}, UI_COMMAND_DELAY);
});
router.post("/trigger-manual-backup", (req, res) => {
const {
body
} = req;
const {
adminCodeHash
} = {
body
};
setTimeout(() => {
if (clientHashIsValid(req.header("Authorization"))) {
rl.write("backup\n");
res.sendStatus(200);
} else {
console.log(
"Rejected unauthorized request to trigger manual backup"
);
res.sendStatus(401);
}
}, UI_COMMAND_DELAY);
});
router.post("/trigger-print-resource-usage", (req, res) => {
const {
body
} = req;
setTimeout(() => {
if (clientHashIsValid(req.header("Authorization"))) {
rl.write("resource-usage\n");
res.sendStatus(200);
} else {
console.log(
"Rejected unauthorized request to print resource usage"
);
res.sendStatus(401);
}
}, UI_COMMAND_DELAY);
});
router.post("/trigger-print-player-list", (req, res) => {
const {
body
} = req;
setTimeout(() => {
if (clientHashIsValid(req.header("Authorization"))) {
rl.write("list\n");
res.sendStatus(200);
} else {
console.log(
"Rejected unauthorized request to print resource usage"
);
res.sendStatus(401);
}
}, UI_COMMAND_DELAY);
});
router.post("/trigger-restore-backup", async (req, res) => {
const {
body
} = req;
const {
adminCodeHash
} = {
body
};
setTimeout(async () => {
if (clientHashIsValid(req.header("Authorization"))) {
const backup = body.backup;
const backups = await getBackupList();
if (backups.includes(backup)) {
// do this check to avoid weird injection errors
rl.write(`force-restore ${body.backup}\n`);
} else {
console.log(`Backup ${body.backup} not found`);
}
res.sendStatus(200);
} else {
console.log(
`Rejected unauthorized request to restore backup ${body.backup}`
);
res.sendStatus(401);
}
}, UI_COMMAND_DELAY);
});
router.get("/backup-size-list", async (req, res) => {
const backups = await getBackupSizeList();
res.send(backups);
});
expressApp.use("/", router);
expressApp.use(express.static("static"));
expressApp.listen(uiConfig.port);
console.log(`Running UI for server on port ${uiConfig.port}\n`);
} else {
console.log("Running server without UI\n");
}
let bs = null;
downloadServerIfNotExists(platform)
.then(() => {
createServerProperties().then(async () => {
await createBackupBucketIfNotExists();
const doesLockFileExist = await doesLockFileExistOrS3Disabled();
if (doesLockFileExist) {
console.error(`\nExiting server; a lock file, ${BUCKET_LOCK_FILE_NAME} exists within the S3 backup bucket.`
+ ' This usually means another server is connected to the backup bucket.'
+ ' This can also happen if the server did not exit gracefully last time.'
+ ' If that is the case, make sure everything is okay, manually delete the lock file from S3, then try starting the server again.')
process.exit(0);
}
await createLockFileIfS3Enabled();
await downloadRemoteBackups();
await restoreLatestLocalBackup();
console.log("\nStarting Minecraft Bedrock server...\n");
console.log(
`!!!!!!!!!!\nWARNING: Use the 'stop' command to stop the server gracefully, or you may lose non backed up up data\n!!!!!!!!!!\n`
);
const spawnServer = () => {
bs = spawn("./bedrock_server", [], {
stdio: ["pipe", "pipe", "pipe", "ipc"],
cwd: UNZIPPED_SERVER_FOLDER_PATH
});
bs.stderr.on("data", error => {
console.error(`SERVER STDERROR: ${error}`);
});
bs.on("error", data => {
console.log(`SERVER ERROR: ${data}`);
});
bs.on("close", code => {
console.log(
`Minecraft server child process exited with code ${code}`
);
});
async function handleServerOutputData(data) {
if (
/^.*] (A previous save has not been completed\.|Saving\.\.\.|Changes to the level are resumed\.)/i.test(
data
)
) {
// do nothing
} else if (
/^.*] (Data saved\. Files are now ready to be copied\.)/i.test(
data
)
) {
isCurrentlyBackingUp = true;
const backupStartTime = Math.floor(new Date() / 1000);
const backupType = currentBackupType;
console.log(
`Files ready for backup! Creating backup of server state at ${new Date(
backupStartTime * MS_IN_SEC
).toLocaleString()} with type ${backupType}...`
);
const dataSplit = data
.toString()
.split(
"Data saved. Files are now ready to be copied."
);
backupFileListString = dataSplit[
dataSplit.length - 1
].replace(/(\n|\r|\\n|\\r)/g, "");
await createBackup(
backupFileListString,
backupStartTime,
backupType
);
isCurrentlyBackingUp = false;
bs.stdin.write("save resume\r\n");
// stop here, since the backup before stop has completed;
if (hasSentStopCommand) {
clearInterval(saveQueryInterval);
clearInterval(saveHoldInterval);
bs.stdin.write("stop\r\n");
setTimeout(async () => {
await deleteLockFileIfExists();
process.exit(0);
}, MS_IN_SEC);
}
} else {
console.log(`[Raw Bedrock Server Output]${data.toString().replace(/$/, "")}`);
}
}
// Microsoft made an annoying change and now listening for the data is super inconsistent. To deal with this we need to reassemble the data into a buffer.
let lineBuffer = "";
bs.stdout.on("data", async function(data) {
lineBuffer += data.toString();
const shouldIngest2Lines = /^.*] (Data saved\. Files are now ready to be copied\.)/i.test(
lineBuffer
); // Microsoft made another annoying change and split this info into 2 lines - to deal with this, wait for 2 lines if we hit the 'Data saved...' message and reassemble
let lines = lineBuffer.split("\n");
if (!shouldIngest2Lines) {
if (lines.length > 1) {
await handleServerOutputData(lines[0]); // standard case
lineBuffer = lines[1];
}
} else {
if (lines.length > 2) {
await handleServerOutputData(lines[0] + lines[1]);
lineBuffer = lines[2];
}
}
});
};
if (platform === "linux" || platform === 'win32') {
spawnServer();
} else {
throw 'Unsupported platform - must be Windows 10 or Ubuntu 18+ based';
}
let lastQueryWasSaveSucccessful = false;
const saveQueryInterval = setInterval(() => {
if (!isCurrentlyBackingUp && bs) {
bs.stdin.write("save query\r\n");
}
}, SAVE_QUERY_FREQUENCY);
const triggerBackup = backupType => {
if (!hasSentStopCommand && !isCurrentlyBackingUp) {
// don't backup if hasSentStopCommand is true
console.log(`\nTelling server to prepare for backup...`);
currentBackupType = backupType;
bs.stdin.write("save hold\r\n");
}
};
const saveHoldInterval = setInterval(
triggerBackup,
backupFrequencyMS,
BACKUP_TYPES.SCHEDULED
);
const printResourceUsage = () => {
if (bs != null) {
pidusage(bs.pid, function(err, stats) {
console.log(
`Resource Usage as of ${new Date().toLocaleString()}:`
);
console.log(
`CPU Percentage (from 0 to 100*vcore): ${stats.cpu.toFixed(
3
)}%`
);
console.log(`RAM: ${formatBytes(stats.memory)}`);
console.log(
`Wrapped Server Uptime : ${sec2time(
Math.round(stats.elapsed / 1000)
)} (hh:mm:ss)`
);
});
}
};
const triggerGracefulExit = () => {
console.log("\nBacking up, then killing Minecraft server...");
hasSentStopCommand = true;
currentBackupType = BACKUP_TYPES.ON_STOP;
bs.stdin.write("save hold\r\n");
};
process.on("SIGINT", async () => {
bs = null;
await createUnscheduledBackup(Math.floor(new Date() / 1000));
process.exit(1);
});
rl.on("line", async line => {
if (/^(\r|\n|)$/i.test(line)) {
// ignore blank commands
} else if (/^(stop|exit)$/i.test(line)) {
triggerGracefulExit();
} else if (/^(save.*)/i.test(line)) {
// intercept saves
console.log(
`Please use the 'backup' command to create a manual backup`
);
} else if (/^(backup)/i.test(line)) {
triggerBackup(BACKUP_TYPES.MANUAL);
} else if (/^(resource-usage)$/i.test(line)) {
printResourceUsage();
} else if (/^(list)$/i.test(line)) {
bs.stdin.write(`list\r\n`);
} else if (/^(force-restore)/i.test(line)) {
const lineSplit = line.split("force-restore ");
if (lineSplit.length > 0) {
console.log(
"\n!!!!!!!!!!\nForcefully killing server and overwriting world state with specified backup - current world state will be lost"
);
bs.stdin.write("stop\r\n");
bs = null;
setTimeout(async () => {
await createUnscheduledBackup(
Math.floor(new Date() / 1000)
);
const didSuccessfulyRestore = await restoreLocalBackup(
lineSplit[1]
);
if (!didSuccessfulyRestore) {
console.log(
"Unable to restore backup - restarting server as is"
);
}
spawnServer();
}, 2 * MS_IN_SEC);
} else {
console.error("USAGE: restore <BACKUP_FILE_NAME>");
}
} else {
console.log(
"Recognized commands: backup, force-restore <BACKUP_FILE_NAME>, resource-usage, stop"
);
console.log(
"Piping the command directly to the underlying base Minecraft server since this command was not recognized by the node wrapper\n"
);
bs.stdin.write(`${line}\r\n`);
}
});
});
})
.catch(error => {
console.error(error);
});