2020-05-08 09:47:10 +00:00
|
|
|
const crypto = require('crypto')
|
2020-05-08 21:53:48 +00:00
|
|
|
const dns = require('dns')
|
2020-05-08 09:47:10 +00:00
|
|
|
|
2020-05-11 23:44:29 +00:00
|
|
|
const { GRAPH_UPDATE_TIME_GAP, TimeTracker } = require('./time')
|
2020-05-08 21:00:36 +00:00
|
|
|
const Server = require('./server')
|
2020-05-08 07:56:39 +00:00
|
|
|
|
2020-05-11 23:29:26 +00:00
|
|
|
const { getPlayerCountOrNull } = require('./util')
|
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
const config = require('../config')
|
|
|
|
const minecraftVersions = require('../minecraft_versions')
|
|
|
|
|
|
|
|
class ServerRegistration {
|
2020-04-29 09:01:10 +00:00
|
|
|
serverId
|
2020-04-21 22:59:53 +00:00
|
|
|
lastFavicon
|
|
|
|
versions = []
|
|
|
|
recordData
|
|
|
|
graphData = []
|
|
|
|
|
2020-05-11 23:12:29 +00:00
|
|
|
constructor (app, serverId, data) {
|
|
|
|
this._app = app
|
2020-04-29 09:01:10 +00:00
|
|
|
this.serverId = serverId
|
2020-04-21 22:59:53 +00:00
|
|
|
this.data = data
|
|
|
|
this._pingHistory = []
|
|
|
|
}
|
|
|
|
|
2020-05-11 09:12:46 +00:00
|
|
|
handlePing (timestamp, resp, err, version, updateHistoryGraph) {
|
2020-05-11 08:10:23 +00:00
|
|
|
// Use null to represent a failed ping
|
2020-05-11 23:29:26 +00:00
|
|
|
const unsafePlayerCount = getPlayerCountOrNull(resp)
|
2020-05-08 07:52:48 +00:00
|
|
|
|
2020-05-08 05:36:15 +00:00
|
|
|
// Store into in-memory ping data
|
2020-05-11 23:29:26 +00:00
|
|
|
TimeTracker.pushAndShift(this._pingHistory, unsafePlayerCount, TimeTracker.getMaxServerGraphDataLength())
|
2020-05-08 05:36:15 +00:00
|
|
|
|
|
|
|
// 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
|
2020-05-11 09:12:46 +00:00
|
|
|
if (updateHistoryGraph) {
|
2020-05-11 23:29:26 +00:00
|
|
|
TimeTracker.pushAndShift(this.graphData, unsafePlayerCount, TimeTracker.getMaxGraphDataLength())
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 05:36:15 +00:00
|
|
|
// Delegate out update payload generation
|
2020-05-11 09:12:46 +00:00
|
|
|
return this.getUpdate(timestamp, resp, err, version)
|
2020-05-08 05:36:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 09:12:46 +00:00
|
|
|
getUpdate (timestamp, resp, err, version) {
|
2020-05-08 05:36:15 +00:00
|
|
|
const update = {}
|
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
if (resp) {
|
|
|
|
if (resp.version && this.updateProtocolVersionCompat(resp.version, version.protocolId, version.protocolIndex)) {
|
|
|
|
// Append an updated version listing
|
|
|
|
update.versions = this.versions
|
|
|
|
}
|
|
|
|
|
2020-04-21 23:51:29 +00:00
|
|
|
if (config.logToDatabase && (!this.recordData || resp.players.online > this.recordData.playerCount)) {
|
2020-04-21 22:59:53 +00:00
|
|
|
this.recordData = {
|
|
|
|
playerCount: resp.players.online,
|
2020-05-11 23:29:26 +00:00
|
|
|
timestamp: TimeTracker.toSeconds(timestamp)
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append an updated recordData
|
|
|
|
update.recordData = this.recordData
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:47:10 +00:00
|
|
|
if (this.updateFavicon(resp.favicon)) {
|
|
|
|
update.favicon = this.getFaviconUrl()
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append a result object
|
|
|
|
// This filters out unwanted data from resp
|
2020-05-08 21:13:24 +00:00
|
|
|
update.playerCount = resp.players.online
|
2020-04-29 09:01:10 +00:00
|
|
|
|
|
|
|
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()) {
|
|
|
|
update.graphPeakData = this.getGraphPeak()
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
} else if (err) {
|
|
|
|
// Append a filtered copy of err
|
|
|
|
// This ensures any unintended data is not leaked
|
|
|
|
update.error = this.filterError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
|
|
|
getPingHistory () {
|
|
|
|
if (this._pingHistory.length > 0) {
|
|
|
|
const payload = {
|
|
|
|
versions: this.versions,
|
|
|
|
recordData: this.recordData,
|
2020-05-08 09:47:10 +00:00
|
|
|
favicon: this.getFaviconUrl()
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 09:01:10 +00:00
|
|
|
// Only append graphPeakData if defined
|
|
|
|
// The value is lazy computed and conditional that config->logToDatabase == true
|
|
|
|
const graphPeakData = this.getGraphPeak()
|
|
|
|
|
|
|
|
if (graphPeakData) {
|
|
|
|
payload.graphPeakData = graphPeakData
|
|
|
|
}
|
|
|
|
|
2020-05-08 07:52:48 +00:00
|
|
|
// 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
|
2020-05-08 21:13:24 +00:00
|
|
|
payload.playerCount = this._pingHistory[this._pingHistory.length - 1]
|
2020-04-21 22:59:53 +00:00
|
|
|
|
2020-05-08 07:39:30 +00:00
|
|
|
// Send a copy of pingHistory
|
2020-05-08 07:52:48 +00:00
|
|
|
// Include the last value even though it is contained within payload
|
|
|
|
// The frontend will only push to its graphData from playerCountHistory
|
|
|
|
payload.playerCountHistory = this._pingHistory
|
2020-04-21 22:59:53 +00:00
|
|
|
|
2020-05-08 07:39:30 +00:00
|
|
|
return payload
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 07:39:30 +00:00
|
|
|
return {
|
2020-04-21 22:59:53 +00:00
|
|
|
error: {
|
2020-05-08 07:39:30 +00:00
|
|
|
message: 'Pinging...'
|
2020-04-21 22:59:53 +00:00
|
|
|
},
|
2020-04-29 09:27:10 +00:00
|
|
|
recordData: this.recordData,
|
2020-05-08 09:47:10 +00:00
|
|
|
graphPeakData: this.getGraphPeak(),
|
|
|
|
favicon: this.data.favicon
|
2020-05-08 07:39:30 +00:00
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 23:12:29 +00:00
|
|
|
loadGraphPoints (startTime, timestamps, points) {
|
2020-05-11 23:44:29 +00:00
|
|
|
this.graphData = TimeTracker.everyN(timestamps, startTime, GRAPH_UPDATE_TIME_GAP, (i) => points[i])
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
findNewGraphPeak () {
|
|
|
|
let index = -1
|
|
|
|
for (let i = 0; i < this.graphData.length; i++) {
|
|
|
|
const point = this.graphData[i]
|
2020-05-11 23:12:29 +00:00
|
|
|
if (index === -1 || point > this.graphData[index]) {
|
2020-04-21 22:59:53 +00:00
|
|
|
index = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index >= 0) {
|
|
|
|
const lastGraphPeakIndex = this._graphPeakIndex
|
|
|
|
this._graphPeakIndex = index
|
|
|
|
return index !== lastGraphPeakIndex
|
|
|
|
} else {
|
|
|
|
this._graphPeakIndex = undefined
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getGraphPeak () {
|
|
|
|
if (this._graphPeakIndex === undefined) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return {
|
2020-05-11 23:12:29 +00:00
|
|
|
playerCount: this.graphData[this._graphPeakIndex],
|
2020-05-11 23:29:26 +00:00
|
|
|
timestamp: this._app.timeTracker.getGraphPointAt(this._graphPeakIndex)
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFavicon (favicon) {
|
2020-05-08 09:47:10 +00:00
|
|
|
// If data.favicon is defined, then a favicon override is present
|
|
|
|
// Disregard the incoming favicon, regardless if it is different
|
|
|
|
if (this.data.favicon) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
if (favicon && favicon !== this.lastFavicon) {
|
|
|
|
this.lastFavicon = favicon
|
2020-05-08 09:47:10 +00:00
|
|
|
|
|
|
|
// Generate an updated hash
|
|
|
|
// This is used by #getFaviconUrl
|
2020-05-10 04:28:37 +00:00
|
|
|
this.faviconHash = crypto.createHash('md5').update(favicon).digest('hex').toString()
|
2020-05-08 09:47:10 +00:00
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-05-08 21:00:36 +00:00
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:47:10 +00:00
|
|
|
getFaviconUrl () {
|
|
|
|
if (this.faviconHash) {
|
2020-05-08 21:00:36 +00:00
|
|
|
return Server.getHashedFaviconUrl(this.faviconHash)
|
2020-05-08 09:47:10 +00:00
|
|
|
} else if (this.data.favicon) {
|
|
|
|
return this.data.favicon
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
updateProtocolVersionCompat (incomingId, outgoingId, protocolIndex) {
|
|
|
|
// If the result version matches the attempted version, the version is supported
|
|
|
|
const isSuccess = incomingId === outgoingId
|
|
|
|
const indexOf = this.versions.indexOf(protocolIndex)
|
|
|
|
|
|
|
|
// Test indexOf to avoid inserting previously recorded protocolIndex values
|
|
|
|
if (isSuccess && indexOf < 0) {
|
|
|
|
this.versions.push(protocolIndex)
|
|
|
|
|
|
|
|
// Sort versions in ascending order
|
|
|
|
// This matches protocol ids to Minecraft versions release order
|
|
|
|
this.versions.sort((a, b) => a - b)
|
|
|
|
|
|
|
|
return true
|
|
|
|
} else if (!isSuccess && indexOf >= 0) {
|
|
|
|
this.versions.splice(indexOf, 1)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
getNextProtocolVersion () {
|
2020-04-21 23:02:29 +00:00
|
|
|
// Minecraft Bedrock Edition does not have protocol versions
|
|
|
|
if (this.data.type === 'PE') {
|
|
|
|
return {
|
|
|
|
protocolId: 0,
|
|
|
|
protocolIndex: 0
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
const protocolVersions = minecraftVersions[this.data.type]
|
|
|
|
if (typeof this._nextProtocolIndex === 'undefined' || this._nextProtocolIndex + 1 >= protocolVersions.length) {
|
|
|
|
this._nextProtocolIndex = 0
|
|
|
|
} else {
|
|
|
|
this._nextProtocolIndex++
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
protocolId: protocolVersions[this._nextProtocolIndex].protocolId,
|
|
|
|
protocolIndex: this._nextProtocolIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filterError (err) {
|
2020-04-21 23:10:07 +00:00
|
|
|
let message = 'Unknown error'
|
|
|
|
|
2020-04-21 22:59:53 +00:00
|
|
|
// Attempt to match to the first possible value
|
|
|
|
for (const key of ['message', 'description', 'errno']) {
|
|
|
|
if (err[key]) {
|
2020-04-21 23:10:07 +00:00
|
|
|
message = err[key]
|
|
|
|
break
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 23:10:07 +00:00
|
|
|
|
|
|
|
// Trim the message if too long
|
|
|
|
if (message.length > 28) {
|
|
|
|
message = message.substring(0, 28) + '...'
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
2020-04-21 23:10:07 +00:00
|
|
|
|
2020-04-21 23:11:29 +00:00
|
|
|
return {
|
|
|
|
message: message
|
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
2020-05-08 21:22:24 +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,
|
|
|
|
color: this.data.color
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 21:53:48 +00:00
|
|
|
|
|
|
|
unfurlSrv (callback) {
|
|
|
|
// Skip unfurling SRV, instantly return pre-configured data
|
|
|
|
if (config.performance && config.performance.skipUnfurlSrv) {
|
|
|
|
callback(this.data.ip, this.data.port)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const timestamp = new Date().getTime()
|
|
|
|
|
|
|
|
// If a cached copy exists and is within its TTL, instantly return
|
|
|
|
if (this._lastUnfurlSrv && timestamp - this._lastUnfurlSrv.timestamp <= config.performance.unfurlSrvCacheTtl) {
|
|
|
|
callback(this._lastUnfurlSrv.ip, this._lastUnfurlSrv.port)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Group callbacks into an array
|
|
|
|
// Once resolved, fire callbacks sequentially
|
|
|
|
// This avoids callbacks possibly executing out of order
|
|
|
|
if (!this._unfurlSrvCallbackQueue) {
|
|
|
|
this._unfurlSrvCallbackQueue = []
|
|
|
|
}
|
|
|
|
|
|
|
|
this._unfurlSrvCallbackQueue.push(callback)
|
|
|
|
|
|
|
|
// Prevent multiple #resolveSrv calls per ServerRegistration
|
|
|
|
if (!this._isUnfurlingSrv) {
|
|
|
|
this._isUnfurlingSrv = true
|
|
|
|
|
|
|
|
dns.resolveSrv('_minecraft._tcp' + this.data.ip, (_, records) => {
|
|
|
|
this._lastUnfurlSrv = {
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
if (records && records.length > 0) {
|
|
|
|
this._lastUnfurlSrv.ip = records[0].name
|
|
|
|
this._lastUnfurlSrv.port = records[0].port
|
|
|
|
} else {
|
|
|
|
// Provide fallback to pre-configured data
|
|
|
|
this._lastUnfurlSrv.ip = this.data.ip
|
|
|
|
this._lastUnfurlSrv.port = this.data.port
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire the waiting callbacks in queue
|
|
|
|
// Release blocking flag to allow new #resolveSrv calls
|
|
|
|
this._isUnfurlingSrv = false
|
|
|
|
|
|
|
|
for (const callback of this._unfurlSrvCallbackQueue) {
|
|
|
|
callback(this._lastUnfurlSrv.ip, this._lastUnfurlSrv.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the callback queue
|
|
|
|
this._unfurlSrvCallbackQueue = []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 22:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ServerRegistration
|