-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBDSWrapper.js
96 lines (73 loc) · 2.74 KB
/
BDSWrapper.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
const express = require('express');
const { spawn } = require('child_process');
const path = require('path');
const colors = require('colors');
const serverLocation = 'C:/PATH_TO_BEDROCK_SERVER_DIRECTORY'; // Directory where bedrock_server.exe lives
const httpPort = 8080; // Port for HTTP requests
const authCode = 'your_auth_key'; // Authentication for HTTP requests
let serverProcess;
const app = express();
app.use(express.text());
const startServer = () => {
const serverExecutable = path.join(serverLocation, 'bedrock_server.exe');
if (!serverExecutable) {
console.error(`Server executable not found at ${serverExecutable}.\n`);
process.exit(1);
}
serverProcess = spawn(serverExecutable, [], {
stdio: ['pipe', 'pipe', 'pipe']
});
serverProcess.stdout.on('data', (data) => {
console.log(data.toString());
if (data.toString().includes('Server started.')) {
console.log(colors.green('BDSWrapper successfully launched.\n'));
}
});
serverProcess.stderr.on('data', (data) => {
console.error(`STDERR: ${data.toString()}`);
});
serverProcess.on('exit', (code) => {
console.log(colors.yellow(`Server process exited with code ${code}.\n`));
});
};
const stopServer = () => {
if (!serverProcess) {
return;
}
serverProcess.stdin.write('stop\n');
serverProcess.on('exit', () => {
console.log(colors.magenta('Server stopped successfully.'));
process.exit(0);
});
};
app.get('/', (req, res) => {
res.status(200).send('Server is running.');
});
// Function to handle HTTP POST requests for commands
app.post('/command', (req, res) => {
const requestAuth = req.headers['auth'];
if (requestAuth !== authCode) {
console.error('Invalid authentication.\n');
res.status(403).send('Forbidden.');
return;
}
let command = JSON.stringify(req.body); // Stringify body to test for bad requests
if (!command || command === "{}") {
console.error('No command received.\n');
res.status(400).send('Bad Request.');
return;
}
command = req.body.trim(); // Use original body and trim whitespace to avoid extra characters
console.log(colors.green(`Received command: ${command}\n`));
if (command === 'stop') {
console.log(colors.yellow('Received stop command via HTTP. Exiting...\n'));
stopServer();
} else if (serverProcess && serverProcess.stdin) {
serverProcess.stdin.write(`${command}\n`); // Execute command
}
res.status(200).send('OK.');
});
app.listen(httpPort, () => {
console.log(colors.yellow(`HTTP Listener started. Listening on port ${httpPort}...\n`));
startServer();
});