This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
db.js
850 lines (703 loc) · 21.2 KB
/
db.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* eslint-disable camelcase */
const fetch = require('node-fetch');
const mysql = require('mysql2');
const pool = mysql.createPool({
host: process.env.database.host,
port: process.env.database.port,
user: process.env.database.user,
password: process.env.database.password,
database: process.env.database.database,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
// If you were smart, you would complete redo the coin system by using something that could higher the coin limit, while being able to handle big numbers. (since Javascript sucks at handling big numbers)
pool.query(
`
CREATE TABLE IF NOT EXISTS \`accounts\` (
\`email\` varchar(255) PRIMARY KEY,
\`password\` varchar(255) DEFAULT NULL,
\`discord_id\` varchar(255) DEFAULT NULL,
\`pterodactyl_id\` varchar(255) DEFAULT NULL,
\`blacklisted\` varchar(255) DEFAULT NULL,
\`coins\` int(11) DEFAULT NULL,
\`package\` varchar(255) DEFAULT NULL,
\`memory\` int(11) DEFAULT NULL,
\`disk\` int(11) DEFAULT NULL,
\`cpu\` int(11) DEFAULT NULL,
\`servers\` int(11) DEFAULT NULL,
\`name\` varchar(255) DEFAULT NULL,
\`reset_id\` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
);
pool.query(
`
CREATE TABLE IF NOT EXISTS \`renewal_timer\` (
\`server_id\` varchar(255) PRIMARY KEY,
\`date\` varchar(255) DEFAULT NULL,
\`action\` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
);
pool.query(
`
CREATE TABLE IF NOT EXISTS \`settings\` (
\`id\` varchar(255) PRIMARY KEY,
\`name\` varchar(255) DEFAULT NULL,
\`smtp_server\` varchar(255) DEFAULT NULL,
\`smtp_port\` int(11) DEFAULT NULL,
\`smtp_user\` varchar(255) DEFAULT NULL,
\`smtp_pass\` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
);
pool.query(
`
CREATE TABLE IF NOT EXISTS \`coupons\` (
\`code\` varchar(255) PRIMARY KEY,
\`coins\` int(11) DEFAULT NULL,
\`memory\` int(11) DEFAULT NULL,
\`disk\` int(11) DEFAULT NULL,
\`cpu\` int(11) DEFAULT NULL,
\`servers\` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
`
);
async function createSettings(id) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO settings (id, name, smtp_server, smtp_port, smtp_user, smtp_pass) VALUES (?, ?, ?, ?, ?, ?)',
[
id,
'Fyreactyl',
process.env.mail.server,
process.env.mail.port,
process.env.mail.user,
process.env.mail.pass,
],
function (error, results, fields) {
if (error) return reject(error);
resolve(results);
}
);
});
}
module.exports = {
async findOrCreateSettings(id) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM settings WHERE id = ?',
[id],
async function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) return resolve(await createSettings(id));
const settings = results[0];
resolve(settings);
}
);
});
},
async updatePassword(email, password) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET password = ? WHERE email = ?',
[password, email],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async updateDiscordId(email, newId) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET discord_id = ? WHERE email = ?',
[newId, email],
function (error, results, fields) {
if (error) return reject(error);
resolve(results);
}
);
});
},
async updateResetId(email, newID) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET reset_id = ? WHERE email = ?',
[newID, email],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async updateName(id, name) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE settings SET name = ? WHERE id = ?',
[name, id],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async updateSmtp(id, server, port, user, password) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE settings SET smtp_server = ?, smtp_port = ?, smtp_user = ?, smtp_pass = ? WHERE id = ?',
[server, port, user, password, id],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async createAccountOnDB(
email,
pterodactyl_id,
discord_id = null,
name = null,
password = null
) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO accounts (email, discord_id, pterodactyl_id, blacklisted, coins, package, memory, disk, cpu, servers, name, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
[
email,
discord_id,
pterodactyl_id,
'false',
0,
null,
0,
0,
0,
0,
name,
password,
],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async createOrFindAccount(
username,
email,
first_name,
last_name,
discord_id = null,
password = null
) {
const generated_password =
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
const specialChars = containsSpecialChars(username);
const generatedUsername =
Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15);
const account = await fetch(
`${process.env.pterodactyl.domain}/api/application/users`,
{
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.pterodactyl.key}`,
},
body: JSON.stringify({
username: specialChars ? generatedUsername : username,
email: email,
first_name: first_name,
last_name: last_name,
password: password ? password : generated_password,
}),
}
);
if ((await account.status) === 201) {
// Successfully created account.
const accountinfo = await account.json();
await this.createAccountOnDB(
email,
accountinfo.attributes.id,
discord_id,
username,
password
);
accountinfo.attributes.password = generated_password;
accountinfo.attributes.relationships = {
servers: { object: 'list', data: [] },
};
return accountinfo.attributes;
} else {
// Find account.
const accountlistjson = await fetch(
`${
process.env.pterodactyl.domain
}/api/application/users?include=servers&filter[email]=${encodeURIComponent(
email
)}`,
{
method: 'get',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.pterodactyl.key}`,
},
}
);
const accountlist = await accountlistjson.json();
const user = accountlist.data.filter(
(acc) => acc.attributes.email === email
);
if (user.length === 1) {
const userid = user[0].attributes.id;
await this.createAccountOnDB(
email,
userid,
discord_id,
username,
password
);
return user[0].attributes;
}
return false;
}
},
async fetchAccountPterodactylID(pterodactyl_id) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM accounts WHERE pterodactyl_id = ?',
[pterodactyl_id],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) return resolve(null);
const userInfo = results[0];
resolve(userInfo);
}
);
});
},
async fetchAccountByResetId(resetId) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM accounts WHERE reset_id = ?',
[resetId],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) return resolve(null);
const userInfo = results[0];
resolve(userInfo);
}
);
});
},
async fetchAccountDiscordID(discord_id) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM accounts WHERE discord_id = ?',
[discord_id],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) return resolve(null);
const userInfo = results[0];
resolve(userInfo);
}
);
});
},
async fetchAccountByEmail(email) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM accounts WHERE email = ?',
[email],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) return resolve(null);
const userInfo = results[0];
resolve(userInfo);
}
);
});
},
async fetchAccountByEmailAndPassword(email, password) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM accounts WHERE email = ? AND password = ?',
[email, password],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) return resolve(null);
const userInfo = results[0];
resolve(userInfo);
}
);
});
},
async getCoinsByEmail(email) {
const dbinfo = await this.fetchAccountByEmail(email);
if (!dbinfo) return null;
return dbinfo.coins || 0;
},
async getCoinsByDiscordID(discord_id) {
const dbinfo = await this.fetchAccountDiscordID(discord_id);
if (!dbinfo) return null;
return dbinfo.coins || 0;
},
async addCoinsByEmail(email, amount) {
const dbinfo = await this.fetchAccountByEmail(email);
if (!dbinfo) return null;
let coins = dbinfo.coins || 0;
coins += amount;
coins = Math.round(coins);
if (coins < 0) coins = 0;
if (coins > 2147483647) coins = 2147483647;
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET coins = ? WHERE accounts.email = ?',
[coins, email],
function (error, results, fields) {
if (error) return reject(error);
resolve(coins);
}
);
});
},
async addCoinsByDiscordID(discord_id, amount) {
const dbinfo = await this.fetchAccountDiscordID(discord_id);
if (!dbinfo) return null;
let coins = dbinfo.coins || 0;
coins += amount;
coins = Math.round(coins);
if (coins < 0) coins = 0;
if (coins > 2147483647) coins = 2147483647;
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET coins = ? WHERE accounts.discord_id = ?',
[coins, discord_id],
function (error, results, fields) {
if (error) return reject(error);
resolve(coins);
}
);
});
},
async setCoinsByEmail(email, amount) {
const dbinfo = await this.fetchAccountByEmail(email);
if (!dbinfo) return null;
let coins = Math.round(amount);
if (coins < 0) coins = 0;
if (coins > 2147483647) coins = 2147483647;
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET coins = ? WHERE email = ?',
[coins, email],
function (error, results, fields) {
if (error) return reject(error);
resolve(Math.round(coins));
}
);
});
},
async setCoinsByDiscordID(discord_id, amount) {
const dbinfo = await this.fetchAccountDiscordID(discord_id);
if (!dbinfo) return null;
let coins = Math.round(amount);
if (coins < 0) coins = 0;
if (coins > 2147483647) coins = 2147483647;
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET coins = ? WHERE discord_id = ?',
[coins, discord_id],
function (error, results, fields) {
if (error) return reject(error);
resolve(Math.round(coins));
}
);
});
},
async setPackageByEmail(email, pkg) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET package = ? WHERE email = ?',
[pkg, email],
function (error, results, fields) {
if (error) return reject(error);
resolve(pkg);
}
);
});
},
async setPackageByDiscordID(discord_id, pkg) {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET package = ? WHERE discord_id = ?',
[pkg, discord_id],
function (error, results, fields) {
if (error) return reject(error);
resolve(pkg);
}
);
});
},
async setResourcesByEmail(email, memory, disk, cpu, servers) {
const additions = [];
const the_array = []; // Contains variables for "?" in MySQL.
// Beautiful code that hurts my eyes, and I'm lazy af. - Two
if (typeof memory === 'number') {
additions.push('memory = ?');
the_array.push(memory);
if (memory > 1073741823) memory = 1073741823;
}
if (typeof disk === 'number') {
additions.push('disk = ?');
the_array.push(disk);
if (disk > 1073741823) disk = 1073741823;
}
if (typeof cpu === 'number') {
additions.push('cpu = ?');
the_array.push(cpu);
if (cpu > 1073741823) cpu = 1073741823;
}
if (typeof servers === 'number') {
additions.push('servers = ?');
the_array.push(servers);
if (servers > 1073741823) servers = 1073741823;
}
the_array.push(email);
return new Promise((resolve, reject) => {
pool.query(
`UPDATE accounts SET ${additions.join(', ')} WHERE email = ?`,
the_array,
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async setResourcesByDiscordID(discord_id, memory, disk, cpu, servers) {
const additions = [];
const the_array = []; // Contains variables for "?" in MySQL.
// Beautiful code that hurts my eyes, and I'm lazy af. - Two
if (typeof memory === 'number') {
additions.push('memory = ?');
the_array.push(memory);
if (memory > 1073741823) memory = 1073741823;
}
if (typeof disk === 'number') {
additions.push('disk = ?');
the_array.push(disk);
if (disk > 1073741823) disk = 1073741823;
}
if (typeof cpu === 'number') {
additions.push('cpu = ?');
the_array.push(cpu);
if (cpu > 1073741823) cpu = 1073741823;
}
if (typeof servers === 'number') {
additions.push('servers = ?');
the_array.push(servers);
if (servers > 1073741823) servers = 1073741823;
}
the_array.push(discord_id);
return new Promise((resolve, reject) => {
pool.query(
`UPDATE accounts SET ${additions.join(', ')} WHERE discord_id = ?`,
the_array,
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async getAllRenewalTimers() {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM renewal_timer',
function (error, results, fields) {
if (error) return reject(error);
resolve(results);
}
);
});
},
async getSingleRenewalDate(server_id) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM renewal_timer WHERE server_id = ?',
[server_id],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) {
return resolve({
action: '???',
timer: '???',
});
}
resolve({
action: results[0].action,
timer: parseFloat(results[0].date),
});
}
);
});
},
async runDBTimerActions(server_id, date, action = 'suspend') {
await this.removeRenewTimerFromDB(server_id);
await this.addRenewTimerToDB(server_id, date, action);
return true;
},
async addRenewTimerToDB(server_id, date, action) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO renewal_timer (server_id, date, action) VALUES (?, ?, ?)',
[server_id, date, action],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async removeRenewTimerFromDB(server_id) {
return new Promise((resolve, reject) => {
pool.query(
'DELETE FROM renewal_timer WHERE server_id=?',
[server_id],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
},
async createCoupon(code, coins, memory, disk, cpu, servers) {
const check_if_coupon_exists = await this.getCouponInfo(code);
if (!check_if_coupon_exists) {
return new Promise((resolve, reject) => {
pool.query(
'INSERT INTO coupons (code, coins, memory, disk, cpu, servers) VALUES (?, ?, ?, ?, ?, ?)',
[code, coins, memory, disk, cpu, servers],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
} else {
return new Promise((resolve, reject) => {
pool.query(
'UPDATE coupons SET coins = ?, memory = ?, disk = ?, cpu = ?, servers = ? WHERE code = ?',
[coins, memory, disk, cpu, servers, code],
function (error, results, fields) {
if (error) return reject(error);
resolve(true);
}
);
});
}
},
async allAccounts() {
return new Promise((resolve, reject) => {
pool.query('SELECT * FROM accounts', function (error, results, fields) {
if (error) return reject(error);
resolve(results);
});
});
},
async claimCoupon(code) {
const check_if_coupon_exists = await this.getCouponInfo(code);
if (check_if_coupon_exists) {
return new Promise((resolve, reject) => {
pool.query(
'DELETE FROM coupons WHERE code = ?',
[code],
async (error, results, fields) => {
if (error) return reject(error);
resolve(check_if_coupon_exists);
}
);
});
} else {
return null;
}
},
async getCouponInfo(code) {
return new Promise((resolve, reject) => {
pool.query(
'SELECT * FROM coupons WHERE code = ?',
[code],
function (error, results, fields) {
if (error) return reject(error);
if (results.length !== 1) resolve(null);
resolve(results[0]);
}
);
});
},
async deleteCoupon(code) {
const check_if_coupon_exists = await this.getCouponInfo(code);
if (!check_if_coupon_exists) return null;
return new Promise((resolve, reject) => {
pool.query(
'DELETE FROM coupons WHERE code = ?',
[code],
async (error, results, fields) => {
if (error) return reject(error);
resolve(check_if_coupon_exists);
}
);
});
},
async blacklistStatusByEmail(email) {
return (await this.fetchAccountByEmail(email))?.blacklisted;
},
async blacklistStatusByDiscordID(discord_id) {
return (await this.fetchAccountDiscordID(discord_id))?.blacklisted;
},
async toggleBlacklistByEmail(email, specific) {
const new_status = specific || !(await this.blacklistStatusByEmail(email));
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET blacklisted = ? WHERE email = ?',
[
new_status.toString(), // Is .toString() required? Too lazy to check.
email,
],
function (error, results, fields) {
if (error) return reject(error);
resolve(new_status);
}
);
});
},
async toggleBlacklistByDiscordID(discord_id, specific) {
const new_status =
specific || !(await this.blacklistStatusByDiscordID(discord_id));
return new Promise((resolve, reject) => {
pool.query(
'UPDATE accounts SET blacklisted = ? WHERE discord_id = ?',
[
new_status.toString(), // Is .toString() required? Too lazy to check.
discord_id,
],
function (error, results, fields) {
if (error) return reject(error);
resolve(new_status);
}
);
});
},
};
function containsSpecialChars(str) {
const specialChars = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
return specialChars.test(str);
}