prevent high player counts from crashing browsers

This commit is contained in:
Nick Krecklow 2020-03-30 01:31:35 -05:00
parent 0ac311ce37
commit 6d0a2f28e5
No known key found for this signature in database
GPG Key ID: 5F149FDE156FFA94

@ -1,6 +1,7 @@
var mcpe_ping = require('mcpe-ping-fixed');
var mcpc_ping = require('mc-ping-updated');
var logger = require('./logger');
var util = require('./util');
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
@ -21,7 +22,7 @@ function pingMinecraftPC(host, port, timeout, callback, version) {
callback(null, {
players: {
online: parseInt(res.players.online),
online: capPlayerCount(host, parseInt(res.players.online)),
max: parseInt(res.players.max)
},
version: parseInt(res.version.protocol),
@ -43,7 +44,7 @@ function pingMinecraftPE(host, port, timeout, callback) {
// Remap our JSON into our custom structure.
callback(err, {
players: {
online: parseInt(res.currentPlayers),
online: capPlayerCount(host, parseInt(res.currentPlayers)),
max: parseInt(res.maxPlayers)
},
version: res.version,
@ -53,6 +54,20 @@ function pingMinecraftPE(host, port, timeout, callback) {
}, timeout);
}
// player count can be up to 1^32-1, which is a massive scale and destroys browser performance when rendering graphs
// Artificially cap and warn to prevent propogating garbage
function capPlayerCount(host, playerCount) {
const maxPlayerCount = 250000;
if (playerCount !== Math.min(playerCount, maxPlayerCount)) {
logger.log('warn', '%s returned a player count of %d, Minetrack has capped it to %d to prevent browser performance issues with graph rendering. If this is in error, please edit maxPlayerCount in ping.js!', host, playerCount, maxPlayerCount);
return maxPlayerCount;
} else if (playerCount !== Math.max(playerCount, 0)) {
logger.log('warn', '%s returned an invalid player count of %d, setting to 0.', host, playerCount);
return 0;
}
return playerCount;
}
exports.ping = function(host, port, type, timeout, callback, version) {
if (type === 'PC') {
util.unfurlSRV(host, port, function(host, port){