-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.js
708 lines (637 loc) · 20.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
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
(function () {
'use strict';
var request = require('ajax-request');
var fs = require("fs");
var archiver = require('archiver');
var AdmZip = require('adm-zip');
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory();
});
}
function removeDuplicates(a)
{
return a.filter(function(item, pos, self) { return self.indexOf(item) == pos; })
}
var cloudEnabled = false;
var cloudFiles = {};
var _MODS = [];
function initCloud(max, cur) {
}
var options = {};
try {
options = JSON.parse(fs.readFileSync('./options.json', 'utf8'));
}
catch (err) {
console.log("failed to load options");
}
const ngrok = require('ngrok');
var ngrokURL;
ngrok.kill();
if (!options.noNgrok) {
ngrok.connect(options.port || 30000).then(function(resultURL){
ngrokURL = resultURL;
});
}
if (cloudEnabled) {
// load up the file cache
}
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var handlebars = require('express-handlebars');
var app = express();
var compression = require('compression');
// compress responses
app.use(compression());
var rimraf = require('rimraf');
var mkdirp = require('mkdirp');
var formidable = require('formidable');
var natUpnp = require('nat-upnp');
var cors = require('cors');
var async = require('async');
var client = natUpnp.createClient();
var privatePort = options.port || 30000;
var publicPort = Math.ceil(5536 + Number(privatePort));
var externalIP;
var os = require('os');
var interfaces = os.networkInterfaces();
var addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
}
}
}
client.portMapping({
public: publicPort,
private: privatePort,
ttl: 0
}, function(err) {
// Will be called once finished
});
/*client.portUnmapping({
public: publicPort
});*/
client.getMappings(function(err, results) {});
client.getMappings({local: true}, function(err, results) {});
client.externalIp(function(err, ip) {
if (!err) {
externalIP = ip;
}
else {
externalIP = "127.0.0.1";
}
});
//view engine setup
app.set('port', privatePort);
app.engine('.hbs', handlebars({
extname: '.hbs',
layoutsDir:'views/layouts',
partialsDir:'views/'
}));
app.set('view engine', '.hbs');
app.use(cors({
'allowedHeaders': ['sessionId', 'Content-Type'],
'exposedHeaders': ['sessionId'],
'origin': '*',
'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE',
'preflightContinue': false
}));
app.use(bodyParser.json({limit: '64mb'}));
app.use(bodyParser.urlencoded({
limit: '64mb',
extended:true
}));
app.use("/core",express.static('./public'));
app.use(cookieParser());
app.get('/delete', function(req, res){
var ip = req.ip;
if (req.query.path) {
if (isThisLocalhost(req)) {
if (req.query.path[req.query.path.length-1] == "/" || req.query.path[req.query.path.length-1] == "\\") {
rimraf('./public/custom' + req.query.path.substring(0, req.query.path.length-1), function() { res.redirect("/files"); });
}
else {
fs.stat('./public/custom' + req.query.path, function (err, stats) {
if (err) {
return console.error(err);
}
fs.unlink('./public/custom' + req.query.path, (err) => {res.redirect("/files");});
});
}
}
}
else {
res.redirect("/files");
}
});
app.get('/move', function(req, res){
var ip = req.ip;
if (req.query.path && req.query.newPath) {
if (isThisLocalhost(req)) {
fs.stat('./public/custom' + req.query.newPath, function (err, stats) {
if (err) {
fs.rename('./public/custom' + req.query.path, './public/custom' + req.query.newPath, function (err) {
if (err) throw err;
res.redirect("/files");
});
return;
}
else {
if (err) throw err;
res.send("File Already Exists");
}
});
}
}
else {
res.redirect("/files");
}
});
app.get('/create', function(req, res){
var ip = req.ip;
if (req.query.path) {
if (isThisLocalhost(req)) {
mkdirp('./public/custom' + req.query.path, function(err) {
res.redirect("/files");
});
}
}
else {
res.redirect("/files");
}
});
app.post('/patch', function(req, res){
var ip = req.ip;
var successes = {};
if (req.body && req.body.upload && req.body.upload.length) {
if (isThisLocalhost(req)) {
for (var i=0; i<req.body.upload.length; i++) {
function readWrap(file, index) {
var buf = new Buffer(file.blob, 'base64'); // decode
fs.writeFile('./' + file.path + file.name, buf, function(err) {
if (err) {
console.log("err", err);
}
successes[index] = true;
for (var j=0; j<req.body.upload.length; j++) {
if (successes[j] == null) {
return false;
}
}
res.redirect("/files");
});
}
readWrap(req.body.upload[i], i);
}
}
}
else {
res.redirect("/files");
}
});
app.post('/upload', function(req, res){
var ip = req.ip;
var successes = {};
if (req.body && req.body.upload && req.body.upload.length) {
if (isThisLocalhost(req)) {
for (var i=0; i<req.body.upload.length; i++) {
function readWrap(file, index) {
var buf = new Buffer(file.blob, 'base64'); // decode
fs.writeFile('./public/custom' + file.path + file.name, buf, function(err) {
if (err) {
console.log("err", err);
}
successes[index] = true;
for (var j=0; j<req.body.upload.length; j++) {
if (successes[j] == null) {
return false;
}
}
res.redirect("/files");
});
}
readWrap(req.body.upload[i], i);
}
}
}
else {
res.redirect("/files");
}
});
app.get('/files', function(req, res){
var waiting = true;
var waitTable = {};
function read(path, success) {
var data = {path : (path || ""), c : []};
waitTable[path || "_root"] = true;
fs.readdir("." + '/public/custom' + (path || ""), (err, files) => {
for (var i in files) {
if (files[i].match("\\.")) {
data.c.push((path || "") + files[i]);
}
else {
if (path) {
read(path+files[i]+"\\", (list)=>{data.c.push(list);});
}
else {
read("\\"+files[i]+"\\", (list)=>{data.c.push(list);});
}
}
}
waitTable[path || "_root"] = false;
if (success) {
success(data);
}
});
return data;
}
var result = read();
function sendRes(depth) {
var waiting = false;
for (var key in waitTable) {
if (waitTable[key] == true) {
waiting = true;
break;
}
}
if (!waiting || depth >= 10) {
function recurseSort(list) {
list.c.sort(function(a, b){
var aname = a;
if (a instanceof Object) {
aname = a.path.toUpperCase();
if (b instanceof Object) {
bname = b.path.toUpperCase();
if(aname < bname) return -1;
if(aname > bname) return 1;
return 0;
}
else {
return -1;
}
}
var bname = b;
if (b instanceof Object) {
return 1;
}
aname = aname.split("\\")[aname.split("\\").length-1].toUpperCase();
bname = bname.split("\\")[bname.split("\\").length-1].toUpperCase();
if(aname < bname) return -1;
if(aname > bname) return 1;
return 0;
});
for (var i in list.c) {
if (list.c[i] instanceof Object) {
recurseSort(list.c[i]);
}
}
}
recurseSort(result);
res.send(result);
}
else {
setTimeout(function(){sendRes(depth+1);}, 1000);
}
}
sendRes(0);
});
app.get('/', function(req, res){
//res.redirect('/files');
var ip = req.ip;
if (isThisLocalhost(req)) {
ip = "127.0.0.1";
res.render('index', {host : true, layout : "main"});
}
else {
res.render('index', {player : true, layout : "main"});
}
});
var server = app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + server.address().port);
});
var http = require('http');
var session = require('express-session');
var game = require('./lib/game');
var users = require('./lib/users');
// Session Support:
var sessionStore = session({
secret: 'octocat',
saveUninitialized: false, // does not save uninitialized session.
resave: false // does not save session if not modified.
});
app.use(sessionStore);
app.get('/createGame', function(req, res){
game.createGame(userID,
{
name : gameName,
password : gamePassword,
ownerName : data.name || user.displayName,
game : gameKey,
gameID: id,
url : genURL(userID.substring(userID.length-6, 6)),
membership : data.membership,
},
(game)=>{
res.send({url : '/'});
},
()=>{res.send({url : '/'});}
);
});
app.get('/game', function(req, res){
res.render('index', {
layout : "game",
title : "GM Forge",
});
});
app.get('/retrieveUser', function(req, res){
var userID;
res.send({displyName : "Game Host", membership : 400});
});
app.get('/cloud/:useToken/*', function(req, res) {
res.redirect("https://files.gmforge.io/file/"+req.url.split("/cloud/")[1]);
});
app.get('/cdn/*', function(req, res) {
res.header("Access-Control-Allow-Origin", "https://www.gmforge.io" || "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
var url = decodeURI(req.url);
if (fs.existsSync("./public/content/" + url.replace("/cdn/", "").split("?")[0])) {
res.sendFile(url.replace("/cdn/", "").split("?")[0], { root: "/public/content" });
}
else {
res.sendFile("error.png", { root: "/public/content" });
}
});
app.get('/join', function(req, res){
var ip = req.ip;
console.log(ngrokURL);
if (req.query.userID && req.query.userID != "localhost") {
if (!options.accounts || (!options.accounts[req.query.userID].password || options.accounts[req.query.userID].password == req.query.password)) {
res.cookie("InternalIP", addresses[0] || "", 99999999999999)
res.cookie("ExternalIP", externalIP || "", 99999999999999);
res.cookie("PublicPort", publicPort || privatePort, 9999999999999);
res.cookie("PublicLink", encodeURI(ngrokURL), 9999999999999);
res.cookie("PrivatePort", publicPort, 9999999999999); // don't give them the private port regardless
res.cookie("UserID", req.query.userID, 9999999999999999);
res.render('game', {
layout : "game",
title : "GM Forge",
local : (isThisLocalhost(req))?(true):(false)
});
}
else {
res.redirect("/");
}
}
else {
if ((isThisLocalhost(req) || (options.hostPW && req.query.password == options.hostPW)) && !req.query.select) {
ip = "127.0.0.1";
res.cookie("InternalIP", addresses[0] || "", 99999999999999)
res.cookie("ExternalIP", externalIP || "", 99999999999999);
res.cookie("PublicPort", publicPort || privatePort, 9999999999999);
res.cookie("PublicLink", encodeURI(ngrokURL), 9999999999999);
res.cookie("PrivatePort", privatePort, 9999999999999);
res.cookie("UserID", "localhost", 99999999999999);
res.render('game', {host : true, layout : "game"});
}
else {
res.render('index', {player : true, layout : "main"});
}
}
});
app.get('/loadGame', function(req, res){
var ip = req.ip;
if (isThisLocalhost(req)) {
if (fs.existsSync("./public/worlds/" + req.query.gameData)) {
fs.readFile("./public/worlds/" + req.query.gameData, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
game.createGame(data, req.query.gameName, req.query.gameKey, "localhost");
res.send({text : "Successfully Loaded"});
});
}
else {
game.createGame(null, req.query.gameName, req.query.gameKey, "localhost");
if (!req.query.gameData) {
game.saveGame();
}
res.send({text : "Successfully Loaded"});
}
}
});
var users = require("./lib/users.js");
users.initialize(server);
app.get('/gameList', function(req, res){
res.send({list : game.getGameTypes()});
});
var exitfn;
app.get('/exit', function(req, res){
var ip = req.ip;
if (isThisLocalhost(req)) {
exitfn();
res.send({result : true});
}
});
app.get('/getOptions', function(req, res){
var ip = req.ip;
if (isThisLocalhost(req)) {
res.send(options);
}
else {
var dupeOptions = JSON.parse(JSON.stringify(options));
var tempOptions = {};
tempOptions.accounts = dupeOptions.accounts;
for (var k in tempOptions.accounts) {
if (tempOptions.accounts[k].password) {
tempOptions.accounts[k].password = true;
}
}
res.send(tempOptions);
}
});
app.get('/getWorlds', function(req, res){
var ip = req.ip;
if (isThisLocalhost(req)) {
res.send(fs.readdirSync("./public/worlds"));
}
});
app.get('/getWorkshopFiles', function(req, res){
var ip = req.ip;
if (isThisLocalhost(req)) {
res.send(fs.readdirSync("./public/workshop"));
}
});
app.get('/updateOptions', function(req, res){
var ip = req.ip;
if (isThisLocalhost(req)) {
fs.writeFileSync('options.json', req.query.options, 'utf8');
options = JSON.parse(req.query.options);
res.send({result : true});
}
});
app.get('/getCollections', function(req, res){
var packList = [];
fs.readdirSync("./public/collections").forEach(function(file){
packList.push(file);
});
getDirectories("./public/workshop/").forEach(function(mod)
{
if(!fs.existsSync("./public/workshop/"+mod+"/collections")){return;}
fs.readdirSync("./public/workshop/"+mod+"/collections").forEach(function(file){
packList.push(file);
});
});
packList = removeDuplicates(packList);
res.send(packList);
});
app.get('/getFonts', function(req, res){
var packList = [];
fs.readdirSync("./public/fonts").forEach(function(file){
packList.push(file);
});
getDirectories("./public/workshop/").forEach(function(mod)
{
if(!fs.existsSync("./public/workshop/"+mod+"/fonts")){return;}
fs.readdirSync("./public/workshop/"+mod+"/fonts").forEach(function(file){
packList.push(file);
});
});
packList = removeDuplicates(packList);
res.send(packList);
});
app.get('/getPacks', function(req, res){
var packList = [];
fs.readdirSync("./public/packs").forEach(function(file){
packList.push(file);
});
getDirectories("./public/workshop/").forEach(function(mod)
{
if(!fs.existsSync("./public/workshop/"+mod+"/packs")){
return;
}
fs.readdirSync("./public/workshop/"+mod+"/packs").forEach(function(file){
packList.push(file);
});
});
packList = removeDuplicates(packList);
res.send(packList);
});
app.get('/getScripts', function(req, res){
var packList = [];
fs.readdirSync("./public/scripts").forEach(function(file){
packList.push({src : file});
});
getDirectories("./public/workshop/").forEach(function(mod)
{
if(!fs.existsSync("./public/workshop/"+mod+"/scripts")){return;}
fs.readdirSync("./public/workshop/"+mod+"/scripts").forEach(function(file){
packList.push({src : file, mod : mod});
});
});
packList = packList.filter((e, i) => {
return packList.findIndex((x) => {return x.src == e.src;}) == i;
});
console.log(packList);
res.send(packList);
});
app.get('/getMods', function(req, res){
res.send(getDirectories("./public/workshop/"));
});
app.get('/getWhitelistedMods', function(req, res){
if (game.getGame()) {
res.send(game.getGame().config.library || {});
}
else {
res.send({});
}
});
var isThisLocalhost = function (req){
var ip = req.connection.remoteAddress;
var host = req.get('host');
return ip === "127.0.0.1" || ip === "::ffff:127.0.0.1" || ip === "::1" || host.indexOf("localhost") !== -1;
}
app.get("/openFiles", function(req, res){
var ip = req.ip;
console.log(ip);
if (isThisLocalhost(req)) {
const {shell} = require('electron') // deconstructing assignment
var path = require("path");
var queryPath = "./public/custom";
if (req.query && req.query.path) {
queryPath = "./public" + req.query.path;
}
var absolutePath = path.resolve(queryPath);
shell.openItem(absolutePath);
console.log(absolutePath);
}
});
app.get('/workshopPublish', function(req, res){
});
app.get('/myWorkshop', function(req, res){
});
app.get('/packagePack', function(req, res){
});
app.get('/workshopUpdate', function(req, res){
});
function mountMods()
{
console.log("\x1b[32mModloader: Mounting mods!")
_MODS = getDirectories("./public/workshop/");
for(var k in _MODS)
{
var k = _MODS[k];
app.use('/'+encodeURIComponent('mod_'+k),express.static('.\\public\\workshop\\'+k));
console.log('/'+encodeURIComponent('mod_'+k),'===>','.\\public\\workshop\\'+k);
}
console.log("Modloader: Mounting done.\x1b[0m")
}
function deleteFolderRecursive(path) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file) {
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
function loadMods(callback)
{
}
app.get('/workshopSync', function(req, res){
});
app.use(function(req, res, next) {
if (req.url.indexOf('/core')==0||req.url.indexOf('/mod_')==0)
{
next();
return;
}
var urlPath = decodeURIComponent(req.url);
urlPath = /([^?]*).*/.exec(urlPath)[1]
console.log("Request to:",urlPath);
for(var k in _MODS)
{
var k = _MODS[k];
var path = './public/workshop/'+k+urlPath
if (fs.existsSync(path)) {
res.redirect('/'+encodeURIComponent('mod_'+k)+urlPath);
console.log("Redirected to "+encodeURIComponent('mod_'+k)+urlPath);
return;
}
}
res.redirect('/core'+req.url);
});
// synchronize workshop items
loadMods(function(ret){
console.log("Initial modload:",ret)
})
module.exports = {app : app, server : server, publicPort : publicPort, options : options, exit : function(cb){exitfn = cb;}};
}());