This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
forked from ArchmageInc/minecraft-player-locations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
230 lines (209 loc) · 6.94 KB
/
server.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
const WebSocket = require('ws');
const Rcon = require('modern-rcon');
const Fs = require('fs');
const Https = require('https');
const config = {
WEBSOCKET_PORT: Number(process.env.WEBSOCKET_PORT) || 8888,
WEBSOCKET_KEY: process.env.WEBSOCKET_KEY || '',
WEBSOCKET_CERT: process.env.WEBSOCKET_CERT || '',
RCON_HOST: process.env.RCON_HOST,
RCON_PORT: Number(process.env.RCON_PORT) || 25575,
RCON_PASSWORD: process.env.RCON_PASSWORD,
RCON_TIMEOUT: process.env.RCON_TIMEOUT || 5000,
frequency: 1000,
debug: process.env.DEBUG || false,
maxErrors: 5
};
const emptyData = JSON.stringify({});
var rcon,
connections = [],
playerData = emptyData,
rconErrorCount = 0,
wsIdCounter = 1,
loopId = -1;
async function initializeRcon() {
console.info(`Initializing RCON connection to ${config.RCON_HOST}:${config.RCON_PORT}`);
try {
rcon = new Rcon(config.RCON_HOST, config.RCON_PORT, config.RCON_PASSWORD, config.RCON_TIMEOUT);
await connectRcon();
} catch (error) {
console.error('Failed to initialize RCON connection');
if (error.code == 'ECONNREFUSED')
console.error('RCON port not listening');
else
console.error(error);
}
}
async function connectRcon() {
if (rcon && !rcon.hasAuthed) {
console.info('Attempting to establish an RCON connection');
await rcon.connect();
console.info('RCON connection established');
rconErrorCount = 0;
} else if (!rcon) {
throw new Error('RCON object not initialized');
}
}
async function resetRcon() {
try {
if (rcon) {
// custom fix: disconnect is not timing out by itself
await Promise.any(
rcon.disconnect(), // try to disconnect
new Promise(resolve => setTimeout(resolve, config.RCON_TIMEOUT)) // or time out
);
rcon = null;
}
} catch (error) {
rcon = null;
}
}
function initializeWebSocketServer() {
console.info(`Starting WebSocket Server on port ${config.WEBSOCKET_PORT}`);
try {
let webSocketServer;
if (config.WEBSOCKET_KEY && config.WEBSOCKET_CERT) {
console.info('Using a secure connection');
const server = Https.createServer({
key: Fs.readFileSync(config.WEBSOCKET_KEY),
cert: Fs.readFileSync(config.WEBSOCKET_CERT)
});
webSocketServer = new WebSocket.Server({
server: server
});
server.listen(config.WEBSOCKET_PORT);
} else {
webSocketServer = new WebSocket.Server({
port: config.WEBSOCKET_PORT
});
}
webSocketServer.on('connection', openWebSocketConnection);
} catch (error) {
console.error(`Failed to initialize WebSocket Server: `, error);
}
}
function openWebSocketConnection(ws, req) {
ws.rconId = wsIdCounter++;
if (config.debug)
console.info(`WebSocket connection ${ws.rconId} opened from ${req.connection.remoteAddress}`);
connections.push(ws);
ws.on('close', closeWebSocketConnection);
ws.send(playerData); // send current data
if (loopId === -1) {
loopId = setTimeout(loop, config.frequency); // enable loop for first connection
if (config.debug)
console.info('Loop started');
}
}
function closeWebSocketConnection() {
const index = connections.indexOf(this);
if (index >= 0)
connections.splice(index, 1);
if (config.debug)
console.info(`WebSocket connection ${this.rconId} closed`);
};
async function loop() {
if (connections.length === 0) {
// disable loop, it's not needed atm
loopId = -1;
playerData = emptyData;
if (config.debug)
console.info('Loop stopped (no connections)');
return;
}
// reconnect
try {
if (!rcon)
await initializeRcon();
if (!rcon.hasAuthed)
await connectRcon();
} catch (error) {
console.error(`Could not establish rcon connection. Reason: ${error.message}`);
loopId = setTimeout(loop, config.frequency);
return;
}
// update
const lastData = playerData;
try {
await updatePlayerData();
} catch (error) {
console.error(`Could not update player data. Reason: ${error.message}`);
rconErrorCount++;
if (rconErrorCount >= config.maxErrors) {
console.info('Too many RCON errors, attempting to re-establish connection...');
playerData = emptyData; // reset data too
await resetRcon();
}
}
if (lastData === playerData) { // nothing changed, skip update
loopId = setTimeout(loop, config.frequency);
return;
}
// send
if (config.debug)
console.info(`Sending player data to ${connections.length} connections`, playerData);
try {
connections.forEach((conn) => {
conn.send(playerData);
});
} finally {
loopId = setTimeout(loop, config.frequency);
}
}
// 1.16: Additional 'of' after 'max'
const playerListRegex = /There are \d+ of a max(?: of)? \d+ players online: ((?:[^\s,]+,?\s?)*)/
const playerPosRegex = /has the following entity data: \[(-?\d+.?\d*)d, (-?\d+.?\d*)d, (-?\d+.?\d*)d\]/
// 1.16: Dimension can be a string like 'minecraft:overworld'
const playerDimRegex = /has the following entity data: (-?\d+|"[^\s]+")/
const knownDims = ['minecraft:the_nether', 'minecraft:overworld', 'minecraft:the_end'];
async function updatePlayerData() {
// list players
const listResponse = await rcon.send('list');
let regexMatch = playerListRegex.exec(listResponse);
if (regexMatch === null)
throw new Error(`Invalid response while listing online players. Response was: ${listResponse}`);
// to array
let players = regexMatch[1].split(', ');
players = players.filter(p => p).sort(); // this ensures comparability for changes
if (players.length === 0) {
playerData = emptyData;
return; // nothing to do
}
// get every position
let newData = {};
for (const player of players) {
// position
let playerResult = await rcon.send(`data get entity ${player} Pos`);
if (playerResult === 'No entity was found')
continue; // player left
regexMatch = playerPosRegex.exec(playerResult);
if (regexMatch === null)
throw new Error(`Invalid response while getting player position for player ${player}. Response was: ${playerResult}`);
const x = parseFloat(regexMatch[1]), y = parseFloat(regexMatch[2]), z = parseFloat(regexMatch[3]);
// dimension
playerResult = await rcon.send(`data get entity ${player} Dimension`);
if (playerResult === 'No entity was found')
continue; // player left
regexMatch = playerDimRegex.exec(playerResult);
if (regexMatch === null)
throw new Error(`Invalid response while getting player dimension for player ${player}. Response was: ${playerResult}`);
let dim = parseInt(regexMatch[1], 10);
if (isNaN(dim))
dim = regexMatch[1].slice(1, -1); // starting with 1.16: dimension is already a string but in quotes
else
dim = dim >= -1 && dim <= 1 ? knownDims[dim + 1] : "unknown";
// combine
newData[player] = {
name: player,
x: x,
y: y,
z: z,
dimension: dim
};
}
// save
playerData = JSON.stringify(newData);
}
// entry point
initializeRcon();
initializeWebSocketServer();