-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
767 lines (640 loc) · 30.9 KB
/
server.js
File metadata and controls
767 lines (640 loc) · 30.9 KB
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
// Increases the libuv thread pool default from 4 to 128 threads.
// Must absolutely be set BEFORE any 'require' imports trigger underlying async C++ initializations.
process.env.UV_THREADPOOL_SIZE = 128;
const express = require('express');
const mustacheExpress = require('mustache-express');
const app = express();
global.expressapp = app;
const path = require("path");
const util = require('util');
const bodyParser = require('body-parser');
const proxy = require('express-http-proxy');
const cron = require("node-cron");
const AsyncNedb = require('@seald-io/nedb');
const Mongod = require("./node_components/mongodb_server/mongod");
const portfinder = require("portfinder");
const passport = require('passport');
global.passport = passport;
const axios = require('axios');
const session = require('express-session');
const assert = require('assert');
const fs = require('fs-extra');
const socket = require('socket.io');
// const clientFile = require("./node_modules/socket.io/client-dist/socket.io.min?raw");
// const clientMap = require("./node_modules/socket.io/client-dist/socket.io.min.js.map?raw");
// socket.sendFile = (filename, req, res) => {
// res.end(filename.endsWith(".map") ? clientMap : clientFile);
// };
const socketwildcard = require('socketio-wildcard');
const configmgr = require('./node_components/server_config');
const database_controller = require('./node_components/common/database_controller');
const { Player } = require('./node_components/player');
const logfactory = require("./shared_modules/logger.js");
const restApiController = require("./node_components/common/rest_api_controller.js");
let m_rest_api_worker;
const dns = require('node:dns');
dns.setDefaultResultOrder("ipv4first"); //node 18 tends to take ipv6 first, this forces it to use ipv4first.
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; //as we have a self-signed example cert, we allow self-signed
// const blocked = require('blocked-at')
// blocked((time, stack) => {
// console.log(`Blocked for ${time}ms, operation started here:`, stack)
// })
//LOGGING
require('console-stamp')(console, '[HH:MM:ss.l]'); //adds HHMMss to every console log
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
//catch all uncaught exceptions - keeps the server running
process.on('uncaughtException', function (err) {
console.trace('Global unexpected error: ', err);
if (err.stack) {
err.stackTraceLimit = Infinity;
console.error(err.stack);
}
});
process.on('unhandledRejection', (reason, promise) => {
console.trace('Global unexpected error: ', reason);
if (reason.stack) {
console.error(reason.stack);
}
})
//needed for running as nexe - access to local files (database) is different
global.approot = path.dirname(process.execPath);
if (fs.existsSync(path.join(global.approot, "/database/"))) {
console.log("Running as compiled file")
} else {
global.approot = __dirname;
console.log("Running as node script - developer mode")
if (!fs.existsSync(global.approot + "/database/")) {
console.error("Database does not exist, please create it:" + global.approot + "/database/config");
}
}
//fire up logger, overrides console log
var logger = logfactory.getLogger("webint");
console.log = (...args) => logger.info.call(logger, ...args);
console.info = (...args) => logger.info.call(logger, ...args);
console.warn = (...args) => logger.warn.call(logger, ...args);
console.error = (...args) => logger.error.call(logger, ...args);
console.debug = (...args) => logger.debug.call(logger, ...args);
try {
console.log("Version: " + fs.readFileSync(path.join(global.approot, "/webinterface/version.txt")))
} catch (ex) { }
//job scheduler - TODO: reset isactive state at program start
global.jobScheduler = require("./node_components/cron_tasks/scheduled_jobs.js");
//init DB
console.log("Database file: ", global.approot + "/database/config")
global.db = {};
global.db.config = new AsyncNedb({ filename: path.join(global.approot, "database", "config") });
global.db.config.loadDatabase(function (err) { //database is vacuumed at startup
assert.equal(null, err);
});
//get global config
configmgr.get(init);
async function connectDb() {
//fire up database process
var dbpath = path.join(global.approot, "/database/job_db");
let is_initial_db_setup = await (fs.exists(dbpath));
await fs.ensureDir(dbpath);
console.log("Database path:", dbpath)
var dblogger = logfactory.getLogger("database");
global.db.mongod = new Mongod(dbpath);
let db_config_from_file = path.join(dbpath, "..", "mongo_config.json");
if (fs.existsSync(path.join(dbpath, "..", "mongo_config.json"))) {
//we have a db config file!
global.db.mongod.configFilePath = path.join(dbpath, "..", "mongo_config.json");
db_config_from_file = fs.readFileSync(path.join(dbpath, "..", "mongo_config.json"));
db_config_from_file = JSON.parse(db_config_from_file);
if (!db_config_from_file.net?.port) {
console.log("No net.port property in config file ", db_config_from_file);
process.exit(1);
}
if (!db_config_from_file.storage?.dbPath) {
console.log("No storage.dbPath property in config file ", db_config_from_file);
process.exit(1);
} else {
full_dbpath = db_config_from_file.storage.dbPath;
if (!fs.existsSync(db_config_from_file.storage.dbPath)) {
//mongod sets cwd to database folder, if a relative path is in config, it will assume it from there
full_dbpath = path.join(global.approot, "database", db_config_from_file.storage.dbPath);
}
console.log("Ensure Database path exists: ", full_dbpath);
await fs.ensureDir(full_dbpath);
}
global.db.mongod.port = db_config_from_file.net.port;
console.log("Port from Config file: ", global.db.mongod.port)
;
console.log("mongo_config.json contents:", db_config_from_file);
} else {
//no db config, choose random port
var dbPort = await portfinder.getPortPromise({ port: 8010, stopPort: 8020 });
console.log("Database port: " + dbPort);
global.db.mongod.port = dbPort;
dblogger.info("Database port: ", dbPort);
}
var db_status = await global.db.mongod.start();
if (db_status == 6) {
console.log("Database is Version 6, please delete the job_db folder and restart the application");
//force the webserver to display the update_database.html page
global.db.mongod.status = "update required";
return;
}
global.db.mongod.onStdOut = function (data) {
dblogger.info(data.toString());
}
global.db.mongod.onStdErr = function (data) {
dblogger.error(data.toString());
}
global.db.mongod.onExit = function (data) {
dblogger.info("database process exited, code: ", data);
//todo:restart DB? Anyway, we must inform clients continuously...
setTimeout(function () {
dblogger.info("Initiating connect retry in 3 seconds");
}, 3000);
}
//connect to database, store connection in global object
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:" + global.db.mongod.port + "/";//jobs
if (await (fs.exists(path.join(dbpath, "..", "mongo_connection_string.txt")))) {
url = fs.readFileSync(path.join(dbpath, "..", "mongo_connection_string.txt"));
url = url.toString().trim();
}
var mongoclient;
try {
if (db_config_from_file.replication?.replSetName && url.indexOf(",") == -1) {
url += "?directConnection=true"
}
mongoclient = await MongoClient.connect(url)
let status = await checkStatus(mongoclient);
async function checkStatus(client) {
const admin = client.db("admin");
const status = await admin.command({ hello: 1 });
if (status.isWritablePrimary) {
console.log("Connected to the Primary node.");
return "PRIMARY";
} else if (status.secondary) {
console.log("Connected to a Secondary node.");
return "SECONDARY";
} else {
return "STARTUP/OTHER";
}
}
// Check if the replica set needs to be initialized
let is_uninitialized_master = db_config_from_file.replication?.replSetName && url.indexOf(",") == -1;
if (is_uninitialized_master) {
//this is the only cluster member, if there is a client config with replication set, we must initialize it
const adminDb = mongoclient.db("admin");
try {
dblogger.info("DB Command replSetGetStatus...");
await adminDb.command({ replSetGetStatus: 1 });
} catch (e) {
if (e.codeName === "NotYetInitialized") {
dblogger.info("Initializing Replica Set...");
await adminDb.command({
replSetInitiate: {
_id: "rs0", // Must match your config file
members: [{ _id: 0, host: "localhost:" + global.db.mongod.port }]
}
});
dblogger.info("Replica Set Initialized!");
} else {
dblogger.error("Unexpected error while checking replica set", e)
}
}
}
} catch (ex) {
dblogger.error("Unexpected error connecting to DB:", ex)
setTimeout(function () {
dblogger.error("Initiating connect retry in 3 seconds");
global.socketio.emit("error", "Fatal Error connecting to job history database, view db logs and restart service!" + " Message: " + ex);
connectDb();
}, 3000);
}
const db = mongoclient.db("webinterface");
global.db.jobs = db.collection('jobs');
global.db.deleted_jobs = db.collection('deleted_jobs');
// var old_dbpath = path.join(global.approot, "/database/jobs");
// if (fs.existsSync(old_dbpath)) {
// //jobfetcher.importLegacyDatabase(old_dbpath);
// }
//ensure db indexes
//try{await global.db.jobs.createIndex({ worfklow:"text"}, { default_language: "english" });}catch(ex){};
//await global.db.jobs.dropIndexes();
ensureJobsIndexes()
db_connected_first_time();
}
async function ensureJobsIndexes() {
const desiredIndexes = [
{ key: { workflow: "text" }, name: "workflow_text" }, // text search
{ key: { workflow: 1 }, name: "workflow_1" }, // distinct() and equality queries
{ key: { job_start: 1 }, name: "job_start_1" },
{ key: { job_end: 1 }, name: "job_end_1" },
{ key: { job_start: 1, job_end: 1 }, name: "job_start_job_end" }, // compound index for date range queries
{ key: { deleted: 1 }, name: "deleted_1" },
// { key: { state: 1, job_start: 1 }, name: "state_job_start" }, // we have a compound index for state,job_start, workflow so no need for state,job_start
{ key: { state: 1, job_start: 1, workflow: 1 }, name: "state_job_workflow" }, // new compound index for filtering by state, start, and workflow
{ key: { job_id: -1 }, name: "job_id_-1" } // for distinct() logic
];
// Get existing indexes
const existingIndexes = await global.db.jobs.indexes();
console.log("Existing indexes:", existingIndexes.map(i => i.name));
// Filter out indexes that already exist by name
const indexesToCreate = desiredIndexes.filter(idx =>
!existingIndexes.some(e => {
// Compare key pattern, not just name
return JSON.stringify(e.key) === JSON.stringify(idx.key);
})
);
if (indexesToCreate.length > 0) {
// Create only the missing indexes
await global.db.jobs.createIndexes(indexesToCreate);
console.log("Created indexes:", indexesToCreate.map(i => i.name));
} else {
console.log("All indexes already exist ✅");
}
}
function db_connected_first_time() {
//these functions may depend on working database
start_cron();
}
function start_cron() {
let maintenance_funcs = require("./node_components/cron_tasks/maintenance");
cron.schedule("*/5 * * * * *", async function () {
//JOBFETCHER
if (!global.dbfetcheractive) {
global.dbfetcheractive = true;
try {
await global.jobfetcher.fetchjobs();
} catch (ex) {
console.trace("Error, jobfetcher exception. ", ex)
}
global.dbfetcheractive = false;
} else {
console.error("Jobfetcher still active, that should not happen");
}
})
cron.schedule("0 0 0-23 * * *", async function () {
//MAINTENANCE
try {
await maintenance_funcs.exec_all();
} catch (ex) {
console.error("Error in automatic Maintenance: ", ex)
}
})
maintenance_funcs.exec_all();
cron.schedule("*/2 * * * * *", function () {
//SCHEDULED JOBS
if (!global.jobscheduleractive) {
global.jobscheduleractive = true;
try {
jobScheduler.execute();
} catch (ex) {
//TODO: what to do when scheduler runs into error?
console.trace("Error, scheduler exception. " + ex)
}
global.jobscheduleractive = false;
} else {
console.error("Scheduler still active, that should not happen!");
}
});
}
async function registerAddedFolders(newline_separated_folder_list) {
if (newline_separated_folder_list) {
let list = newline_separated_folder_list.split("\n");
if (list.length == 0)
return;
for (let i = 0; i < list.length; i++) {
if (await fs.exists(list[i])) {
let parts = path.parse(list[i]);
let lastDir = parts.name;
console.log("Registering additional Webserver Folder: ", list[i], "Url:", "/" + lastDir);
app.use("/" + lastDir, express.static(list[i], {
index: false,
setHeaders: (response, file_path, file_stats) => {
// This function is called when “serve-static” makes a response.
console.log("Added folder Serving file:", file_path);
}
}));
} else {
console.error("Error registering additional folder, does not exist: ", list[i]);
}
}
}
}
async function init(conf) {
global.config = conf;
//maybe there is a better place to initialize confidential_config but nothing comes to my head rigtht now
let queryresult = await global.db.config.findOne({ "confidential_config": { $exists: true } }) || {};
if (queryresult) {
global.confidential_config = queryresult.confidential_config;
} else {
global.confidential_config = {};
}
connectDb(); //mongodb
// Intercept all requests if database needs an update
app.use((req, res, next) => {
if (global.db && global.db.mongod && global.db.mongod.status === "update required") {
if (req.accepts('html')) {
return res.sendFile(path.join(global.approot, 'webinterface', 'components', 'update_database.html'));
} else {
return res.status(503).json({ error: "Database update required" });
}
}
next();
});
// we need bodyparser to take care about the sent jsons except for the following cases
// Skip body-parser for /new_proxy routes to preserve multipart data
app.use((req, res, next) => {
const contentType = req.headers['content-type'] || '';
if (req.path.startsWith('/new_proxy') && contentType.includes('multipart/form-data')) {
return next(); //bodyparser disabled for multipart/form-data
}
try {
bodyParser.urlencoded({ extended: true })(req, res, next);
}
catch (ex) {
console.error("FATAL Error in bodyParser:", ex.stack);
}
});
app.use((req, res, next) => {
const contentType = req.headers['content-type'] || '';
if (req.path.startsWith('/new_proxy') && contentType.includes('multipart/form-data')) {
return next(); //bodyparser disabled for multipart/form-data
}
try {
bodyParser.json()(req, res, next);
} catch (ex) {
console.error("FATAL Error in bodyParser:", ex.stack);
}
});
//NON Password protected stuff
require("./node_components/metrics_control.js")(app);//metrics control must work unauthorized
app.use('/webinterface/images/F364x64.png', express.static('./webinterface/images/F364x64.png'));
//Registers user configured additinal webfolders
await registerAddedFolders(global.config.ADDITIONAL_WEBSERVER_FOLDERS_UNPROTECTED);
//mustache setup and login page
app.set('views', `${__dirname}/webinterface/components`);
//enables mustache engine when client requests file extension mustache
let mustacheInstance = mustacheExpress();
delete mustacheInstance.cache;
app.set('view engine', 'mustache');
app.engine('mustache', mustacheInstance);
//enables mustache engine when client requests file extension html, used in routes.js
app.set('view engine', 'html');
app.engine('html', mustacheInstance);
app.use('/webinterface/components/login.html', function (req, res) {
//changed from static login.html to mustache dynamically rendered
var azure_link = "";
if (global.confidential_config && global.confidential_config.azure_config) {
azure_link = global.confidential_config.azure_config.azure_login_link;
}
if (fs.existsSync(path.join(global.approot, "alternate-server/login.html"))) {
res.sendFile(path.join(global.approot, "alternate-server/login.html"));
return;
}
res.render("login.mustache",
{
instanceName: global.config.LOGIN_WELCOME_MESSAGE || '<img class="brand_image" alt="" height="20px" src="/webinterface/images/F364x64.png" title="" width="20px" style="margin-bottom:6px;float:left"> FFAStrans Web Interface',
azureLink: azure_link
}
)
});
//EVERYTHING FROM HERE IS PASSWORD PROTECTED (i think)
// required for passport
var farFuture = new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 365 * 10)); // ~10y
app.use(session({
secret: process.env.SESSION_SECRET || 'a-fallback-secret-for-dev-only',
resave: false, // Do not save session if unmodified
saveUninitialized: false, // Do not create session until something stored
cookie: { maxAge: farFuture, sameSite: 'lax' } // Add sameSite for CSRF protection
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
require("./node_components/passport/azurelogin.js");
//init job fetching cron every 3 seconds - we use cron instead of setTimeout or interval because cron might be needed in future for other stuff
console.log("Checking alternate jobfetcher", path.join(global.approot, "alternate-server/jobfetcher.js"))
if (fs.existsSync(path.join(global.approot, "alternate-server/jobfetcher.js"))) {
/* alternate server allows to disable inbuild jobfetcher - so webint can be used with another system than ffastrans*/
console.log("detected alternate jobfetcher module");
global.jobfetcher = require(path.join(global.approot, "alternate-server/jobfetcher.js"));
global.config["alternate-server"] = true;
}
else {
console.log("No alternate server detected");
global.jobfetcher = require("./node_components/cron_tasks/jobfetcher");
global.config["alternate-server"] = false;
}
if (!global.config["alternate-server"]) {
delete global.config["ffastrans-about"]; //clean up legacy stuff
//NEW REST API - replaces the builtin ffastrans api, possible
//var about_url = ("http://" + global.config["STATIC_API_HOST"] + ":" + global.config["STATIC_API_PORT"] + "/api/json/v2/about");
console.log("NOT running on alternate-server");
async function connectApi() {
//while(!got_connection){
try {
// var res = await axios.get(about_url)
restApiController.start_rest_api_thread(global.config.STATIC_API_NEW_PORT, global.config.STATIC_FFASTRANS_PATH, global.config);
got_connection = true;
// Register callback for ticket events
setTimeout(async () => {
try {
const callbackUrl = `http://127.0.0.1:${global.config.STATIC_WEBSERVER_LISTEN_PORT}/api/ticket_events`;
const registerUrl = `http://127.0.0.1:${global.config.STATIC_API_NEW_PORT}/events`;
console.log("Registering ticket event callback:", callbackUrl, "at", registerUrl);
await axios.post(registerUrl, { url: callbackUrl });
} catch (ex) {
console.error("Failed to register ticket event callback:", ex.message);
}
}, 3000); // Give the rest_service a few seconds to start up
} catch (exc) {
console.error("Cannot start rest_api_thread");
await sleep(1000);
}
//}
}
connectApi();
}
//PROXY, forward to new api, port 3003 default
var protocol = global.config.STATIC_WEBSERVER_ENABLE_HTTPS == "true" ? "https://" : "http://";
app.use('/new_proxy', proxy(protocol + global.config.STATIC_API_HOST + ":" + global.config.STATIC_API_NEW_PORT, {
limit: '500mb',
logLevel: "info",
proxyTimeout: global.config.STATIC_API_TIMEOUT,
onProxyReq: function (proxyReq, req, res) {
console.log("proxying request to:", protocol + global.config.STATIC_API_HOST + ":" + global.config.STATIC_API_NEW_PORT, req.url)
},
parseReqBody: true,
reqBodyEncoding: null,
reqAsBuffer: true,
proxyReqBodyDecorator: function (bodyContent, srcReq) {
//the "" is important here, it works around that node adds strange bytes to the request body, looks like BOM but isn't
//we actually want the body to be forwarded unmodified
console.debug("Proxying API call, request to url: ", srcReq.method, protocol + global.config.STATIC_API_HOST + ":" + global.config.STATIC_API_NEW_PORT + srcReq.url)
// Don't modify multipart/form-data requests - pass through as-is
const contentType = srcReq.headers['content-type'] || '';
if (contentType.includes('multipart/form-data')) {
console.debug("Proxying multipart/form-data - passing through unchanged");
return bodyContent;
}
if (typeof (srcReq.body) == "object") {
bodyContent = ("" + JSON.stringify(srcReq.body));
} else {
bodyContent = ("" + srcReq.body);
console.debug("Body:", srcReq.body)
}
return bodyContent;
}
}));
//log all requests
app.use(function (req, res, next) {
//console.debug("REQUEST: " + "[" + req.originalUrl + "]");
if (req.url.indexOf("temp") != -1) {
var stop = 1;
}
next();
});
//"routes"
require('./node_components/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
require('./node_components/passport/passport')(passport); // pass passport for configuration
require("./node_components/filebrowser")(app, express);
require("./node_components/getserverconfig")(app, express);
require("./node_components/views/adminconfig")(app, express);
require("./node_components/views/gethistoryjobs_dhx")(app, express);
require("./node_components/views/get_jobviewercolumns")(app, express);
require("./node_components/views/generic_json_to_html")(app, express);
require("./node_components/views/getactivejobs_dhx")(app, express);
require("./node_components/views/userlist")(app, express);
require("./node_components/views/usergrouplist")(app, express);
require("./node_components/views/usergrouprightslist")(app, express);
require("./node_components/views/getworkflowlist")(app, passport);
require("./node_components/views/getworkflowdetails")(app, passport);
require("./node_components/views/scheduledjobs")(app, passport);
require("./node_components/views/browselocations")(app, express);
require("./node_components/views/variablecolumns")(app, express);
require("./node_components/views/getjobstate")(app, express);
require("./node_components/views/localdrives")(app, express);
require("./node_components/get_userpermissions")(app, passport);
require("./node_components/activedirectory_tester.js")(app, passport);
require("./node_components/admin_alert_email_tester.js")(app, passport);
require("./node_components/farmadmin_install_service.js")(app, passport);
require("./node_components/databasemaintenance")(app, express);
require("./node_components/views/databasemaintenance_views")(app, passport);
//Registers user configured additinal webfolders
await registerAddedFolders(global.config.ADDITIONAL_WEBSERVER_FOLDERS);
//uppy overrides "all other urls" for receiving the upload using tus so it should be the last thing to load
require("./node_components/uppy")(app, passport);
//favicon
app.use('/favicon.ico', express.static('./webinterface/images/favicon.ico'));
//all routes registered?
//console.log("Express routes",app._router.stack);
//startup server
console.log('\x1b[32mHello and welcome, thank you for using FFAStrans')
var path_to_privkey = global.approot + '/cert/key.pem';
var path_to_cert = global.approot + '/cert/cert.pem';
var path_to_pfx = global.approot + '/cert/cert.pfx';
var key_password = global.config["STATIC_WEBSERVER_HTTPS_PK_PASSWORD"];
try {
if (global.config["STATIC_WEBSERVER_ENABLE_HTTPS"] == 'true') {
const https = require('https');
if (fs.existsSync(path_to_pfx)) {
//cert is pfx
httpsServer = https.createServer({
pfx: fs.readFileSync(path_to_pfx),
passphrase: key_password
}, app);
} else {
//cert is pem
httpsServer = https.createServer({
key: fs.readFileSync(path_to_privkey),
cert: fs.readFileSync(path_to_cert),
passphrase: key_password
}, app);
}
httpsServer.listen(global.config.STATIC_WEBSERVER_LISTEN_PORT, () => {
console.log('HTTPS Server running on port', global.config.STATIC_WEBSERVER_LISTEN_PORT);
initSocketIo(httpsServer);
});
} else {
startStandardHttpServer();
}
} catch (ex) {
console.error("Fatal Error starting webserver on https, using http.");
console.error(ex);
}
}
function startStandardHttpServer() {
const http = require('http').Server(app);
http.listen(global.config.STATIC_WEBSERVER_LISTEN_PORT, () => {
console.log('\x1b[36m%s\x1b[0m', 'Running on http://localhost:' + global.config.STATIC_WEBSERVER_LISTEN_PORT);
initSocketIo(http);
}).on('error', handleListenError);
}
function handleListenError(err) {
console.log(err)//prevents the program keeps running when port is in use
if (err.code == "EADDRINUSE") {
const { exec } = require('child_process');
exec('netstat -ano |findstr ' + global.config.STATIC_WEBSERVER_LISTEN_PORT, (err, stdout, stderr) => {
console.log("\nError starting webserver, please check if Port " + global.config.STATIC_WEBSERVER_LISTEN_PORT + " is in use or the server is already running... ")
if (err) {
console.log("was not able to start netstat, please enter it manually")
process.exit();
}
console.log(`stdout: ${stdout}`);
console.log("\n\n Please see above what processid (rightmost number) is LISTENING to Port " + global.config.STATIC_WEBSERVER_LISTEN_PORT + " and close the process")
process.exit();
})
}
}
function initSocketIo(created_httpserver) {
//init live connection to clients using socket.io
global.socketio = socket(created_httpserver, {
serveClient: false, // disables serving /socket.io/socket.io.js from socket.io require, we serve it in routes
path: "/ws" // sets the socket.io server path to /ws instead of default /socket.io (clients must specify same path)
});
var wildcard = socketwildcard();
global.socketio.use(wildcard);
//register supported functions
global.socketio.on('connection', function (_socket) {
global.logged_in_users_count = Math.round(global.socketio.engine.clientsCount / 2);
console.log('New socket io client connected, client: ' + _socket.id);
global.socketio.emit("logged_in_users_count", global.socketio.engine.clientsCount);
console.log("Count of concurrent connections: " + global.socketio.engine.clientsCount);
//send back the socketio id to the client
_socket.emit("socketid", _socket.id);
//register to all events from client
_socket.on('*', function (data) {
var cmd = data.data[0];
var obj = data.data[1];
if (cmd == "echo") {
_socket.emit("echo", _socket.id);
}
if (cmd == "player") {
let thisplayer = new Player();
//player has it's own socket io connection, should be ok to attach event only for this socket.
obj = JSON.parse(obj);
if (obj.file) {//opens file
thisplayer.initiate(_socket, obj);
}
return;
}
if (cmd == "deletejob") {
//obj is job_id array
try {
obj = JSON.parse(obj);
database_controller.deleteRecords(obj);
} catch (ex) {
console.log("Error deleting jobs: ", ex);
}
return;
}
})
//client disconnected
_socket.on('disconnect', function () {
global.logged_in_users_count = Math.round(global.socketio.engine.clientsCount / 2);
global.socketio.emit("logged_in_users_count", global.socketio.engine.clientsCount);
console.log("Count of concurrent connections: " + global.socketio.engine.clientsCount);
console.log('client disconnected');
});
});
}