forked from Cryptkeeper/Minetrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
316 lines (255 loc) · 8.94 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
var server = require('./lib/server');
var ping = require('./lib/ping');
var logger = require('./lib/logger');
var mojang = require('./lib/mojang_services');
var util = require('./lib/util');
var db = require('./lib/database');
var config = require('./config.json');
var servers = require('./servers.json');
var networkHistory = [];
var connectedClients = 0;
var currentVersionIndex = {
'PC': 0,
'PE': 0
};
var networkVersions = [];
var graphData = [];
var highestPlayerCount = {};
var averagePlayerCount = {};
var lastGraphPush = [];
function pingAll() {
for (var i = 0; i < servers.length; i++) {
// Make sure we lock our scope.
(function(network) {
// Asign auto generated color if not present
if (!network.color) {
network.color = util.stringToColor(network.name);
}
var attemptedVersion = config.versions[network.type][currentVersionIndex[network.type]];
ping.ping(network.ip, network.port, network.type, config.rates.connectTimeout, function(err, res) {
// Handle our ping results, if it succeeded.
if (err) {
logger.log('error', 'Failed to ping ' + network.ip + ': ' + err.message);
}
// If we have favicon override specified, use it.
if (res && config.faviconOverride && config.faviconOverride[network.name]) {
res.favicon = config.faviconOverride[network.name];
}
handlePing(network, res, err, attemptedVersion);
}, attemptedVersion);
})(servers[i]);
}
currentVersionIndex['PC']++;
currentVersionIndex['PE']++;
if (currentVersionIndex['PC'] >= config.versions['PC'].length) {
// Loop around
currentVersionIndex['PC'] = 0;
}
if (currentVersionIndex['PE'] >= config.versions['PE'].length) {
// Loop around
currentVersionIndex['PE'] = 0;
}
}
// This is where the result of a ping is feed.
// This stores it and converts it to ship to the frontend.
function handlePing(network, res, err, attemptedVersion) {
// Log our response.
if (!networkHistory[network.name]) {
networkHistory[network.name] = [];
}
// Update the version list
if (!networkVersions[network.name]) {
networkVersions[network.name] = [];
}
// If the result version matches the attempted version, the version is supported
var _networkVersions = networkVersions[network.name];
if (res) {
if (res.version == attemptedVersion) {
if (_networkVersions.indexOf(res.version) == -1) {
_networkVersions.push(res.version);
}
} else {
// Mismatch, so remove the version from the supported version list
var index = _networkVersions.indexOf(attemptedVersion);
if (index != -1) {
_networkVersions.splice(index, 1);
}
}
}
// Update the clients
var networkSnapshot = {
info: {
name: network.name,
timestamp: util.getCurrentTimeMs(),
type: network.type
},
versions: _networkVersions,
record: highestPlayerCount[network.ip],
average: averagePlayerCount[network.ip]
};
if (res) {
networkSnapshot.result = res;
// Validate that we have logToDatabase enabled otherwise in memory pings
// will create a record that's only valid for the runtime duration.
if (config.logToDatabase && res.players.online > highestPlayerCount[network.ip]) {
highestPlayerCount[network.ip] = res.players.online;
}
if (config.logToDatabase && res.players.online) {
averagePlayerCount[network.ip].players += res.players.online;
averagePlayerCount[network.ip].count++;
}
} else if (err) {
networkSnapshot.error = err;
}
server.io.sockets.emit('update', networkSnapshot);
var _networkHistory = networkHistory[network.name];
// Remove our previous data that we don't need anymore.
for (var i = 0; i < _networkHistory.length; i++) {
delete _networkHistory[i].info;
if (_networkHistory[i].result) {
delete _networkHistory[i].result.favicon;
}
}
_networkHistory.push({
error: err,
result: res,
versions: _networkVersions,
timestamp: util.getCurrentTimeMs(),
info: {
ip: network.ip,
port: network.port,
type: network.type,
name: network.name
}
});
// Make sure we never log too much.
if (_networkHistory.length > 72) { // 60/2.5 = 24, so 24 is one minute
_networkHistory.shift();
}
// Log it to the database if needed.
if (config.logToDatabase) {
db.log(network.ip, util.getCurrentTimeMs(), res ? res.players.online : 0);
}
// Push it to our graphs.
var timeMs = util.getCurrentTimeMs();
// The same mechanic from trimUselessPings is seen here.
// If we dropped the ping, then to avoid destroying the graph, ignore it.
// However if it's been too long since the last successful ping, we'll send it anyways.
if (config.logToDatabase) {
if (!lastGraphPush[network.ip] || (timeMs - lastGraphPush[network.ip] >= 60 * 1000 && res) || timeMs - lastGraphPush[network.ip] >= 70 * 1000) {
lastGraphPush[network.ip] = timeMs;
// Don't have too much data!
util.trimOldPings(graphData);
if (!graphData[network.name]) {
graphData[network.name] = [];
}
graphData[network.name].push([timeMs, res ? res.players.online : 0]);
// Send the update.
server.io.sockets.emit('updateHistoryGraph', {
ip: network.ip,
name: network.name,
players: (res ? res.players.online : 0),
timestamp: timeMs
});
}
}
}
// Start our main loop that does everything.
function startMainLoop() {
util.setIntervalNoDelay(pingAll, config.rates.pingAll);
util.setIntervalNoDelay(function() {
mojang.update(config.rates.mojangStatusTimeout);
server.io.sockets.emit('updateMojangServices', mojang.toMessage());
}, config.rates.upateMojangStatus);
}
function startServices() {
server.start();
// Track how many people are currently connected.
server.io.on('connect', function(client) {
// We're good to connect them!
connectedClients += 1;
logger.log('info', '%s connected, total clients: %d', client.request.connection.remoteAddress, connectedClients);
// We send the boot time (also sent in publicConfig.json) to the frontend to validate they have the same config.
// If so, they'll send back "requestListing" event, otherwise they will pull the new config and retry.
client.emit('bootTime', util.getBootTime());
// Attach our listeners.
client.on('disconnect', function() {
connectedClients -= 1;
logger.log('info', '%s disconnected, total clients: %d', client.request.connection.remoteAddress, connectedClients);
});
client.on('requestHistoryGraph', function() {
if (config.logToDatabase) {
// Send them the big 24h graph.
client.emit('historyGraph', graphData);
}
});
client.on('requestListing', function() {
// Send them our previous data, so they have somewhere to start.
client.emit('updateMojangServices', mojang.toMessage());
// Remap our associative array into just an array.
var networkHistoryKeys = Object.keys(networkHistory);
networkHistoryKeys.sort();
// Send each individually, this should look cleaner than waiting for one big array to transfer.
for (var i = 0; i < servers.length; i++) {
var server = servers[i];
if (!(server.name in networkHistory) || networkHistory[server.name].length < 1) {
// This server hasn't been ping'd yet. Send a hacky placeholder.
client.emit('add', [[{
error: {
description: 'Waiting'
},
result: null,
timestamp: util.getCurrentTimeMs(),
info: {
ip: server.ip,
port: server.port,
type: server.type,
name: server.name
}
}]]);
} else {
client.emit('add', [networkHistory[networkHistoryKeys[i]]]);
}
}
client.emit('syncComplete');
});
});
startMainLoop();
}
logger.log('info', 'Booting, please wait...');
if (config.logToDatabase) {
// Setup our database.
db.setup();
var timestamp = util.getCurrentTimeMs();
db.queryPings(config.graphDuration, function(data) {
graphData = util.convertServerHistory(data);
completedQueries = 0;
logger.log('info', 'Queried and parsed ping history in %sms', util.getCurrentTimeMs() - timestamp);
for (var i = 0; i < servers.length; i++) {
(function(server) {
const days = 7;
const startRecord = new Date();
db.getTotalRecord(server.ip, days, function(record) {
logger.log('info', 'Completed total recored query for %s in %s ms', server.ip, new Date() - startRecord);
highestPlayerCount[server.ip] = record || 0;
completedQueries += 1;
if (completedQueries / 2 === servers.length) {
startServices();
}
});
const startAverage = new Date();
db.getAveragePlayers(server.ip, days, function(avgPlayers) {
logger.log('info', 'Completed average players query for %s in %s ms', server.ip, new Date() - startAverage);
averagePlayerCount[server.ip] = avgPlayers || {players: 0, count: 0};
completedQueries += 1;
if (completedQueries / 2 === servers.length) {
startServices();
}
});
})(servers[i]);
}
});
} else {
logger.log('warn', 'Database logging is not enabled. You can enable it by setting "logToDatabase" to true in config.json. This requires sqlite3 to be installed.');
startServices();
}