-
Notifications
You must be signed in to change notification settings - Fork 529
/
index.js
560 lines (500 loc) · 21.6 KB
/
index.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
const modulename = 'FXRunner';
import { spawn } from 'node:child_process';
import path from 'node:path';
import { setTimeout as sleep } from 'node:timers/promises';
import { parseArgsStringToArgv } from 'string-argv';
import StreamValues from 'stream-json/streamers/StreamValues';
import { convars, txEnv } from '@core/globalData';
import { validateFixServerConfig } from '@core/extras/fxsConfigHelper';
import OutputHandler from './outputHandler';
import { customAlphabet } from 'nanoid/non-secure';
import dict51 from 'nanoid-dictionary/nolookalikes';
import consoleFactory from '@extras/console';
const console = consoleFactory(modulename);
const genMutex = customAlphabet(dict51, 5);
//Helpers
const now = () => { return Math.round(Date.now() / 1000); };
const escape = (x) => { return x.toString().replace(/"/g, '\uff02'); };
const formatCommand = (cmd, ...params) => {
return `${cmd} "` + [...params].map(escape).join('" "') + '"';
};
const getMutableConvars = (isCmdLine = false) => {
const p = isCmdLine ? '+' : '';
const playerDbConfigs = globals.playerDatabase.config;
const checkPlayerJoin = (playerDbConfigs.onJoinCheckBan || playerDbConfigs.whitelistMode !== 'disabled');
return [
//type, name, value
[`${p}setr`, 'txAdmin-locale', globals.translator.language ?? 'en'],
[`${p}set`, 'txAdmin-localeFile', globals.translator.customLocalePath ?? 'false'],
[`${p}setr`, 'txAdmin-verbose', console.isVerbose],
[`${p}set`, 'txAdmin-checkPlayerJoin', checkPlayerJoin],
[`${p}set`, 'txAdmin-menuAlignRight', globals.config.menuAlignRight],
[`${p}set`, 'txAdmin-menuPageKey', globals.config.menuPageKey],
[`${p}set`, 'txAdmin-hideDefaultAnnouncement', globals.config.hideDefaultAnnouncement],
[`${p}set`, 'txAdmin-hideDefaultDirectMessage', globals.config.hideDefaultDirectMessage],
[`${p}set`, 'txAdmin-hideDefaultWarning', globals.config.hideDefaultWarning],
[`${p}set`, 'txAdmin-hideDefaultScheduledRestartWarning', globals.config.hideDefaultScheduledRestartWarning],
];
};
export default class FXRunner {
constructor(txAdmin, config) {
this.config = config;
//Checking config validity
if (this.config.shutdownNoticeDelay < 0 || this.config.shutdownNoticeDelay > 30) {
throw new Error('The fxRunner.shutdownNoticeDelay setting must be between 0 and 30 seconds.');
}
this.spawnVariables = null;
this.fxChild = null;
this.restartDelayOverride = 0;
this.history = [];
this.lastKillRequest = 0;
this.fxServerHost = null;
this.currentMutex = null;
this.cfxId = null;
this.outputHandler = new OutputHandler(txAdmin);
}
//================================================================
/**
* Refresh fxRunner configurations
*/
refreshConfig() {
this.config = globals.configVault.getScoped('fxRunner');
}//Final refreshConfig()
//================================================================
/**
* Receives the signal that all the start banner was already printed and other modules loaded
*/
signalStartReady() {
if (!this.config.autostart) return;
if (this.config.serverDataPath === null || this.config.cfgPath === null) {
return console.warn('Please open txAdmin on the browser to configure your server.');
}
if (!globals.adminVault || !globals.adminVault.admins) {
return console.warn('The server will not auto start because there are no admins configured.');
}
this.spawnServer(true);
}//Final signalStartReady()
//================================================================
/**
* Setup the spawn parameters
*/
setupVariables() {
// Prepare extra args
let extraArgs = [];
if (typeof this.config.commandLine === 'string' && this.config.commandLine.length) {
extraArgs = parseArgsStringToArgv(this.config.commandLine);
}
// Prepare default args (these convars can't change without restart)
const txAdminInterface = (convars.forceInterface)
? `${convars.forceInterface}:${convars.txAdminPort}`
: `127.0.0.1:${convars.txAdminPort}`;
const cmdArgs = [
getMutableConvars(true),
extraArgs,
'+set', 'onesync', this.config.onesync,
'+sets', 'txAdmin-version', txEnv.txAdminVersion,
'+setr', 'txAdmin-menuEnabled', globals.config.menuEnabled,
'+set', 'txAdmin-luaComHost', txAdminInterface,
'+set', 'txAdmin-luaComToken', globals.webServer.luaComToken,
'+set', 'txAdminServerMode', 'true', //Can't change this one due to fxserver code compatibility
'+exec', this.config.cfgPath,
].flat(2);
// Configure spawn parameters according to the environment
if (txEnv.isWindows) {
this.spawnVariables = {
command: `${txEnv.fxServerPath}/FXServer.exe`,
args: cmdArgs,
};
} else {
const alpinePath = path.resolve(txEnv.fxServerPath, '../../');
this.spawnVariables = {
command: `${alpinePath}/opt/cfx-server/ld-musl-x86_64.so.1`,
args: [
'--library-path', `${alpinePath}/usr/lib/v8/:${alpinePath}/lib/:${alpinePath}/usr/lib/`,
'--',
`${alpinePath}/opt/cfx-server/FXServer`,
'+set', 'citizen_dir', `${alpinePath}/opt/cfx-server/citizen/`,
...cmdArgs,
],
};
}
}//Final setupVariables()
//================================================================
/**
* Spawns the FXServer and sets up all the event handlers
* @param {boolean} announce
* @returns {string} null or error message
*/
async spawnServer(announce) {
//If the server is already alive
if (this.fxChild !== null) {
const msg = `The server is already started.`;
console.error(msg);
return msg;
}
//Setup variables
globals.webServer.resetToken();
this.currentMutex = genMutex();
this.setupVariables();
console.verbose.log('Spawn Variables: ' + this.spawnVariables.args.join(' '));
//Sanity Check
if (
this.spawnVariables == null
|| typeof this.spawnVariables.command == 'undefined'
|| typeof this.spawnVariables.args == 'undefined'
) {
const msg = `this.spawnVariables is not set.`;
console.error(msg);
return msg;
}
//If there is any FXServer configuration missing
if (this.config.serverDataPath === null || this.config.cfgPath === null) {
const msg = `Cannot start the server with missing configuration (serverDataPath || cfgPath).`;
console.error(msg);
return msg;
}
//Validating server.cfg & configuration
try {
const result = await validateFixServerConfig(this.config.cfgPath, this.config.serverDataPath);
if (result.errors) {
const msg = `**Unable to start the server due to error(s) in your config file(s):**\n${result.errors}`;
console.error(msg);
return msg;
}
if (result.warnings) {
const msg = `**Warning regarding your configuration file(s):**\n${result.warnings}`;
console.warn(msg);
}
this.fxServerHost = result.connectEndpoint;
} catch (error) {
const errMsg = `server.cfg error: ${error.message}`;
console.error(errMsg);
if (error.message.includes('unreadable')) {
console.error('That is the file where you configure your server and start resources.');
console.error('You likely moved/deleted your server files or copied the txData folder from another server.');
console.error('To fix this issue, open the txAdmin web interface then go to "Settings > FXServer" and fix the "Server Data Folder" and "CFG File Path".');
}
return errMsg;
}
//Reseting monitor stats
globals.healthMonitor.resetMonitorStats();
//Announcing
if (announce === 'true' || announce === true) {
globals.discordBot.sendAnnouncement({
type: 'success',
description: {
key: 'server_actions.spawning_discord',
data: { servername: globals.config.serverName }
}
});
}
//Starting server
let pid;
let historyIndex;
try {
this.fxChild = spawn(
this.spawnVariables.command,
this.spawnVariables.args,
{
cwd: this.config.serverDataPath,
stdio: ['pipe', 'pipe', 'pipe', 'pipe'],
},
);
if (typeof this.fxChild.pid === 'undefined') {
throw new Error(`Executon of "${this.spawnVariables.command}" failed.`);
}
pid = this.fxChild.pid.toString();
console.ok(`>> [${pid}] FXServer Started!`);
globals.logger.fxserver.writeMarker('starting');
this.history.push({
pid: pid,
timestamps: {
start: now(),
kill: false,
exit: false,
close: false,
},
});
historyIndex = this.history.length - 1;
globals.webServer?.webSocket.pushRefresh('status');
} catch (error) {
console.error('Failed to start FXServer with the following error:');
console.dir(error);
process.exit(5400);
}
//Setting up stream handlers
this.fxChild.stdout.setEncoding('utf8');
//Setting up event handlers
this.fxChild.on('close', function (code) {
let printableCode;
if (typeof code === 'number') {
printableCode = `0x${code.toString(16).toUpperCase()}`;
} else {
printableCode = new String(code).toUpperCase();
}
console.warn(`>> [${pid}] FXServer Closed (${printableCode}).`);
this.history[historyIndex].timestamps.close = now();
globals.webServer?.webSocket.pushRefresh('status');
}.bind(this));
this.fxChild.on('disconnect', function () {
console.warn(`>> [${pid}] FXServer Disconnected.`);
}.bind(this));
this.fxChild.on('error', function (err) {
console.warn(`>> [${pid}] FXServer Errored:`);
console.dir(err);
}.bind(this));
this.fxChild.on('exit', function () {
process.stdout.write('\n'); //Make sure this isn't concatenated with the last line
console.warn(`>> [${pid}] FXServer Exited.`);
this.history[historyIndex].timestamps.exit = now();
globals.webServer?.webSocket.pushRefresh('status');
if (this.history[historyIndex].timestamps.exit - this.history[historyIndex].timestamps.start <= 5) {
setTimeout(() => {
console.warn('FXServer didn\'t start. This is not an issue with txAdmin.');
}, 500);
}
}.bind(this));
this.fxChild.stdin.on('error', () => { });
this.fxChild.stdin.on('data', () => { });
this.fxChild.stdout.on('error', () => { });
this.fxChild.stdout.on('data', this.outputHandler.write.bind(this.outputHandler, 'stdout', this.currentMutex));
this.fxChild.stderr.on('error', () => { });
this.fxChild.stderr.on('data', this.outputHandler.write.bind(this.outputHandler, 'stderr', this.currentMutex));
const tracePipe = this.fxChild.stdio[3].pipe(StreamValues.withParser());
tracePipe.on('error', (data) => {
//NOTE: this should be verbose warn, but since i've never seen it happen, let's see if someone reports it.
console.error(`FD3 decode error: ${data.message}`);
});
tracePipe.on('data', this.outputHandler.trace.bind(this.outputHandler, this.currentMutex));
return null;
}//Final spawnServer()
//================================================================
/**
* Restarts the FXServer
* @param {string} reason
* @param {string} author
*/
async restartServer(reason = null, author = null) {
try {
//Restart server
const killError = await this.killServer(reason, author, true);
if (killError) return killError;
//If delay override
if (this.restartDelayOverride) {
console.warn(`Restarting the fxserver with delay override ${this.restartDelayOverride}`);
await sleep(this.restartDelayOverride);
} else {
await sleep(this.config.restartDelay);
}
//Start server again :)
return this.spawnServer();
} catch (error) {
const errMsg = `Couldn't restart the server.`;
console.error(errMsg);
console.verbose.dir(error);
return errMsg;
}
}
//================================================================
/**
* Kills the FXServer
* @param {string} reason
* @param {string} author
* @param {boolean} isRestarting
*/
async killServer(reason = null, author = null, isRestarting = false) {
try {
//Prevent concurrent restart request
const msTimestamp = Date.now();
if (msTimestamp - this.lastKillRequest < this.config.shutdownNoticeDelay * 1000) {
return 'Restart already in progress.';
} else {
this.lastKillRequest = msTimestamp;
}
// Send warnings
const messageType = isRestarting ? 'restarting' : 'stopping';
const messageColor = isRestarting ? 'warning' : 'danger';
const tOptions = {
servername: globals.config.serverName,
reason: reason ?? 'no reason provided',
};
this.sendEvent('serverShuttingDown', {
delay: this.config.shutdownNoticeDelay * 1000,
author: author ?? 'txAdmin',
message: globals.translator.t(`server_actions.${messageType}`, tOptions),
});
globals.discordBot.sendAnnouncement({
type: messageColor,
description: {
key: `server_actions.${messageType}_discord`,
data: tOptions
}
});
//Awaiting restart delay
//The 250 is so at least everyone is kicked from the server
await sleep(250 + this.config.shutdownNoticeDelay * 1000);
//Stopping server
if (this.fxChild !== null) {
this.fxChild.kill();
this.fxChild = null;
this.history[this.history.length - 1].timestamps.kill = now();
}
globals.resourcesManager.handleServerStop();
globals.playerlistManager.handleServerStop(this.currentMutex);
return null;
} catch (error) {
const msg = "Couldn't kill the server. Perhaps What Is Dead May Never Die.";
console.error(msg);
console.verbose.dir(error);
this.fxChild = null;
return msg;
}
}
//================================================================
/**
* Resets the convars in the server.
* Useful for when we change txAdmin settings and want it to reflect on the server.
* This will also fire the `txAdmin:event:configChanged`
*/
resetConvars() {
console.log('Refreshing fxserver convars.');
try {
const convarList = getMutableConvars(false);
console.verbose.dir(convarList);
convarList.forEach(([type, name, value]) => {
this.srvCmd(formatCommand(type, name, value));
});
return this.sendEvent('configChanged');
} catch (error) {
console.verbose.error('Error resetting server convars');
console.verbose.dir(error);
return false;
}
}
//================================================================
/**
* Fires an `txAdmin:event` inside the server via srvCmd > stdin > command > lua broadcaster.
* @param {string} eventType
* @param {object} data
*/
sendEvent(eventType, data = {}) {
if (typeof eventType !== 'string') throw new Error('Expected eventType as String!');
try {
const eventCommand = formatCommand(
'txaEvent',
eventType,
JSON.stringify(data),
);
// console.verbose.dir({ eventType, data});
return this.srvCmd(eventCommand);
} catch (error) {
console.verbose.error(`Error writing firing server event ${eventType}`);
console.verbose.dir(error);
return false;
}
}
//================================================================
/**
* Pipe a string into FXServer's stdin (aka executes a cfx's command)
* TODO: make this method accept an array and apply the formatCommand() logic
* @param {string} command
*/
srvCmd(command) {
if (typeof command !== 'string') throw new Error('Expected String!');
if (this.fxChild === null) return false;
const sanitized = command.replaceAll(/\n/g, ' ');
try {
const success = this.fxChild.stdin.write(sanitized + '\n');
globals.logger.fxserver.writeMarker('command', sanitized);
return success;
} catch (error) {
console.verbose.error('Error writing to fxChild.stdin');
console.verbose.dir(error);
return false;
}
}
//================================================================
/**
* Handles a live console command input
* @param {import('../WebServer/authLogic').AuthedAdminType} admin
* @param {string} command
*/
liveConsoleCmdHandler(admin, command) {
admin.logCommand(command, 'command');
globals.fxRunner.srvCmd(command);
}
//================================================================
/**
* Pipe a string into FXServer's stdin (aka executes a cfx's command) and returns the stdout output.
* NOTE: used only in webroutes\fxserver\commands.js and webroutes\player\actions.js
* FIXME: deprecate this with a promise that resolves or rejects.
* we can create a promise with settimeout to reject, and create a function that resolves it
* and set this function in a map with the cmd id, and the resolve function as value
* the internal functions should fd3 {id, message?} and outputhandler do Map.get(id)(message)
* @param {*} command
* @param {*} bufferTime the size of the buffer in milliseconds
* @returns {string} buffer
*/
async srvCmdBuffer(command, bufferTime = 1500) {
if (typeof command !== 'string') throw new Error('Expected String!');
if (this.fxChild === null) return false;
this.outputHandler.cmdBuffer = '';
this.outputHandler.enableCmdBuffer = true;
const result = this.srvCmd(command);
if (!result) return false;
await sleep(bufferTime);
this.outputHandler.enableCmdBuffer = false;
return this.outputHandler.cmdBuffer.replace(/\x1b\[\d+(;\d)?m/g, '');
}
//================================================================
/**
* Returns the status of the server, with the states being:
* - not started
* - spawn ready
* - spawn awaiting last: <list of pending status of last instance>
* - kill pending: <list of pending events from current instance>
* - killed
* - closing
* - closed
* - spawned
* @returns {string} status
*/
getStatus() {
if (!this.history.length) return 'not started';
let curr = this.history[this.history.length - 1];
if (!curr.timestamps.start && this.history.length == 1) {
throw new Error('This should NOT happen. Let\'s see how long people will take to find this...');
} else if (!curr.timestamps.start) {
let last = this.history[this.history.length - 2];
let pending = Object.keys(last.timestamps).filter((k) => !curr.timestamps[k]);
if (!pending.length) {
return 'spawn ready';
} else {
return 'spawn awaiting last: ' + pending.join(', ');
}
} else if (curr.timestamps.kill) {
let pending = Object.keys(curr.timestamps).filter((k) => !curr.timestamps[k]);
if (pending.length) {
return 'kill pending: ' + pending.join(', ');
} else {
return 'killed';
}
} else if (curr.timestamps.exit && !curr.timestamps.close) {
return 'closing';
} else if (curr.timestamps.exit && curr.timestamps.close) {
return 'closed';
} else {
return 'spawned';
}
}
//================================================================
/**
* Returns the current fxserver uptime in seconds
* @returns {numeric} buffer
*/
getUptime() {
if (!this.history.length) return 0;
let curr = this.history[this.history.length - 1];
return now() - curr.timestamps.start;
}
};