Minetrack/lib/servers.js

298 lines
8.0 KiB
JavaScript
Raw Permalink Normal View History

2023-12-30 23:03:54 +00:00
const crypto = require("crypto");
2023-12-30 23:03:54 +00:00
const DNSResolver = require("./dns");
const Server = require("./server");
2023-12-30 23:03:54 +00:00
const { GRAPH_UPDATE_TIME_GAP, TimeTracker } = require("./time");
const { getPlayerCountOrNull } = require("./util");
2023-12-30 23:03:54 +00:00
const config = require("../config");
const minecraftVersions = require("../minecraft_versions");
class ServerRegistration {
2023-12-30 23:03:54 +00:00
serverId;
lastFavicon;
versions = [];
recordData;
graphData = [];
constructor(app, serverId, data) {
this._app = app;
this.serverId = serverId;
this.data = data;
this._pingHistory = [];
this.dnsResolver = new DNSResolver(this.data.ip, this.data.port);
}
2023-12-30 23:03:54 +00:00
handlePing(timestamp, resp, err, version, updateHistoryGraph) {
// Use null to represent a failed ping
2023-12-30 23:03:54 +00:00
const unsafePlayerCount = getPlayerCountOrNull(resp);
// Store into in-memory ping data
2023-12-30 23:03:54 +00:00
TimeTracker.pushAndShift(
this._pingHistory,
unsafePlayerCount,
TimeTracker.getMaxServerGraphDataLength()
);
// Only notify the frontend to append to the historical graph
// if both the graphing behavior is enabled and the backend agrees
// that the ping is eligible for addition
if (updateHistoryGraph) {
2023-12-30 23:03:54 +00:00
TimeTracker.pushAndShift(
this.graphData,
unsafePlayerCount,
TimeTracker.getMaxGraphDataLength()
);
}
// Delegate out update payload generation
2023-12-30 23:03:54 +00:00
return this.getUpdate(timestamp, resp, err, version);
}
2023-12-30 23:03:54 +00:00
getUpdate(timestamp, resp, err, version) {
const update = {};
// Always append a playerCount value
// When resp is undefined (due to an error), playerCount will be null
2023-12-30 23:03:54 +00:00
update.playerCount = getPlayerCountOrNull(resp);
if (resp) {
2023-12-30 23:03:54 +00:00
if (
resp.version &&
this.updateProtocolVersionCompat(
resp.version,
version.protocolId,
version.protocolIndex
)
) {
// Append an updated version listing
2023-12-30 23:03:54 +00:00
update.versions = this.versions;
}
2023-12-30 23:03:54 +00:00
if (
config.logToDatabase &&
(!this.recordData || resp.players.online > this.recordData.playerCount)
) {
this.recordData = {
playerCount: resp.players.online,
2023-12-30 23:03:54 +00:00
timestamp: TimeTracker.toSeconds(timestamp),
};
// Append an updated recordData
2023-12-30 23:03:54 +00:00
update.recordData = this.recordData;
// Update record in database
2023-12-30 23:03:54 +00:00
this._app.database.updatePlayerCountRecord(
this.data.ip,
resp.players.online,
timestamp
);
}
if (this.updateFavicon(resp.favicon)) {
2023-12-30 23:03:54 +00:00
update.favicon = this.getFaviconUrl();
}
if (config.logToDatabase) {
// Update calculated graph peak regardless if the graph is being updated
// This can cause a (harmless) desync between live and stored data, but it allows it to be more accurate for long surviving processes
if (this.findNewGraphPeak()) {
2023-12-30 23:03:54 +00:00
update.graphPeakData = this.getGraphPeak();
}
}
} else if (err) {
// Append a filtered copy of err
// This ensures any unintended data is not leaked
2023-12-30 23:03:54 +00:00
update.error = this.filterError(err);
}
2023-12-30 23:03:54 +00:00
return update;
}
2023-12-30 23:03:54 +00:00
getPingHistory() {
if (this._pingHistory.length > 0) {
const payload = {
versions: this.versions,
recordData: this.recordData,
2023-12-30 23:03:54 +00:00
favicon: this.getFaviconUrl(),
};
// Only append graphPeakData if defined
// The value is lazy computed and conditional that config->logToDatabase == true
2023-12-30 23:03:54 +00:00
const graphPeakData = this.getGraphPeak();
if (graphPeakData) {
2023-12-30 23:03:54 +00:00
payload.graphPeakData = graphPeakData;
}
// Assume the ping was a success and define result
// pingHistory does not keep error references, so its impossible to detect if this is an error
// It is also pointless to store that data since it will be short lived
2023-12-30 23:03:54 +00:00
payload.playerCount = this._pingHistory[this._pingHistory.length - 1];
// Send a copy of pingHistory
// Include the last value even though it is contained within payload
// The frontend will only push to its graphData from playerCountHistory
2023-12-30 23:03:54 +00:00
payload.playerCountHistory = this._pingHistory;
2023-12-30 23:03:54 +00:00
return payload;
}
return {
error: {
2023-12-30 23:03:54 +00:00
message: "Pinging...",
},
recordData: this.recordData,
graphPeakData: this.getGraphPeak(),
2023-12-30 23:03:54 +00:00
favicon: this.data.favicon,
};
}
2023-12-30 23:03:54 +00:00
loadGraphPoints(startTime, timestamps, points) {
this.graphData = TimeTracker.everyN(
timestamps,
startTime,
GRAPH_UPDATE_TIME_GAP,
(i) => points[i]
);
}
2023-12-30 23:03:54 +00:00
findNewGraphPeak() {
let index = -1;
for (let i = 0; i < this.graphData.length; i++) {
2023-12-30 23:03:54 +00:00
const point = this.graphData[i];
if (point !== null && (index === -1 || point > this.graphData[index])) {
2023-12-30 23:03:54 +00:00
index = i;
}
}
if (index >= 0) {
2023-12-30 23:03:54 +00:00
const lastGraphPeakIndex = this._graphPeakIndex;
this._graphPeakIndex = index;
return index !== lastGraphPeakIndex;
} else {
2023-12-30 23:03:54 +00:00
this._graphPeakIndex = undefined;
return false;
}
}
2023-12-30 23:03:54 +00:00
getGraphPeak() {
if (this._graphPeakIndex === undefined) {
2023-12-30 23:03:54 +00:00
return;
}
return {
playerCount: this.graphData[this._graphPeakIndex],
2023-12-30 23:03:54 +00:00
timestamp: this._app.timeTracker.getGraphPointAt(this._graphPeakIndex),
};
}
2023-12-30 23:03:54 +00:00
updateFavicon(favicon) {
// If data.favicon is defined, then a favicon override is present
// Disregard the incoming favicon, regardless if it is different
if (this.data.favicon) {
2023-12-30 23:03:54 +00:00
return false;
}
if (favicon && favicon !== this.lastFavicon) {
2023-12-30 23:03:54 +00:00
this.lastFavicon = favicon;
// Generate an updated hash
// This is used by #getFaviconUrl
2023-12-30 23:03:54 +00:00
this.faviconHash = crypto
.createHash("md5")
.update(favicon)
.digest("hex")
.toString();
2023-12-30 23:03:54 +00:00
return true;
}
2023-12-30 23:03:54 +00:00
return false;
}
2023-12-30 23:03:54 +00:00
getFaviconUrl() {
if (this.faviconHash) {
2023-12-30 23:03:54 +00:00
return Server.getHashedFaviconUrl(this.faviconHash);
} else if (this.data.favicon) {
2023-12-30 23:03:54 +00:00
return this.data.favicon;
}
}
2023-12-30 23:03:54 +00:00
updateProtocolVersionCompat(incomingId, outgoingId, protocolIndex) {
// If the result version matches the attempted version, the version is supported
2023-12-30 23:03:54 +00:00
const isSuccess = incomingId === outgoingId;
const indexOf = this.versions.indexOf(protocolIndex);
// Test indexOf to avoid inserting previously recorded protocolIndex values
if (isSuccess && indexOf < 0) {
2023-12-30 23:03:54 +00:00
this.versions.push(protocolIndex);
// Sort versions in ascending order
// This matches protocol ids to Minecraft versions release order
2023-12-30 23:03:54 +00:00
this.versions.sort((a, b) => a - b);
2023-12-30 23:03:54 +00:00
return true;
} else if (!isSuccess && indexOf >= 0) {
2023-12-30 23:03:54 +00:00
this.versions.splice(indexOf, 1);
return true;
}
2023-12-30 23:03:54 +00:00
return false;
}
2023-12-30 23:03:54 +00:00
getNextProtocolVersion() {
// Minecraft Bedrock Edition does not have protocol versions
2023-12-30 23:03:54 +00:00
if (this.data.type === "PE") {
return {
protocolId: 0,
2023-12-30 23:03:54 +00:00
protocolIndex: 0,
};
}
2023-12-30 23:03:54 +00:00
const protocolVersions = minecraftVersions[this.data.type];
if (
typeof this._nextProtocolIndex === "undefined" ||
this._nextProtocolIndex + 1 >= protocolVersions.length
) {
this._nextProtocolIndex = 0;
} else {
2023-12-30 23:03:54 +00:00
this._nextProtocolIndex++;
}
return {
protocolId: protocolVersions[this._nextProtocolIndex].protocolId,
2023-12-30 23:03:54 +00:00
protocolIndex: this._nextProtocolIndex,
};
}
2023-12-30 23:03:54 +00:00
filterError(err) {
let message = "Unknown error";
2020-04-21 23:10:07 +00:00
// Attempt to match to the first possible value
2023-12-30 23:03:54 +00:00
for (const key of ["message", "description", "errno"]) {
if (err[key]) {
2023-12-30 23:03:54 +00:00
message = err[key];
break;
}
}
2020-04-21 23:10:07 +00:00
// Trim the message if too long
if (message.length > 28) {
2023-12-30 23:03:54 +00:00
message = message.substring(0, 28) + "...";
}
2020-04-21 23:10:07 +00:00
return {
2023-12-30 23:03:54 +00:00
message: message,
};
}
2023-12-30 23:03:54 +00:00
getPublicData() {
// Return a custom object instead of data directly to avoid data leakage
return {
name: this.data.name,
ip: this.data.ip,
type: this.data.type,
2023-12-30 23:03:54 +00:00
color: this.data.color,
};
}
}
2023-12-30 23:03:54 +00:00
module.exports = ServerRegistration;