2021-10-30 06:03:58 +00:00
|
|
|
//const net = require('net)
|
|
|
|
const fetch = require('node-fetch')
|
|
|
|
const loggerLanding = LoggerUtil('%c[Landing]', 'color: #000668; font-weight: bold')
|
2018-04-16 02:35:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the status of a minecraft server.
|
|
|
|
*
|
|
|
|
* @param {string} address The server address.
|
|
|
|
* @param {number} port Optional. The port of the server. Defaults to 25565.
|
|
|
|
* @returns {Promise.<Object>} A promise which resolves to an object containing
|
|
|
|
* status information.
|
|
|
|
*/
|
2021-10-30 06:03:58 +00:00
|
|
|
exports.getStatus = async function (address, port = 25565) {
|
2018-04-16 02:35:14 +00:00
|
|
|
|
2021-10-30 06:03:58 +00:00
|
|
|
if (port == null || port == '') {
|
2018-04-16 02:35:14 +00:00
|
|
|
port = 25565
|
|
|
|
}
|
2021-10-30 06:03:58 +00:00
|
|
|
if (typeof port === 'string') {
|
2018-04-16 02:35:14 +00:00
|
|
|
port = parseInt(port)
|
|
|
|
}
|
2021-10-30 06:03:58 +00:00
|
|
|
let online,
|
|
|
|
version,
|
|
|
|
motd,
|
|
|
|
onlinePlayers,
|
|
|
|
maxPlayers;
|
|
|
|
let data;
|
2018-04-16 02:35:14 +00:00
|
|
|
|
2021-10-30 06:03:58 +00:00
|
|
|
try {
|
|
|
|
var response = await fetch(`https://api.mcsrvstat.us/2/${address}`);
|
|
|
|
data = await response.json();
|
|
|
|
online = data.online;
|
|
|
|
version = data.version
|
|
|
|
motd = data.motd.clean
|
|
|
|
onlinePlayers = data.players.online;
|
|
|
|
maxPlayers = data.players.max
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
loggerLanding.warn('Unable to refresh server status, assuming offline.')
|
|
|
|
loggerLanding.debug(err)
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
online,
|
|
|
|
version,
|
|
|
|
motd,
|
|
|
|
onlinePlayers,
|
|
|
|
maxPlayers
|
|
|
|
};
|
|
|
|
/*return await new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
Commented out for documentation
|
2018-04-16 02:35:14 +00:00
|
|
|
const socket = net.connect(port, address, () => {
|
2018-09-22 09:07:18 +00:00
|
|
|
let buff = Buffer.from([0xFE, 0x01])
|
2018-04-16 02:35:14 +00:00
|
|
|
socket.write(buff)
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.setTimeout(2500, () => {
|
|
|
|
socket.end()
|
|
|
|
reject({
|
|
|
|
code: 'ETIMEDOUT',
|
|
|
|
errno: 'ETIMEDOUT',
|
|
|
|
address,
|
|
|
|
port
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-30 06:03:58 +00:00
|
|
|
Commented out for documentation
|
2018-04-16 02:35:14 +00:00
|
|
|
socket.on('data', (data) => {
|
2021-10-30 06:03:58 +00:00
|
|
|
if (data != null && data != '') {
|
2018-04-16 02:35:14 +00:00
|
|
|
let server_info = data.toString().split('\x00\x00\x00')
|
|
|
|
const NUM_FIELDS = 6
|
2021-10-30 06:03:58 +00:00
|
|
|
if (server_info != null && server_info.length >= NUM_FIELDS) {
|
2018-04-16 02:35:14 +00:00
|
|
|
resolve({
|
|
|
|
online: true,
|
|
|
|
version: server_info[2].replace(/\u0000/g, ''),
|
|
|
|
motd: server_info[3].replace(/\u0000/g, ''),
|
|
|
|
onlinePlayers: server_info[4].replace(/\u0000/g, ''),
|
2021-10-30 06:03:58 +00:00
|
|
|
maxPlayers: server_info[5].replace(/\u0000/g, '')
|
2018-04-16 02:35:14 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
resolve({
|
|
|
|
online: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socket.end()
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('error', (err) => {
|
|
|
|
socket.destroy()
|
|
|
|
reject(err)
|
|
|
|
// ENOTFOUND = Unable to resolve.
|
|
|
|
// ECONNREFUSED = Unable to connect to port.
|
|
|
|
})
|
2021-10-30 06:03:58 +00:00
|
|
|
})*/
|
2018-04-16 02:35:14 +00:00
|
|
|
|
|
|
|
}
|