From 6d0a2f28e5145efe506ae7413477260d404bd3e7 Mon Sep 17 00:00:00 2001 From: Nick Krecklow Date: Mon, 30 Mar 2020 01:31:35 -0500 Subject: [PATCH] prevent high player counts from crashing browsers --- lib/ping.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/ping.js b/lib/ping.js index 3d53087..8305c9e 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -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){