This repository has been archived by the owner on Mar 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
493 lines (450 loc) · 13.9 KB
/
app.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
var express = require('express');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var flash = require('connect-flash');
var exphbs = require('express-handlebars');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var app = express();
var promise = require('promise');
var rp = require('request-promise-native');
var Keycloak = require('keycloak-connect');
const url = require('url');
var fs = require('fs');
var config = JSON.parse(fs.readFileSync('config/config.json', 'utf8'));
var pool = mysql.createPool({
connectionLimit: config.mysql_limit,
host: config.mysql_host,
user: config.mysql_user,
database: config.mysql_db,
password: config.mysql_password
});
app.use(bodyParser.urlencoded({
extended: false
}))
// parse application/json
app.use(bodyParser.json())
app.use(cookieParser());
var memoryStore = new session.MemoryStore();
app.use(session({
secret: config.session_secret,
saveUninitialized: false,
resave: false,
store: memoryStore
}));
var keycloak = new Keycloak({store: memoryStore});
app.use(keycloak.middleware());
app.use(flash()); // use connect-flash for flash messages stored in session
app.use(express.static(__dirname + '/public'));
app.use("/player", express.static(__dirname + '/public'));
app.use("/book", express.static(__dirname + '/public'));
require('./handlebars.js')(exphbs, app, keycloak);
app.set('view engine', 'handlebars');
var User = new(require("./modules/user.js"))(pool);
var Logger = new(require("./modules/logger.js"))(pool, keycloak);
require("./routes/user.js")(app, User, pool, keycloak);
require("./routes/logs.js")(app, pool);
require("./routes/books.js")(app, pool, Logger, keycloak);
require("./routes/serverlog.js")(app, config.log_path, keycloak);
_running_update = false;
app.use(function(req,res,next){
res.locals.kauth = req.kauth;
next();
})
app.use("/", keycloak.protect("access"));
app.get("/view-logs",function(req,res) {
res.render("viewlog");
});
app.get("/view-serverlogs", keycloak.protect("view_logs"), function(req,res) {
res.render("viewserverlog");
});
app.get("/saverestore", keycloak.protect("restore_saves"), function(req,res) {
res.render("restoresave");
});
app.get('/', function (req, res) {
var flash = req.flash();
res.render("dashboard");
});
app.use('/book', keycloak.protect('manage_books'));
app.get('/book', function (req, res) {
res.render("booksearch");
});
app.options('/book/:key');
app.get('/book/:key', function (req, res) {
if (!req.params.key) {
res.redirect("/book");
} else {
if (req.params.key)
res.render("book", {
id: req.params.key,
});
}
});
app.get('/player', keycloak.protect('view_players'), function (req, res) {
res.render("playersearch");
});
app.options('/player/:key');
app.get('/player/:key', keycloak.protect('view_players'), function (req, res) {
if (!req.params.key) {
res.redirect("/player");
} else {
if (req.params.key)
res.render("player", {
ckey: req.params.key,
});
}
});
function monitorRequest(method, endpoint, body) {
if (!body)
body = {}
return rp({
uri: config.monitor_url + "/" + endpoint,
json: true,
method: method,
formData: body,
auth: {
user: "auth",
pass: config.monitor_key
}
});
}
var isRunning = function () {
return monitorRequest("GET", "is_running").then((res) => {
return {server: res.message}
}).catch((_) => {
return {server: false}
});
};
app.get('/is-server-alive', function (req, res) {
isRunning().then((data) => {
res.send(data);
});
});
app.get('/start-server', keycloak.protect('manage_server'), async function(req, res) {
const { server } = await isRunning()
if (server)
return res.send({ success: false, message: 'Server is already running.' })
const monitor = await monitorRequest('POST', 'start').catch(error => error)
if (monitor instanceof Error)
return res.send({ success: false, message: 'Unable to start server.' })
Logger.log(req, '%USER% has started the server.')
return res.send(monitor)
})
app.get('/stop-server', keycloak.protect('manage_server'), function(req,res) {
isRunning().then(function(data) {
if(!data.server)
{
res.send({success:false, message:"Server is already stopped.."});
return;
}
Logger.log(req, "%USER% has stopped the server.")
return monitorRequest("POST", "stop").then((monRes) => {
res.send(monRes);
});
});
});
app.get('/current-commit', function (req, res) {
monitorRequest("GET", "commit").then((monRes) => {
res.send(monRes.message);
});
});
app.get('/update-server', keycloak.protect('manage_server'), function (req, res) {
if(_running_update)
{
res.send({success:false,message:"Server is already being updated"});
return;
}
isRunning().then(function(data) {
if(data.server)
{
res.send({success:false,message:"Stop the server first.."});
return;
}
_running_update = true;
Logger.log(req, "%USER% has updated the server.")
return monitorRequest("POST", "update").then((monRes) => {
res.send(monRes);
_running_update = false;
});
});
});
app.post('/restore-save', keycloak.protect('restore_saves'), function (req, res) {
if (!req.body.ckey || !req.body.date) {
res.send({
success: false,
message: "Missing arguments (date or key)"
});
return;
}
monitorRequest("POST", "restoresave", {ckey: req.body.ckey, date: req.body.date}).then((monRes) => {
res.send(monRes);
});
});
app.post('/get-byondaccount', keycloak.protect('view_players'), function (req, res) {
if (!req.body.ckey) {
res.send({
success: false,
message: "No key provided"
});
return;
};
var request = require('request');
request('http://www.byond.com/members/' + req.body.ckey + "?format=text", function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = {};
var lines = body.split(/\r?\n/);
lines.forEach(function(entry) {
console.log(entry);
if(entry.includes("=")) {
var split = entry.split("=");
var key = split[0].trim().replace(/\"/g,"");
var value = split[1].trim().replace(/\"/g,"");
data[key] = value;
};
});
res.send(JSON.stringify(data));
};
});
});
app.post('/get-statistics', keycloak.protect('view_players'), function (req, res) {
if (!req.body.key) {
res.send({
success: false,
message: "No key provided"
});
return;
}
pool.getConnection(function (err, connection) {
if (err) {
res.send({
success: false,
message: err
});
return;
}
// TODO LOWERCASE AND REMOVE SPACES
connection.query('SELECT `firstseen`, `lastseen`, `ip`, `computerid`, (SELECT COUNT(*) FROM `whitelist` WHERE `ckey` = ?) AS `whitelist_count`, (SELECT COUNT(*) FROM `erro_ban` WHERE `ckey` = ? AND `expiration_time` > NOW() AND (unbanned IS NULL)) AS `ban_count` FROM `erro_player` WHERE `ckey` = ?', [req.body.key, req.body.key, req.body.key], function (err, rows) {
connection.release();
if (err) {
res.send({
success: false,
message: err
});
return;
}
if (rows.count <= 0) {
res.send({
success: false,
message: "cancer"
});
return;
}
var row = rows[0];
if (!row) {
res.send({
success: false,
message: "cancer"
});
return;
}
res.send({
success: true,
firstseen: row.firstseen,
lastseen: row.lastseen,
cid: row.computerid,
ip: row.ip,
whitelist_count: row.whitelist_count,
ban_count: row.ban_count,
});
});
});
});
app.post('/get-location', keycloak.protect('view_players'), function (req, res) {
var request = require('request');
request('http://www.freegeoip.net/json/' + req.body.ip, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.send(body);
}
})
});
app.post('/get-bans', keycloak.protect('view_players'), function (req, res) {
if (!req.body.key) {
res.send({
success: false,
message: "No key provided"
});
return;
}
pool.getConnection(function (err, connection) {
if (err) {
res.send({
success: false,
message: err
});
return;
}
// TODO LOWERCASE AND REMOVE SPACES
connection.query('SELECT * FROM `erro_ban` WHERE ckey = ? ORDER BY bantime DESC', [req.body.key], function (err, rows) {
connection.release();
if (err) {
res.send({
success: false,
message: err
});
return;
}
if (rows.count <= 0) {
res.send({
success: false,
message: "cancer"
});
return;
}
res.send({
success: true,
rows: rows
});
});
});
});
app.post('/get-whitelist', keycloak.protect('view_players'), function (req, res) {
if (!req.body.key) {
res.send({
success: false,
message: "No key provided"
});
return;
}
pool.getConnection(function (err, connection) {
if (err) {
res.send({
success: false,
message: err
});
return;
}
// TODO LOWERCASE AND REMOVE SPACES
connection.query('SELECT * FROM `whitelist` WHERE ckey = ?', [req.body.key], function (err, rows) {
connection.release();
if (err) {
res.send({
success: false,
message: err
});
return;
}
if (rows.count <= 0) {
res.send({
success: false,
message: "cancer"
});
return;
}
res.send({
success: true,
rows: rows
});
});
});
});
app.post('/remove-whitelist', keycloak.protect('manage_players'), function (req, res) {
if (!req.body.key) {
res.send({
success: false,
message: "No key provided"
});
return;
}
if (!req.body.race) {
res.send({
success: false,
message: "No race provided"
});
return;
}
Logger.log(req, "%USER% has removed " + req.body.key + "'s whitelist for " + req.body.race)
pool.getConnection(function (err, connection) {
if (err) {
res.send({
success: false,
message: err
});
return;
}
req.body.key = req.body.key.toLowerCase();
req.body.race = req.body.race.toLowerCase();
connection.query('DELETE FROM `whitelist` WHERE ckey = ? AND race = ?', [req.body.key, req.body.race], function (err, rows) {
connection.release();
if (err) {
res.send({
success: false,
message: err
});
return;
}
res.send({
success: true
});
});
});
});
app.post('/add-whitelist', keycloak.protect('manage_players'), function (req, res) {
if (!req.body.key) {
res.send({
success: false,
message: "No key provided"
});
return;
}
if (!req.body.race) {
res.send({
success: false,
message: "No race provided"
});
return;
}
Logger.log(req, "%USER% has whitelisted " + req.body.key + " for " + req.body.race)
req.body.key = req.body.key.toLowerCase();
req.body.race = req.body.race.toLowerCase();
pool.getConnection(function (err, connection) {
if (err) {
res.send({
success: false,
message: err
});
return;
}
// TODO LOWERCASE AND REMOVE SPACES
connection.query('INSERT INTO `whitelist` (ckey,race) VALUES (?,?)', [req.body.key, req.body.race], function (err, rows) {
connection.release();
if (err) {
res.send({
success: false,
message: err
});
return;
}
res.send({
success: true
});
});
});
});
if(config.https_enabled)
{
const https = require('https');
const options = {
key: fs.readFileSync(config.https_key),
cert: fs.readFileSync(config.https_cert)
};
https.createServer(options, app).listen(config.https_port);
console.log('Example app listening at https')
}
if(config.http_enabled)
{
var server = app.listen(config.http_port, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
}