This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
802 lines (774 loc) · 27.7 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
// arkham-revived
// Licensed under the MIT License
// Copyright (c) 2023 KiwifruitDev
// Imports
const express = require("express");
const fse = require("fs-extra");
const Database = require("better-sqlite3");
const getUuid = require("uuid-by-string");
const https = require("https");
const http = require("http");
const xmljs = require("xml-js");
const js2xml = xmljs.js2xml;
const xml2js = xmljs.xml2js;
// If usercfg folder doesn't exist, copy basecfg to usercfg
if(!fse.existsSync("./usercfg")) {
if(fse.existsSync("./basecfg")) {
fse.copySync("./basecfg", "./usercfg");
} else {
// Warn user
console.log("WARNING: basecfg folder is missing! Re-install is recommended.");
process.exit(1);
}
}
// Load usercfg
const config = JSON.parse(fse.readFileSync("./usercfg/config.json"));
const motd = JSON.parse(fse.readFileSync("./usercfg/motd.json"));
const store = JSON.parse(fse.readFileSync("./usercfg/store.json"));
const credits = JSON.parse(fse.readFileSync("./usercfg/credits.json"));
const catalog = JSON.parse(fse.readFileSync("./usercfg/catalog.json"));
const save = JSON.parse(fse.readFileSync("./usercfg/save.json"));
const netvars = fse.readFileSync("./usercfg/netvars.dat").toString("base64");
const baseinventory = JSON.parse(fse.readFileSync("./usercfg/inventory.json"));
// Database
const db = new Database("./usercfg/database.db", { verbose: config.debug ? console.log : null });
db.pragma('journal_mode = WAL');
// Delete users table if wipe_on_start is true
if(config.database.wipe_on_start)
{
db.exec("DROP TABLE users");
}
// Create users table if it doesn't exist
db.exec("CREATE TABLE IF NOT EXISTS users (uuid TEXT PRIMARY KEY, inventory TEXT, data TEXT, consoleid TEXT, consoleticket TEXT, ip TEXT)");
// Create app and configure
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.text({ type: "text/xml" }));
// Log requests
if(config.debug) {
app.use((req, res, next) => {
// Log request
console.log(`${req.method} ${req.url}`);
// Next
next();
});
}
// Endpoint: /files/netvars.dat
// Game info stored in base64
app.get("/files/netvars.dat", function(req, res) {
// Build JSON response
const response = {
"data": netvars,
};
// Send response
res.json(response);
});
// Endpoint: /auth/token
// Returns a UUID specific to the user's ticket (Steam, presumably)
app.post("/auth/token", function(req, res) {
// Validate authorization header
if(!req.headers.authorization) {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Get authorization header
const auth = req.headers.authorization.split(" ");
// Verify authorization header
if(auth[0] != "Basic") {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Find UUID by ticket header
const ticketHeader = req.body.ticket.replace("_", "").replace("-", "");
const uuidDb = db.prepare("SELECT uuid FROM users WHERE consoleticket = ?").get(ticketHeader);
let uuid;
if(!uuidDb) {
// Try looking by ip
const ipDb = db.prepare("SELECT uuid FROM users WHERE ip = ?").get(req.ip);
if(ipDb) {
// Use this uuid
uuid = ipDb.uuid;
} else {
// Create new UUID
uuid = getUuid(ticketHeader);
}
} else {
// Get UUID from database
uuid = uuidDb.uuid;
}
const token = {
"token_type": "bearer",
"access_token": uuid,
"expires_in": 1000000,
"refresh_token": "",
};
// Send response
res.json(token);
});
// Endpoint: /motd
// Used with parameters ?channels.0=all&channels.1=all_no_wbid&channels.2=multiplayer&channels.3=multiplayer_no_wbid&country=US&page=1&per_page=10
// We're not parsing all of that, so we'll return a static response.
app.get("/motd", function(req, res) {
// Send response
res.json(motd);
});
// Endpoint: /store/catalog/general
// Game catalog
app.get("/store/catalog/general", function(req, res) {
// Send response
res.json(catalog);
});
// Endpoint: /store/offers
// Used with parameters ?page=1&vendor=0 or ?page=1&vendor=4
app.get("/store/offers", function(req, res) {
// Check if vendor is 0 or 4
if(req.query.vendor == 4) {
res.json(credits);
} else {
res.json(store);
}
});
// Endpoint: /store/vouchers/transactions
// POST by the game
app.post("/store/vouchers/transactions", function(req, res) {
// Free vouchers from the game
const vouchers = [
"e8fd70ec-f3ec-519b-8b57-70518c4c4f74",
"640144eb-7862-5186-90d0-606211ec2271",
"54d80a04-cfbc-51a4-91a1-a88a5c96e7ea",
"82a9febc-5f11-57db-8464-2ed2b4df74f9",
];
if(!vouchers.includes(req.body.voucher_id)) {
// Send error
res.status(400).send("Invalid voucher ID");
return;
}
// This doesn't follow API spec, but just use the offer_id as the transaction_id
const transactionid = req.body.voucher_id;
// Build JSON response
const response = {
"transaction_id": transactionid,
};
// 201 Created
res.status(201).json(response);
});
// Endpoint: /store/purchases/transactions
// POST by the game
app.post("/store/purchases/transactions", function(req, res) {
// This doesn't follow API spec, but just use the offer_id as the transaction_id
const transactionid = req.body.offer_id;
// Build JSON response
const response = {
"transaction_id": transactionid,
};
// 201 Created
res.status(201).json(response);
});
// Transactions
function Transaction(req, res) {
// We don't check transaction IDs, but at least ensure it's there
if(!req.params.transactionid) {
// Send error
res.status(400).send("Invalid transaction ID");
return;
}
// Validate authorization header
if(!req.headers.authorization) {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Get authorization header
const auth = req.headers.authorization.split(" ");
// Verify authorization header
if(auth[0] != "Bearer") {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Get UUID
const ticket = auth[1];
// Create UUID from ticket
let uuid = ticket;
// Check if UUID is in database
const uuidDb = db.prepare("SELECT uuid FROM users WHERE uuid = ?").get(uuid);
if(!uuidDb) {
// Report a server error
console.log(`ERROR: UUID ${uuid} not found in database!`);
res.status(500).send("Internal server error");
return;
}
// Log UUID
console.log(`VOUCHER: ${req.params.transactionid} (${uuid}`);
const unlocks = {
"items": {},
};
let replace = true;
try {
switch(req.params.transactionid) {
case "2f93daeb-d68f-4b28-80f4-ace882587a13":
// Assortment of consumables
let consumables = [];
// Get consumables
for(let key in catalog.items) {
const item = catalog.items[key];
if(item.data && item.data.gangland_is_consumable == "1") {
consumables.push(key);
}
}
// Pick 5 random consumables
const consumablecount = 5;
for(let i = 0; i < consumablecount; i++) {
const item = consumables[Math.floor(Math.random() * consumables.length)];
if(unlocks.items[item]) {
unlocks.items[item] += 1;
} else {
unlocks.items[item] = 1;
}
}
replace = false;
break;
}
} catch (e) {
console.log(e);
}
const inventoryprep = db.prepare("SELECT inventory FROM users WHERE uuid = ?");
const inventorylist = inventoryprep.get(uuid);
let inventoryobj = baseinventory;
// Check if inventory exists
if(inventorylist && inventorylist.inventory) {
inventoryobj = JSON.parse(inventorylist.inventory);
}
// Add items to inventory
for(let itemid in unlocks.items) {
// Add item to inventory item count (or create it)
if(inventoryobj.inventory[itemid]) {
inventoryobj.inventory[itemid] += unlocks.items[itemid];
} else {
inventoryobj.inventory[itemid] = unlocks.items[itemid];
}
}
// Update inventory
const inventory = JSON.stringify(inventoryobj);
const inventoryupdate = db.prepare("UPDATE users SET inventory = ? WHERE uuid = ?");
inventoryupdate.run(inventory, uuid);
// 201 Created
res.status(201).json(unlocks);
}
// Endpoint: /store/vouchers/:transactionid
// PUT by the game
app.put("/store/vouchers/:transactionid", Transaction);
// Endpoint: /store/purchases/:transactionid
// PUT by the game
app.put("/store/purchases/:transactionid", Transaction);
// Endpoint: /users/[uuid]/[sub1]/[sub2]
// This is where settings and other user data is stored.
// The game may also PUT to this endpoint.
// We're going to save the data to a file, maybe in the future we'll use a database.
app.get("/users/:uuid/:subpage?/:subpage2?", function(req, res) {
const urluuid = req.url.split("/")[2]; // req.query doesn't work here
const subpage = req.url.split("/")[3];
const subpage2 = req.url.split("/")[4];
// Validate authorization header
if(!req.headers.authorization) {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Get authorization header
const auth = req.headers.authorization.split(" ");
// Verify authorization header
if(auth[0] != "Bearer") {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Get UUID
const ticket = auth[1];
// Create UUID from ticket
let uuid = ticket;
// Log UUID
console.log(`AUTH: ${uuid}`);
if(urluuid === "me") {
if(!subpage) {
// Build JSON response with UUID
const user = {
"user_id": uuid,
};
// Send response
res.json(user);
} else if(subpage == "inventory") {
// Check if UUID is in database
const uuidDb = db.prepare("SELECT uuid FROM users WHERE uuid = ?").get(uuid);
if(!uuidDb) {
// Report a server error
console.log(`ERROR: UUID ${uuid} not found in database!`);
res.status(500).send("Internal server error");
return;
}
let inventoryobj = baseinventory;
let reset = false;
// Query database for inventory
const inventoryprep = db.prepare("SELECT inventory FROM users WHERE uuid = ?");
const inventorylist = inventoryprep.get(uuid);
// If inventory doesn't exist, create it
if(!inventorylist || !inventorylist.inventory) {
// Insert inventory into existing row
const inventoryinsert = db.prepare("UPDATE users SET inventory = ? WHERE uuid = ?");
inventoryinsert.run(JSON.stringify(inventoryobj), uuid);
} else {
// Inventory is a JSON object
inventoryobj = JSON.parse(inventorylist.inventory);
reset = true;
}
// Send response
res.json(inventoryobj);
}
} else if(subpage === "profile") {
// Check if UUID is in database
const uuidDb = db.prepare("SELECT uuid FROM users WHERE uuid = ?").get(uuid);
if(!uuidDb) {
// Report a server error
console.log(`ERROR: UUID ${uuid} not found in database!`);
res.status(500).send("Internal server error");
return;
}
if(subpage2 === "private") {
// Check if UUID matches the one in the URL
if(uuid != urluuid) {
// Send error
res.status(400).send("Invalid UUID");
return;
}
// Pull from database
const dataprep = db.prepare("SELECT data FROM users WHERE uuid = ?");
const data = dataprep.get(uuid);
let json = save;
// Check if data exists
if(!data || !data.data) {
// Insert save into existing row
const insert = db.prepare("UPDATE users SET data = ? WHERE uuid = ?");
insert.run(JSON.stringify(json), uuid);
} else {
// Save is a JSON object
json = JSON.parse(data.data);
}
// Send response
res.json(json);
} else {
// unimplemented, return empty object
console.log(`Unimplemented endpoint: ${req.url}`);
res.json({});
}
} else {
// unimplemented, return empty object
console.log(`Unimplemented endpoint: ${req.url}`);
res.json({});
}
});
app.put("/users/:uuid/:subpage?/:subpage2?", function(req, res) {
const urluuid = req.url.split("/")[2]; // req.query doesn't work here
const subpage = req.url.split("/")[3];
const subpage2 = req.url.split("/")[4];
// Validate authorization header
if(!req.headers.authorization) {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Get authorization header
const auth = req.headers.authorization.split(" ");
// Get UUID
const ticket = auth[1];
// Create UUID from ticket
let uuid = ticket;
if(urluuid === "me") {
if(subpage === "wbnet") {
// log whatever's in the body
console.log(req.body);
res.json({
message: "No WBNet user linked",
code: 2600,
});
return;
} else {
// unimplemented, print out
console.log(`Unimplemented endpoint: ${req.url}`);
}
} else if(subpage === "profile") {
// Check if UUID is in database
const uuidDb = db.prepare("SELECT uuid FROM users WHERE uuid = ?").get(uuid);
if(!uuidDb) {
// Report a server error
console.log(`ERROR: UUID ${uuid} not found in database!`);
res.status(500).send("Internal server error");
return;
}
// Check if UUID matches the one in the URL
if(uuid != urluuid) {
// Send error
res.status(400).send("Invalid UUID");
return;
}
if(subpage2 === "private") {
// Verify authorization header
if(auth[0] != "Bearer") {
// Send error
res.status(400).send("Invalid authorization header");
return;
}
// Check body JSON to ensure .data.AccountXPLevel >= 24
if(!req.body.data || !req.body.data.AccountXPLevel || req.body.data.AccountXPLevel < 24) {
// Send error
res.status(400).send("Invalid body");
return;
}
// Check AccountXPLevel
if(req.body.data && req.body.data.AccountXPLevel < 24) {
// Just set it
req.body.data.AccountXPLevel = 24;
// Log to console
console.log("Account XP level set");
}
if(req.body.data && req.body.data.baneXPLevel < 24) {
// Just set it
req.body.data.baneXPLevel = 24;
// Log to console
console.log("Bane XP level set");
}
// Check jokerXPLevel
if(req.body.data && req.body.data.jokerXPLevel < 24) {
// Just set it
req.body.data.jokerXPLevel = 24;
// Log to console
console.log("Joker XP level set");
}
// Update database
const updateprep = db.prepare("UPDATE users SET data = ? WHERE uuid = ?");
updateprep.run(JSON.stringify(req.body), uuid);
} else {
// unimplemented, print out
console.log(`Unimplemented endpoint: ${req.url}`);
}
} else {
// unimplemented, print out
console.log(`Unimplemented endpoint: ${req.url}`);
}
// Send response
res.status(204).send();
});
// Endpoint: /actions/:action
// Unknown...
// SOAP functions
function DummyFunc(name, args) {
console.log(name);
console.log(args);
}
const wbmanagement = {
LookupWbid: function(args, callback) {
DummyFunc("LookupWbid", args);
// Verify realm
//args.realm == "STEAM" &&
if(args.title == "OZZY" && args.uniqueId) {
// Create or set UUID in database using characters before / in consoleTicket
const ticketHeader = args.consoleTicket.replace("/", "").replace("+", "");
const uuid = getUuid(ticketHeader);
// Create entry if it doesn't exist
const user = db.prepare("SELECT * FROM users WHERE consoleid = ?").get(args.consoleId);
if(!user) {
// Create entry
const prep = db.prepare("INSERT INTO users (uuid, inventory, data, consoleid, consoleticket, ip) VALUES (?, ?, ?, ?, ?, ?)");
prep.run(uuid, JSON.stringify(baseinventory), JSON.stringify(save), args.consoleId, ticketHeader, args.ip);
} else {
// Update console ID and ticket
const prep = db.prepare("UPDATE users SET consoleticket = ?, uuid = ?, ip = ? WHERE consoleid = ?");
prep.run(ticketHeader, uuid, args.ip, args.consoleId);
}
/*
return {
LookupWbidResult: args.realm + "_" + args.consoleId + "@" + "example.com"
};
*/
}
return {};
},
AssociateWbid: function(args, callback) {
DummyFunc("AssociateWbid", args);
return {};
},
DisassociateWbid: function(args, callback) {
DummyFunc("DisassociateWbid", args);
return {};
},
CreateAccount: function(args, callback) {
DummyFunc("CreateAccount", args);
return {};
},
CreateAccountAndAssociate: function(args, callback) {
DummyFunc("CreateAccountAndAssociate", args);
return {};
},
ResetPassword: function(args, callback) {
DummyFunc("ResetPassword", args);
return {};
},
StartWBPasswordReset: function(args, callback) {
DummyFunc("StartWBPasswordReset", args);
return {};
},
StartWBPasswordResetFromConsole: function(args, callback) {
DummyFunc("StartWBPasswordResetFromConsole", args);
return {};
},
FinishWBPasswordReset: function(args, callback) {
DummyFunc("FinishWBPasswordReset", args);
return {};
},
GetSubscriptionInformation: function(args, callback) {
DummyFunc("GetSubscriptionInformation", args);
return {
GetSubscriptionInformationResult: {
WbidAccountId: getUuid(args.consoleId + ":accountid"),
SubscriptionId: getUuid(args.consoleId + ":subscriptionid"),
AccountId: getUuid(args.consoleId + ":accountid"),
Entitlements: [] // ??? I couldn't source a dump of this, may be used for DLC
}
};
}
};
// Pretty much half-implemented SOAP server
// No WDSL because the soap package does not immediately support the official one.
// The game doesn't call it, so it's not needed.
function SOAPPost(req, res) {
// Get XML as JSON
const xml = xml2js(req.body);
let soapreq = {
name: "",
args: {}
};
for(let i = 0; i < xml.elements.length; i++) {
for(let j = 0; j < xml.elements[i].elements.length; j++) {
soapreq.name = xml.elements[i].elements[j].elements[0].name;
for(let k = 0; k < xml.elements[i].elements[j].elements[0].elements.length; k++) {
if(xml.elements[i].elements[j].elements[0].elements[k].elements != undefined) {
for(let l = 0; l < xml.elements[i].elements[j].elements[0].elements[k].elements.length; l++) {
soapreq.args[xml.elements[i].elements[j].elements[0].elements[k].name] = xml.elements[i].elements[j].elements[0].elements[k].elements[l].text;
}
} else {
soapreq.args[xml.elements[i].elements[j].elements[0].elements[k].name] = xml.elements[i].elements[j].elements[0].elements[k].text;
}
}
}
}
let xmlres = {
declaration: {
attributes: {
version: "1.0",
encoding: "utf-8"
}
},
elements: [{
type: "element",
name: "soap:Envelope",
attributes: {
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/"
},
elements: [{
type: "element",
name: "soap:Body",
elements: []
}]
}]
};
let soapres = {
name: "",
args: {}
};
// Content type
res.set("Content-Type", "text/xml");
let fault = false;
// Call SOAP function
if(wbmanagement[soapreq.name]) {
soapres.name = soapreq.name + "Response";
// Add IP address to args
let args = soapreq.args;
args.ip = req.ip;
soapres.args = wbmanagement[soapreq.name](args);
// Empty check
if(Object.keys(soapres.args).length === 0) {
fault = true;
res.status(500);
} else {
// Build XML response
xmlres.elements[0].elements[0].elements.push({
type: "element",
name: soapres.name,
elements: []
});
for(let key in soapres.args) {
xmlres.elements[0].elements[0].elements[0].elements.push({
type: "element",
name: key,
elements: [{
type: "text",
text: soapres.args[key]
}]
});
}
}
} else {
fault = true;
res.status(500);
}
if(fault) {
// Build XML fault
xmlres.elements[0].elements[0].elements.push({
type: "element",
name: "soap:Fault",
elements: [
{
type: "element",
name: "soap:Code",
elements: [{
type: "text",
text: "soap:Receiver"
}]
},
{
type: "element",
name: "soap:Reason",
elements: [{
type: "text",
text: "Unhandled exception ---> The provided SteamTicketInformation ticket has expired."
}]
},
{
type: "element",
name: "soap:Node",
elements: [{
type: "text",
text: "Turbine.Ams.Steam.SteamAuthenticationProvider.ValidateExternalTicket_worker"
}]
},
{
type: "element",
name: "detail",
elements: [
{
type: "element",
name: "exceptiontype",
elements: [{
type: "text",
text: "Turbine.Security.TicketExpiredException"
}]
},
{
type: "element",
name: "errorcode",
elements: [{
type: "text",
text: "0xA01B000C"
}]
}
]
}
]
});
}
// Send response
res.send(js2xml(xmlres));
}
app.post("/CLS/WbAccountManagement.asmx", SOAPPost);
app.post("/CLS/WbSubscriptionManagement.asmx", SOAPPost);
// Start servers
function done() {
if(config.debug) {
console.log(`WEB: Listening on port ${config.host.https_enabled ? config.host.https_port : config.host.http_port}`);
}
}
let server;
let server_passthrough;
if(!config.host.https_enabled) {
server = app.listen(config.host.http_port, () => {
done();
});
} else {
server = https.createServer({
key: fse.readFileSync(`./usercfg/${config.host.https_key}`),
cert: fse.readFileSync(`./usercfg/${config.host.https_cert}`),
}, app).listen(config.host.https_port, () => {
done();
});
server.on("tlsClientError", err => {
if(config.debug) {
console.error(err);
}
});
server.on("connection", socket => {
socket.on("error", err => {
if(config.debug) {
console.error(err);
}
});
socket.on("data", data => {
if(config.debug) {
console.log(`WEB: TLS read ${data.length} bytes`);
}
});
});
}
if(config.host.localhost_passthrough_enabled) {
// Passthrough server
server_passthrough = http.createServer(app).listen(config.host.localhost_passthrough_port, () => {
if(config.debug) {
console.log(`WEB: Passthrough on port ${config.host.localhost_passthrough_port}`);
}
});
}
// Connection storage
let connections = [];
server.on('connection', connection => {
connections.push(connection);
connection.on('close', () => {
connections = connections.filter(curr => curr !== connection);
});
});
if(config.host.localhost_passthrough_enabled) {
server_passthrough.on('connection', connection => {
connections.push(connection);
connection.on('close', () => {
connections = connections.filter(curr => curr !== connection);
});
});
}
// Closing
function Close(param) {
if(param instanceof Error)
console.error(param);
// Log
if(config.debug)
console.log("Shutting down...");
// Close database
db.close();
// Close connections
connections.forEach((connection) => {
connection.destroy();
});
// Close server
server.close();
if(config.localhost_passthrough_enabled)
server_passthrough.close();
// Exit
process.exit();
}
process.on("SIGINT", Close);
process.on("SIGTERM", Close);
process.on("SIGUSR1", Close);
process.on("SIGUSR2", Close);
process.on("uncaughtException", Close);
process.on("unhandledRejection", Close);