-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
923 lines (883 loc) · 42.9 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
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
const express = require('express');
const app = express();
const session = require('express-session');
const fetch = require('node-fetch');
const fs = require('fs');
const bodyParser = require('body-parser');
const timeago = require('epoch-timeago').default;
const sessionSecret = `KZTC$@eBn+9ug75VhF!twY#sW'X3/]v%Epxz<(]j&fcL)a?q6Q`;
const mysql = require('mysql');
const { serverName, serverHost, port } = require('./config.json');
let version = '1.0.1';
const completed = require('./config.json').completedDontTouchThis;
const allowedArgs = ['--log', '--help', '-h'];
const startupArgs = process.argv.splice(2);
if(startupArgs.includes('-h') || startupArgs.includes('--help')) {
console.log('Allowed arguments:\n--help (alias: -h): Lists this help menu\n--log: Logs the IP address of any incoming request');
process.exit(0);
}
let invalidArgs = 0;
startupArgs.forEach(arg => {
if(!allowedArgs.includes(arg)) {
console.log(`INVALID ARGUMENT: ${arg}`);
invalidArgs++;
}
});
if(invalidArgs > 0) console.log('Running with invalid arguments is allowed, but is not recommended. Next time you start the script, please run with allowed arguments.');
const { database_USER, database_NAME, database_HOST, database_PASS } = require('./db_info.json');
const http = require('http').createServer(app);
const db_config = {
host: database_HOST,
user: database_USER,
password: database_PASS,
database: database_NAME
};
let con;
// functions
function handleDisconnect() {
con = mysql.createConnection(db_config);
con.connect(function (err) {
if (err) {
console.log('error when connecting to db:', err);
console.log('reconnecting, this is normal');
setTimeout(handleDisconnect, 2000);
}
});
con.on('error', function (err) {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
}
function millisToSeconds(millis) {
return (millis / 1000).toFixed(0);
}
function secondsToDhms(seconds) {
seconds = Number(seconds);
var d = Math.floor(seconds / (3600*24));
var h = Math.floor(seconds % (3600*24) / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 60);
var dDisplay = d > 0 ? d + (d == 1 ? " day" : " days") : "";
var hDisplay = h > 0 ? h + (h == 1 ? " hour" : " hours") : "";
var mDisplay = m > 0 ? m + (m == 1 ? " minute" : " minutes") : "";
var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
return [dDisplay, hDisplay, mDisplay, sDisplay]
}
function do404(req, res, next) {
res.type('text/html').status(404).sendFile(__dirname + '/errors/404.html');
}
function do500(err, req, res, text) {
if(err.message != 'error is not defined') console.log(err.stack)
res.type('text/html').status(500).sendFile(__dirname + '/errors/500.html');
}
// actual code
if(database_USER == "" || database_NAME == "" || database_HOST == "") {
}
else {
handleDisconnect();
}
const logger = (req, res, next)=>{
if(startupArgs.includes('--log')) {
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(`REQUEST FROM: ${ip}\n-----------------------------------`);
};
next();
}
app.use(logger);
app.use(express.static('public'));
app.use(bodyParser.json({extended: false}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(session({ secret: sessionSecret, store: new session.MemoryStore(), expires: new Date(Date.now() + 604800), resave: false, saveUninitialized: false }));
// API
app.get('/api/punishments', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
let bans = [];
let sql = `SELECT * FROM \`Punishments\` ORDER BY start DESC`;
con.query(sql, async (err, result) => {
if(result.length < 1) {
bans[0] = 'There are no active punishments.';
} else {
result.forEach(async punishment => {
let ends = '';
let seconds = millisToSeconds(punishment.end) - Math.round(Date.now() / 1000);
let a = {
id: punishment.id,
user: punishment.name,
admin: punishment.operator,
type: punishment.type,
reason: punishment.reason,
start: punishment.start,
ends: punishment.end
}
bans.push(a);
});
}
let out = bans;
res.status(200).json(out);
});
});
app.get('/api/punishments/:id', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
if(isNaN(req.params.id)) return do404(req, res);
let sql = `SELECT * FROM \`Punishments\` WHERE id=${req.params.id}`;
con.query(sql, async (err, result) => {
if(err) throw err;
let bans = [];
if(result.length < 1) return do404(req, res);
else {
result.forEach(async punishment => {
let a = {
id: punishment.id,
user: punishment.name,
admin: punishment.operator,
type: punishment.type,
reason: punishment.reason,
start: punishment.start,
ends: punishment.end
}
bans.push(a);
});
}
let out = bans;
res.status(200).json(out);
});
});
app.get('/api/punishments/history', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
let bans = [];
let sql = `SELECT * FROM \`PunishmentHistory\` ORDER BY start DESC`;
con.query(sql, async (err, result) => {
if(result.length < 1) {
bans[0] = 'There are no active punishments.';
} else {
result.forEach(async punishment => {
let ends = '';
let seconds = millisToSeconds(punishment.end) - Math.round(Date.now() / 1000);
let a = {
id: punishment.id,
user: punishment.name,
admin: punishment.operator,
type: punishment.type,
reason: punishment.reason,
start: punishment.start,
ends: punishment.end
}
bans.push(a);
});
}
let out = bans;
res.status(200).json(out);
});
});
app.get('/api/punishments/history/:id', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
if(isNaN(req.params.id)) return do404(req, res);
let sql = `SELECT * FROM \`PunishmentHistory\` WHERE id=${req.params.id}`;
con.query(sql, async (err, result) => {
if(err) throw err;
let bans = [];
if(result.length < 1) return do404(req, res);
else {
result.forEach(async punishment => {
let a = {
id: punishment.id,
user: punishment.name,
admin: punishment.operator,
type: punishment.type,
reason: punishment.reason,
start: punishment.start,
ends: punishment.end
}
bans.push(a);
});
}
let out = bans;
res.status(200).json(out);
});
});
// Web pages
app.get('/', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
else return res.redirect('/punishments');
});
app.get(`/admin/setup`, async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "") return res.redirect('/admin/setup/database');
else return res.redirect('/admin/setup/config');
})
app.get('/admin/setup/database', async (req, res) => {
let err = ``;
if(database_USER == "" || database_NAME == "" || database_HOST == "") {
} else {
return res.redirect('/admin/setup/config');
}
if(req.session.error) {
err = `<div class="bg-red-600 py-4 px-4 rounded">${req.session.error}</div>`;
req.session.error = undefined;
}
let out = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Setup</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<div class="container max-w-full mx-auto py-24 px-6">
<div class="max-w-sm mx-auto px-6">
<div class="relative flex flex-wrap">
<div class="w-full relative">
<div class="md:mt-6">
<div class="text-center font-semibold text-black">
Database Setup
</div>
<form action="" method="POST" class="mt-8">
${err}
<div class="mx-auto max-w-lg ">
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Host<span class="text-sm text-red-700"><b>*</b></span></span>
<input placeholder="" name="host" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Database<span class="text-sm text-red-700"><b>*</b></span></span>
<input placeholder="" name="database" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Username<span class="text-sm text-red-700"><b>*</b></span></span>
<input placeholder="" name="user" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Password<span class="text-sm text-red-700"><b>*</b></span></span>
<input placeholder="" name="password" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<button class="mt-3 text-lg font-semibold
bg-gray-800 w-full text-white rounded-lg
px-6 py-3 block shadow-xl hover:text-white hover:bg-black">
Connect
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>`
res.send(out);
})
app.post('/admin/setup/database', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || completed) {
} else {
return res.redirect('/admin/setup/config');
}
let db = req.body;
let connection = mysql.createConnection(db);
connection.connect(async (err) => {
if(err) {
if(err.code == "ETIMEDOUT") {
req.session.error = 'MySQL Error: Invalid Host';
res.redirect('/admin/setup/database');
} else {
req.session.error = `MySQL Error: ${err.sqlMessage}`;
res.redirect('/admin/setup/database');
}
connection.end();
} else {
let db_towrite = `{
"database_HOST": "${req.body.host}",
"database_USER": "${req.body.user}",
"database_PASS": "${req.body.password}",
"database_NAME": "${req.body.database}"
}`
fs.writeFileSync('db_info.json', db_towrite, {encoding: 'utf-8'});
res.redirect('/admin/setup/config');
connection.end();
}
})
})
app.get('/admin/setup/config', async (req, res) => {
if(completed) return res.redirect('/punishments');
let err = ``;
if(serverName == "") {
} else {
return res.redirect('/');
}
if(req.session.error) {
err = `<div class="bg-red-600 py-4 px-4 rounded">${req.session.error}</div>`;
req.session.error = undefined;
}
let out = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Setup</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<div class="container max-w-full mx-auto py-24 px-6">
<div class="max-w-sm mx-auto px-6">
<div class="relative flex flex-wrap">
<div class="w-full relative">
<div class="md:mt-6">
<div class="text-center font-semibold text-black">
Configuration Setup
</div>
<form action="" method="POST" class="mt-8">
${err}
<div class="mx-auto max-w-lg ">
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Server Name</span>
<input placeholder="" name="serverName" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Hostname</span>
<input placeholder="" name="hostName" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<div class="py-1">
<span class="px-1 text-sm text-gray-600">Port (to host the webserver on, default 3000)<span class="text-sm text-red-700"><b>*</b></span></span>
<input placeholder="" value="3000" onkeypress="return isNumberKey(event)" id="port" name="port" type="text"
class="text-md block px-3 py-2 rounded-lg w-full
bg-white border-2 border-gray-300 placeholder-gray-600 shadow-md focus:placeholder-gray-500 focus:bg-white focus:border-gray-600 focus:outline-none">
</div>
<button class="mt-3 text-lg font-semibold
bg-gray-800 w-full text-white rounded-lg
px-6 py-3 block shadow-xl hover:text-white hover:bg-black">
Continue
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode
console.log(evt)
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
if(Number(evt.value) < 65535) return false;
return true;
}
</script>
</body>
</html>`
res.send(out);
})
app.post('/admin/setup/config', async (req, res) => {
if(completed) return res.redirect('/punishments');
let config_towrite = `{
"serverName": "${req.body.serverName}",
"serverHost": "${req.body.hostName}",
"port": ${req.body.port},
"completedDontTouchThis": true
}`
fs.writeFileSync('config.json', config_towrite, {encoding: 'utf-8'});
res.redirect('/admin/setup/done');
})
app.get('/admin/setup/done', async (req, res) => {
if(completed) return res.redirect('/');
let out = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Setup</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<div class="container max-w-full pt-24 px-6">
<div class="max-w-sm mx-auto px-1/2">
<div class="relative flex flex-wrap">
<div class="w-full relative">
<div class="md:mt-6">
<p class="text-xl">Setup is complete. Please restart the script for changes to take effect.</p>
</div>
</div>
</div>
</div>
</div>
<script>function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : evt.keyCode
console.log(evt)
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
if(Number(evt.value) < 65535) return false;
return true;
}
</script>
</body>
</html>`
await res.send(out);
process.exit(0);
})
app.get('/punishments', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
let bans = [];
let sql = `SELECT * FROM \`Punishments\` ORDER BY start DESC`;
con.query(sql, async (err, result) => {
if(result.length < 1) {
bans[0] = 'There are no active punishments.';
} else {
result.forEach(async punishment => {
let ends = '';
let seconds = millisToSeconds(punishment.end) - Math.round(Date.now() / 1000);
if(punishment.end == '-1') {
if(punishment.punishmentType == "KICK") ends = `<td class="border px-4 py-2"><span class="text-green-700"><b>Ended</b></span></td>`;
else ends = '<td class="border px-4 py-2 text-red-700"><b>Permanent</b></td>';
}
else {
let untilEnds = secondsToDhms(seconds);
let i;
if(untilEnds[0] !== "" && untilEnds[1] !== "") i = `${untilEnds[0]}, ${untilEnds[1]}`;
else if(untilEnds[0] !== "") i = untilEnds[0];
else if(untilEnds[1] !== "") i = untilEnds[1];
else if(untilEnds[2] !== "") i = untilEnds[2];
else if(untilEnds[3] !== "") i = untilEnds[3];
else i = `<span class="text-green-700"><b>Ended</b></span>`
ends = `<td class="border px-4 py-2">${i}</td>`;
}
let type = '';
if(punishment.punishmentType == 'IP_BAN') type = `<td class="border px-4 py-2 text-red-800"><b>IP Ban</b></td>`;
else if(punishment.punishmentType == 'BAN') type = `<td class="border px-4 py-2 text-red-700"><b>Ban</b></td>`;
else if(punishment.punishmentType == 'TEMP_BAN') type = `<td class="border px-4 py-2 text-red-600"><b>Temp-Ban</b></td>`;
else if(punishment.punishmentType == 'WARNING') type = `<td class="border px-4 py-2 text-orange-700"><b>Warning</b></td>`;
else if(punishment.punishmentType == 'TEMP_WARNING') type = `<td class="border px-4 py-2 text-orange-600"><b>Temp-Warning</b></td>`;
else if(punishment.punishmentType == 'KICK') type = `<td class="border px-4 py-2 text-yellow-600"><b>Kick</b></td>`;
else if(punishment.punishmentType == 'TEMP_MUTE') type = `<td class="border px-4 py-2 text-yellow-600"><b>Temp-Mute</b></td>`;
else if(punishment.punishmentType == 'MUTE') type = `<td class="border px-4 py-2 text-red-700"><b>Mute</b></td>`;
else type = `<td class="border px-4 py-2 text-yellow-600"><b>${punishment.punishmentType}</b></td>`;
bans.push(`
<tr>
<td class="border px-4 py-2"><a href="/punishments/${punishment.id}">${punishment.name}</a></td>
<td class="border px-4 py-2">${punishment.operator}</td>
${type}
<td class="border px-4 py-2">${punishment.reason}</td>
<td class="border px-4 py-2">${timeago(punishment.start)}</td>
${ends}
</tr>`);
});
}
bans = bans.join('')
let title = '';
let hostname = '';
if(serverName != "") title = ` - ${serverName}`;
if(serverHost != "" && serverName != "") hostname = ` - ${serverHost}`;
else if(serverHost != "" && servername == "") hostname = serverHost;
let out = `
<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Punishments${title}</title>
</head>
<body class="overflow-x-hidden">
<div class="flex">
<div class="overflow-hidden shadow-md border-t-4 bg-white mb-4 rounded-b-lg rounded-t border-red-light w-full md:w-1/4">
<div class="px-6 py-4 mb-2 mt-4 mb-8">
<div class="uppercase tracking-wide text-c2 mb-4">${serverName}${hostname}</div>
<div onclick="window.location.href='/punishments'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest border-b-0" style="border-left: 4px solid #e2624b !important;">
<img width="18" height="18" src="/svg/sledgehammer.svg" />
<div class="pl-2">Punishments</div>
</div>
<div onclick="window.location.href='/punishments/history'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest">
<svg width="18" height="18" viewBox="0 0 2048 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M640 896v512h-256v-512h256zm384-512v1024h-256v-1024h256zm1024 1152v128h-2048v-1536h128v1408h1920zm-640-896v768h-256v-768h256zm384-384v1152h-256v-1152h256z"
/>
</svg>
<div class="pl-2">History</div>
</div>
<input type="search" id="search" onkeyup="search()" class="flex text-lg w-full bg-purple-white shadow rounded border-0 px-6 py-4 mb-2 mt-4 mb-8" placeholder="Search">
</div>
</div>
<table id="results" class="table-fixed w-full align-end">
<thead>
<tr>
<th class="w-1/2 px-4 py-2">Player</th>
<th class="w-1/4 px-4 py-2">Operator</th>
<th class="w-1/4 px-4 py-2">Type</th>
<th class="w-1/4 px-4 py-2">Reason</th>
<th class="w-1/4 px-4 py-2">Created</th>
<th class="w-1/4 px-4 py-2">Ends</th>
</tr>
</thead>
<tbody>
${bans}
</tbody>
</table>
<script>
function search() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("search");
filter = input.value.toUpperCase();
table = document.getElementById("results");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</body>
</div>
</html>
`;
res.send(out);
})
});
app.get('/punishments/history', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
let bans = [];
let sql = `SELECT * FROM \`PunishmentHistory\` ORDER BY start DESC`;
con.query(sql, async (err, result) => {
if(result.length < 1) {
bans[0] = 'There are no active punishments.';
} else {
result.forEach(async punishment => {
let ends = '';
let seconds = millisToSeconds(punishment.end) - Math.round(Date.now() / 1000);
if(punishment.end == '-1') {
if(punishment.punishmentType == "KICK") ends = `<td class="border px-4 py-2"><span class="text-green-700"><b>Ended</b></span></td>`;
else ends = '<td class="border px-4 py-2 text-red-700"><b>Permanent</b></td>';
}
else {
let untilEnds = secondsToDhms(seconds);
let i;
if(untilEnds[0] !== "" && untilEnds[1] !== "") i = `${untilEnds[0]}, ${untilEnds[1]}`;
else if(untilEnds[0] !== "") i = untilEnds[0];
else if(untilEnds[1] !== "") i = untilEnds[1];
else if(untilEnds[2] !== "") i = untilEnds[2];
else if(untilEnds[3] !== "") i = untilEnds[3];
else i = `<span class="text-green-700"><b>Ended</b></span>`
ends = `<td class="border px-4 py-2">${i}</td>`;
}
let type = '';
if(punishment.punishmentType == 'IP_BAN') type = `<td class="border px-4 py-2 text-red-800"><b>IP Ban</b></td>`;
else if(punishment.punishmentType == 'BAN') type = `<td class="border px-4 py-2 text-red-700"><b>Ban</b></td>`;
else if(punishment.punishmentType == 'TEMP_BAN') type = `<td class="border px-4 py-2 text-red-600"><b>Temp-Ban</b></td>`;
else if(punishment.punishmentType == 'WARNING') type = `<td class="border px-4 py-2 text-orange-700"><b>Warning</b></td>`;
else if(punishment.punishmentType == 'TEMP_WARNING') type = `<td class="border px-4 py-2 text-orange-600"><b>Temp-Warning</b></td>`;
else if(punishment.punishmentType == 'KICK') type = `<td class="border px-4 py-2 text-yellow-600"><b>Kick</b></td>`;
else if(punishment.punishmentType == 'TEMP_MUTE') type = `<td class="border px-4 py-2 text-yellow-600"><b>Temp-Mute</b></td>`;
else if(punishment.punishmentType == 'MUTE') type = `<td class="border px-4 py-2 text-red-700"><b>Mute</b></td>`;
else type = `<td class="border px-4 py-2 text-yellow-600"><b>${punishment.punishmentType}</b></td>`;
bans.push(`
<tr>
<td class="border px-4 py-2"><a href="/punishments/history/${punishment.id}">${punishment.name}</a></td>
<td class="border px-4 py-2">${punishment.operator}</td>
${type}
<td class="border px-4 py-2">${punishment.reason}</td>
<td class="border px-4 py-2">${timeago(punishment.start)}</td>
${ends}
</tr>`);
});
}
bans = bans.join('')
let title = '';
let hostname = '';
if(serverName != "") title = ` - ${serverName}`;
if(serverHost != "" && serverName != "") hostname = ` - ${serverHost}`;
else if(serverHost != "" && servername == "") hostname = serverHost;
let out = `
<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Punishments${title}</title>
</head>
<body class="overflow-x-hidden">
<div class="flex">
<div class="overflow-hidden shadow-md border-t-4 bg-white mb-4 rounded-b-lg rounded-t border-red-light w-full md:w-1/4">
<div class="px-6 py-4 mb-2 mt-4 mb-8">
<div class="uppercase tracking-wide text-c2 mb-4">${serverName}${hostname}</div>
<div onclick="window.location.href='/punishments'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest border-b-0">
<img width="18" height="18" src="/svg/sledgehammer.svg" />
<div class="pl-2">Punishments</div>
</div>
<div onclick="window.location.href='/punishments/history'" style="border-left: 4px solid #e2624b !important;" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest">
<svg width="18" height="18" viewBox="0 0 2048 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M640 896v512h-256v-512h256zm384-512v1024h-256v-1024h256zm1024 1152v128h-2048v-1536h128v1408h1920zm-640-896v768h-256v-768h256zm384-384v1152h-256v-1152h256z"
/>
</svg>
<div class="pl-2">History</div>
</div>
<input type="search" id="search" onkeyup="search()" class="flex text-lg w-full bg-purple-white shadow rounded border-0 px-6 py-4 mb-2 mt-4 mb-8" placeholder="Search">
</div>
</div>
<table id="results" class="table-fixed w-full align-end">
<thead>
<tr>
<th class="w-1/2 px-4 py-2">Player</th>
<th class="w-1/4 px-4 py-2">Operator</th>
<th class="w-1/4 px-4 py-2">Type</th>
<th class="w-1/4 px-4 py-2">Reason</th>
<th class="w-1/4 px-4 py-2">Created</th>
<th class="w-1/4 px-4 py-2">Ends</th>
</tr>
</thead>
<tbody>
${bans}
</tbody>
</table>
</div>
<script>
function search() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById("search");
filter = input.value.toUpperCase();
table = document.getElementById("results");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>
</body>
</div>
</html>
`;
res.send(out);
})
});
// Individual Punishments
app.get('/punishments/:id', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
if(isNaN(req.params.id)) return do404(req, res);
let sql = `SELECT * FROM \`Punishments\` WHERE id=${req.params.id}`;
con.query(sql, async (err, result) => {
if(err) throw err;
if(result.length < 1) return do404(req, res);
else {
let ban = []
result.forEach(async punishment => {
let ends = '';
let seconds = millisToSeconds(punishment.end) - Math.round(Date.now() / 1000);
if(punishment.end == '-1') {
if(punishment.punishmentType == "KICK") ends = `<td class="border px-4 py-2"><span class="text-green-700"><b>Ended</b></span></td>`;
else ends = '<td class="border px-4 py-2 text-red-700"><b>Permanent</b></td>';
}
else {
let untilEnds = secondsToDhms(seconds);
let i;
if(untilEnds[0] !== "" && untilEnds[1] !== "") i = `${untilEnds[0]}, ${untilEnds[1]}`;
else if(untilEnds[0] !== "") i = untilEnds[0];
else if(untilEnds[1] !== "") i = untilEnds[1];
else if(untilEnds[2] !== "") i = untilEnds[2];
else if(untilEnds[3] !== "") i = untilEnds[3];
else i = `<span class="text-green-700"><b>Ended</b></span>`
ends = `<td class="border px-4 py-2">${i}</td>`;
}
let type = '';
if(punishment.punishmentType == 'IP_BAN') type = `<td class="border px-4 py-2 text-red-800"><b>IP Ban</b></td>`;
else if(punishment.punishmentType == 'BAN') type = `<td class="border px-4 py-2 text-red-700"><b>Ban</b></td>`;
else if(punishment.punishmentType == 'TEMP_BAN') type = `<td class="border px-4 py-2 text-red-600"><b>Temp-Ban</b></td>`;
else if(punishment.punishmentType == 'WARNING') type = `<td class="border px-4 py-2 text-orange-700"><b>Warning</b></td>`;
else if(punishment.punishmentType == 'TEMP_WARNING') type = `<td class="border px-4 py-2 text-orange-600"><b>Temp-Warning</b></td>`;
else if(punishment.punishmentType == 'KICK') type = `<td class="border px-4 py-2 text-yellow-600"><b>Kick</b></td>`;
else type = `<td class="border px-4 py-2 text-yellow-600"><b>${punishment.punishmentType}</b></td>`;
ban.push(`
<tr>
<td class="border px-4 py-2"><a href="/punishments/${punishment.id}">${punishment.name}</a></td>
<td class="border px-4 py-2">${punishment.operator}</td>
${type}
<td class="border px-4 py-2">${punishment.reason}</td>
<td class="border px-4 py-2">${timeago(punishment.start)}</td>
${ends}
</tr>`);
});
ban.join('')
let title = '';
let hostname = '';
if(serverName != "") title = ` - ${serverName}`;
if(serverHost != "" && serverName != "") hostname = ` - ${serverHost}`;
else if(serverHost != "" && servername == "") hostname = serverHost;
let out = `
<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Punishment${title}</title>
</head>
<body class="overflow-x-hidden">
<div class="flex">
<div class="overflow-hidden shadow-md border-t-4 bg-white mb-4 rounded-b-lg rounded-t border-red-light w-full md:w-1/4">
<div class="px-6 py-4 mb-2 mt-4 mb-8">
<div class="uppercase tracking-wide text-c2 mb-4">${serverName}${hostname}</div>
<div onclick="window.location.href='/punishments'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest border-b-0">
<img width="18" height="18" src="/svg/sledgehammer.svg" />
<div class="pl-2">Punishments</div>
</div>
<div onclick="window.location.href='/punishments/history'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest">
<svg width="18" height="18" viewBox="0 0 2048 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M640 896v512h-256v-512h256zm384-512v1024h-256v-1024h256zm1024 1152v128h-2048v-1536h128v1408h1920zm-640-896v768h-256v-768h256zm384-384v1152h-256v-1152h256z"
/>
</svg>
<div class="pl-2">History</div>
</div>
</div>
</div>
<table id="results" class="table-fixed w-full align-end">
<thead>
<tr>
<th class="w-1/2 px-4 py-2">Player</th>
<th class="w-1/4 px-4 py-2">Operator</th>
<th class="w-1/4 px-4 py-2">Type</th>
<th class="w-1/4 px-4 py-2">Reason</th>
<th class="w-1/4 px-4 py-2">Created</th>
<th class="w-1/4 px-4 py-2">Ends</th>
</tr>
</thead>
<tbody>
${ban}
</tbody>
</table>
</div>
</body>
</div>
</html>`
res.send(out)
};
})
});
app.get('/punishments/history/:id', async (req, res) => {
if(database_USER == "" || database_NAME == "" || database_HOST == "" || !completed) return res.redirect('/admin/setup');
if(isNaN(req.params.id)) return do404(req, res);
let sql = `SELECT * FROM \`PunishmentHistory\` WHERE id=${req.params.id}`;
con.query(sql, async (err, result) => {
if(err) throw err;
if(result.length < 1) return do404(req, res);
else {
let ban = []
result.forEach(async punishment => {
let ends = '';
let seconds = millisToSeconds(punishment.end) - Math.round(Date.now() / 1000);
if(punishment.end == '-1') {
if(punishment.punishmentType == "KICK") ends = `<td class="border px-4 py-2"><span class="text-green-700"><b>Ended</b></span></td>`;
else ends = '<td class="border px-4 py-2 text-red-700"><b>Permanent</b></td>';
}
else {
let untilEnds = secondsToDhms(seconds);
let i;
if(untilEnds[0] !== "" && untilEnds[1] !== "") i = `${untilEnds[0]}, ${untilEnds[1]}`;
else if(untilEnds[0] !== "") i = untilEnds[0];
else if(untilEnds[1] !== "") i = untilEnds[1];
else if(untilEnds[2] !== "") i = untilEnds[2];
else if(untilEnds[3] !== "") i = untilEnds[3];
else i = `<span class="text-green-700"><b>Ended</b></span>`
ends = `<td class="border px-4 py-2">${i}</td>`;
}
let type = '';
if(punishment.punishmentType == 'IP_BAN') type = `<td class="border px-4 py-2 text-red-800"><b>IP Ban</b></td>`;
else if(punishment.punishmentType == 'BAN') type = `<td class="border px-4 py-2 text-red-700"><b>Ban</b></td>`;
else if(punishment.punishmentType == 'TEMP_BAN') type = `<td class="border px-4 py-2 text-red-600"><b>Temp-Ban</b></td>`;
else if(punishment.punishmentType == 'WARNING') type = `<td class="border px-4 py-2 text-orange-700"><b>Warning</b></td>`;
else if(punishment.punishmentType == 'TEMP_WARNING') type = `<td class="border px-4 py-2 text-orange-600"><b>Temp-Warning</b></td>`;
else if(punishment.punishmentType == 'KICK') type = `<td class="border px-4 py-2 text-yellow-600"><b>Kick</b></td>`;
else type = `<td class="border px-4 py-2 text-yellow-600"><b>${punishment.punishmentType}</b></td>`;
ban.push(`
<tr>
<td class="border px-4 py-2"><a href="/punishments/history/${punishment.id}">${punishment.name}</a></td>
<td class="border px-4 py-2">${punishment.operator}</td>
${type}
<td class="border px-4 py-2">${punishment.reason}</td>
<td class="border px-4 py-2">${timeago(punishment.start)}</td>
${ends}
</tr>`);
});
ban.join('')
let title = '';
let hostname = '';
if(serverName != "") title = ` - ${serverName}`;
if(serverHost != "" && serverName != "") hostname = ` - ${serverHost}`;
else if(serverHost != "" && servername == "") hostname = serverHost;
let out = `
<!DOCTYPE html>
<html>
<head>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Punishment${title}</title>
</head>
<body class="overflow-x-hidden">
<div class="flex">
<div class="overflow-hidden shadow-md border-t-4 bg-white mb-4 rounded-b-lg rounded-t border-red-light w-full md:w-1/4">
<div class="px-6 py-4 mb-2 mt-4 mb-8">
<div class="uppercase tracking-wide text-c2 mb-4">${serverName}${hostname}</div>
<div onclick="window.location.href='/punishments'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest border-b-0">
<img width="18" height="18" src="/svg/sledgehammer.svg" />
<div class="pl-2">Punishments</div>
</div>
<div onclick="window.location.href='/punishments/history'" class="flex cursor-pointer border px-4 py-2 text-lg text-grey-darkest">
<svg width="18" height="18" viewBox="0 0 2048 1792" xmlns="http://www.w3.org/2000/svg">
<path d="M640 896v512h-256v-512h256zm384-512v1024h-256v-1024h256zm1024 1152v128h-2048v-1536h128v1408h1920zm-640-896v768h-256v-768h256zm384-384v1152h-256v-1152h256z"
/>
</svg>
<div class="pl-2">History</div>
</div>
</div>
</div>
<table id="results" class="table-fixed w-full align-end">
<thead>
<tr>
<th class="w-1/2 px-4 py-2">Player</th>
<th class="w-1/4 px-4 py-2">Operator</th>
<th class="w-1/4 px-4 py-2">Type</th>
<th class="w-1/4 px-4 py-2">Reason</th>
<th class="w-1/4 px-4 py-2">Created</th>
<th class="w-1/4 px-4 py-2">Ends</th>
</tr>
</thead>
<tbody>
${ban}
</tbody>
</table>
</div>
</body>
</div>
</html>`
res.send(out)
};
})
});
app.get('/error/500',(req, res)=>res.send(error()));
app.use( async(err, req, res, text) => do500(err, req, res, text))
app.use( async(req, res, next) => do404(req, res, next))
http.listen(port);