This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
548 lines (471 loc) · 17.2 KB
/
index.ts
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
/**
* @module Server
* @category Server
*/
"use strict";
import config from "@/natuna.config.js";
import pkg from "@/package.json";
import fs from "fs";
import path from "path";
import util from "util";
import mysql from "mysql2";
import fetch from "node-fetch";
import figlet from "figlet";
import glob from "tiny-glob";
import CrypterWrapper from "@server/crypter";
import PlayersWrapper from "@server/players";
import CommandWrapper, * as Command from "@server/command";
import MySQL from "@server/database/mysql";
import Cassandra from "@server/database/cassandra";
import MongoDB from "@server/database/mongodb";
import Events from "@server/events";
/**
* @hidden
*/
// prettier-ignore
export type SelectedDatabaseDriver =
typeof config.core.db extends "mysql" ?
MySQL :
typeof config.core.db extends "cassandra" ?
Cassandra :
typeof config.core.db extends "mongodb" ?
MongoDB :
MySQL;
export default class Server extends Events {
/**
* @description
* Database function to execute classic SQL query
*
* @param query SQL query string to execute
*
* @example
* ```ts
* dbQuery('CREATE DATABASE IF NOT EXISTS databaseName');
* ```
*/
dbQuery: MySQL["utils"]["executeQuery"];
/**
* @description
* Crypter to Encrypt or Decrypt your secret data
*/
crypter: (algorithm: string, secretKey: string) => CrypterWrapper;
/**
* @description
* Players wrapper
*/
players: PlayersWrapper;
/**
* @hidden
*
* @description
* Configurations
*/
private config: typeof config;
/**
* @hidden
*
* @description
* List of All Plugins
*/
private plugins: {
[key: string]: {
path: string;
client?: {
modules: Array<string>;
config: {
[key: string]: any;
};
};
server?: {
modules: Array<string>;
config: {
[key: string]: any;
};
};
nui?: boolean;
};
};
/**
* @hidden
*
* @description
* List of Registered Commands
*/
private commands: { [key: string]: CommandWrapper };
/**
* @hidden
*/
constructor() {
super();
this.config = config;
this.plugins = {};
this.commands = {};
this.dbQuery = this.db("").utils.executeQuery;
this.crypter = (algorithm: string = this.config.core.crypter.algorithm, secretKey: string = this.config.core.crypter.secretKey) => new CrypterWrapper(algorithm, secretKey);
this.players = new PlayersWrapper(this, this.config);
this.addSharedEventHandler("natuna:server:registerCommand", this.registerCommand);
this.addSharedCallbackEventHandler("natuna:server:requestClientSettings", this._events.requestClientSettings);
this.addServerEventHandler("playerConnecting", this._events.playerConnecting);
this.addServerEventHandler("onServerResourceStart", this._events.onServerResourceStart);
this.addServerEventHandler("onServerResourceStop", this._events.onServerResourceStop);
// Test database connection (on startup) <-- check if connection success or not, also executing default SQL 👍
this.dbQuery(`CREATE DATABASE IF NOT EXISTS \`${this.config.core.db}\``);
}
/**
* @description
* Database wrapper
*/
db = (table: string): SelectedDatabaseDriver => {
switch (typeof config.core.db) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
case "cassandra":
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return new Cassandra();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
case "mysql":
default:
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return new MySQL(mysql.createConnection(this.config.core.db), table);
}
};
/**
* @description
* Use this function to hold next script below this from executing before it finish the timeout itself
*
* @param ms Milisecond to wait
*
* @example
* ```ts
* const one = () => true; // Always use ES6 for better practice
* const two = () => true;
*
* setTimeout(async() => { // Always do it on async
* one();
* await wait(5000); // Wait 5s (5000ms) before executing next function, always await it
* two(); // Executed after 5 second after wait before
* });
* ```
*/
wait = (ms: number) => new Promise((res) => setTimeout(res, ms));
/**
* @description
* Registrating a command. If isClientCommand was set true, the handler would just triggering a client registered command
*
* @param name Name of the command
* @param handler Function to executed
* @param config Configuration of the command
* @param isClientCommand Whether if the command was sent from client or not
*
* @example
* ```ts
* registerCommand(
* 'hello',
* (src, args) => console.log('Hello!'),
* {
* description: "Say Hello"
* }
* });
* ```
*/
registerCommand = (name: string | Array<string>, handler: Command.Handler, config: Command.Config = {}, isClientCommand = false) => {
const addCommand = (name: string) => {
// Throws an error when same server command was registered twice
if (this.commands[name] && !isClientCommand) throw new Error(`Command "${name}" had already been registered before!`);
emitNet("natuna:client:setCommandDescription", -1, name, config);
// Return if client command was already registered before
if (this.commands[name] && isClientCommand) return;
this.commands[name] = new CommandWrapper(this, name, handler, config, isClientCommand);
};
if (Array.isArray(name)) {
for (const alias of name) {
addCommand(alias);
}
} else {
addCommand(name);
}
return true;
};
/**
* @hidden
* @readonly
*
* @description
* Logger to Console
*
* @param text Text to logs
*/
private _logger = (...text: any) => {
return console.log("\x1b[33m%s\x1b[0m", "[🏝️ Natuna Framework]", "[SERVER]", ...text);
};
/**
* @hidden
* @readonly
*
* @description
* Loops through folder and retrieve every plugins file
*
* @param type Type of the plugin (Client or Server)
*/
private _getPlugins = async () => {
let plugins: Server["plugins"] = {};
const basePath = GetResourcePath(GetCurrentResourceName());
let pluginList = await glob("plugins/*", {
cwd: basePath,
});
for (const pluginPath of pluginList) {
const pluginFullPath = path.join(basePath, pluginPath);
if (fs.lstatSync(pluginFullPath).isDirectory()) {
const pluginName = path.basename(pluginPath);
const moduleTypeList = await glob("*", {
cwd: pluginFullPath,
});
plugins[pluginName] = {
path: path.normalize(pluginPath),
client: {
modules: [],
config: this.config.plugins[pluginName]?.client || {},
},
server: {
modules: [],
config: this.config.plugins[pluginName]?.server || {},
},
nui: false,
};
for (const moduleType of moduleTypeList) {
const moduleTypePath = path.join(pluginFullPath, moduleType);
if (moduleType === "ui") {
plugins[pluginName].nui = true;
} else if (fs.lstatSync(moduleTypePath).isDirectory()) {
const modules = await glob("**/*.{ts,js}", {
cwd: moduleTypePath,
});
for (let module of modules) {
module = path.normalize(module).replace(/\\/g, "/");
plugins[pluginName][moduleType as "client" | "server"].modules.push(module);
}
}
}
}
}
return plugins;
};
/**
* @hidden
* @readonly
*
* @description
* This function is to init a plugin, differs from the client ones, it's triggered whenever the script was ready
*/
private _initServerPlugins = async () => {
this._logger(`Intializing Server Plugins`);
let count = 1;
for (const pluginName in this.plugins) {
const plugin = this.plugins[pluginName];
this._logger(`> Starting Plugins: \x1b[47m\x1b[2m\x1b[30m ${count}. ${pluginName} \x1b[0m`);
for (let pluginFile of plugin.server.modules) {
const module = require(`../../plugins/${pluginName}/server/${pluginFile}`);
if (module && module.default && typeof module.default === "function") {
this._logger(` - Mounting Module: \x1b[32m${pluginFile}\x1b[0m`);
module.default(this, plugin.server.config);
}
}
count += 1;
}
this._logger("Server Plugins Ready!");
};
/**
* @hidden
* @readonly
*
* @description
* Check package version and compare it to remote repository.
*/
private _checkPackageVersion = () => {
return new Promise((resolve, reject) => {
this._logger(`Welcome! Checking your version...`);
this._logger(`You are currently using version ${pkg.version}.`);
// Can't use async await, idk why :(
fetch("https://raw.githack.com/natuna-framework/fivem/master/package.json")
.then((res) => res.text())
.then((data) => {
const rpkg: typeof pkg = JSON.parse(data);
const currentVersion = parseInt(pkg.version.replace(/\./g, ""));
const remoteVersion = parseInt(rpkg.version.replace(/\./g, ""));
if (currentVersion < remoteVersion) {
this._logger(`\x1b[31mYou are not using the latest version of Natuna Framework (${rpkg.version}), please update it!`);
} else if (currentVersion > remoteVersion) {
this._logger(`\x1b[31mYou are not using a valid version version of Natuna Framework!`);
} else if (currentVersion === remoteVersion) {
this._logger("\x1b[32mYou are using a latest version of Natuna Framework!");
}
return resolve(true);
});
});
};
/**
* @hidden
* @readonly
*
* @description
* List of events on server
*/
private _events = {
/**
* @description
* Listen on whenever a player joining a session also validating that player before joining the session
*
* @param name The display name of the player connecting
* @param setKickReason A function used to set a reason message for when the event is canceled.
* @param deferrals An object to control deferrals.
*/
playerConnecting: async (name: string, setKickReason: (reason: string) => any, deferrals: { [key: string]: any }) => {
/**
* @description
* Getting player
*/
deferrals.defer();
const player = (global as any).source;
deferrals.update(`[🏝 Natuna] Hello ${name}! Please wait until we verify your account.`);
/**
* @description
* Checking License
*/
const playerIds = this.players.utils.getIdentifiers(player);
if (!playerIds.license || typeof playerIds.license == "undefined") {
return deferrals.done("[🏝 Natuna] Your game license is invalid!");
}
this._logger(`Player ${name} Joining the server. (License ID: ${playerIds.license})`);
/**
* @description
* Checking Whitelist Status
*/
if (this.config.core.isWhitelisted) {
const checkWhitelistStatus = await this.db("whitelist_lists").findFirst({
where: {
license: playerIds.license,
},
});
if (!checkWhitelistStatus) {
return deferrals.done("[🏝 Natuna] You are not whitelisted!");
}
}
/**
* @description
* Checking Account
*/
deferrals.update(`[🏝 Natuna] Finding your account in our database.`);
const user = await this.db("users").findFirst({
where: {
license: playerIds.license,
},
});
const newCheckpointData = {
last_ip: playerIds.ip.toString(),
last_login: new Date().toLocaleString("en-US", { timeZone: "Asia/Jakarta" }).toString(),
};
/**
* @description
* Handling when account does not exist
*/
// If account does not exist
if (!user) {
await this.db("users").write({
data: {
license: playerIds.license,
...newCheckpointData,
},
});
}
// If account exist
else {
// If user was banned
const checkBanStatus = await this.db("ban_lists").findFirst({
where: {
license: playerIds.license,
},
});
if (checkBanStatus) {
return deferrals.done(checkBanStatus.reason);
}
// If not
await this.db("users").update({
data: {
...newCheckpointData,
},
where: {
license: playerIds.license,
},
});
}
return deferrals.done();
},
/**
* @description
* Listen on whenever a player requested plugins to be setup on their client
*/
requestClientSettings: async (source: number) => {
while (Object.keys(this.plugins).length == 0) await this.wait(500);
let figletText: string;
let clientPlugins = { ...this.plugins };
for (const plugins in clientPlugins) {
delete clientPlugins[plugins].server;
}
await new Promise((resolve) => {
figlet.text("Natuna Framework", {}, (err: Error, result: string) => {
figletText = result;
resolve(true);
});
});
return JSON.stringify({
figletText,
plugins: clientPlugins,
config: {
players: this.config.core.players,
discordRPC: this.config.core.discordRPC,
nui: this.config.core.nui,
game: this.config.core.game,
},
});
},
/**
* @description
* Listen when Natuna Framework is starting
*/
onServerResourceStart: async (resourceName: string) => {
if (GetCurrentResourceName() == resourceName) {
// Starting
this.triggerServerEvent("natuna:server:starting");
await new Promise((resolve) => {
figlet.text("Natuna Framework", {}, (err: Error, result: string) => {
console.log(result);
resolve(true);
});
});
await this._checkPackageVersion();
// Initializing
this.triggerServerEvent("natuna:server:initializing");
this._logger("Starting Server...");
this.plugins = await this._getPlugins();
await this._initServerPlugins();
// Ready
this.triggerServerEvent("natuna:server:ready");
this._logger("Server Ready!");
}
},
/**
* @description
* Listen when Natuna Framework is stopping
*/
onServerResourceStop: (resourceName: string) => {
if (GetCurrentResourceName() == resourceName) {
// Stopping
this.triggerServerEvent("natuna:server:stopped");
}
},
};
}
new Server();