67 lines
2.0 KiB
JavaScript
67 lines
2.0 KiB
JavaScript
const express = require('express');
|
|
const { promisify } = require('util');
|
|
const { exec } = require('child_process');
|
|
const app = express();
|
|
const port = 3066;
|
|
require('dotenv').config();
|
|
|
|
const execPromise = promisify(exec);
|
|
|
|
async function checkConnectionStatus(hostname, port) {
|
|
try {
|
|
const command = `${process.env.STATUS_CHECK_PATH} -host ${hostname} -port ${port}`;
|
|
const { stdout, stderr } = await execPromise(command);
|
|
if (stderr) {
|
|
return { isOnline: false, error: stderr };
|
|
}
|
|
const data = JSON.parse(stdout);
|
|
return { isOnline: true, data };
|
|
} catch (error) {
|
|
return { isOnline: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
async function checkGeyserStatus(hostname, port) {
|
|
try {
|
|
const command = `${process.env.GEYSER_STATUS_CHECK_PATH} -host ${hostname} -port ${port}`;
|
|
const { stdout, stderr } = await execPromise(command);
|
|
if (stderr) {
|
|
return { isOnline: false, error: stderr };
|
|
}
|
|
const data = JSON.parse(stdout);
|
|
return { isOnline: true, data };
|
|
} catch (error) {
|
|
return { isOnline: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
app.use(express.static('public'));
|
|
|
|
app.get('/', (req, res) => {
|
|
res.sendFile(__dirname + '/public/index.html');
|
|
});
|
|
|
|
app.get('/java/:host/:port', async (req, res) => {
|
|
const { host, port } = req.params;
|
|
try {
|
|
const result = await checkConnectionStatus(host, port);
|
|
res.json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ isOnline: false, error: `Server error: ${error.message}` });
|
|
}
|
|
});
|
|
|
|
app.get('/bedrock/:host/:port', async (req, res) => {
|
|
const { host, port } = req.params;
|
|
try {
|
|
const result = await checkGeyserStatus(host, port);
|
|
console.log(result)
|
|
res.json(result);
|
|
} catch (error) {
|
|
res.status(500).json({ isOnline: false, error: `Server error: ${error.message}` });
|
|
}
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`Server running at http://localhost:${port}`);
|
|
}); |