-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
404 lines (352 loc) · 11.5 KB
/
server.js
File metadata and controls
404 lines (352 loc) · 11.5 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
/**
* @description RocketCraftServer
* REST API Server powered with
* PWA Admin Panel, mongoDB, express.
* @web maximumroulette.com
* @version 2024 ULTIMATE
* @author Nikola Lukic
* @email zlatnaspirala@gmail.com
*/
console.log("Initial params [3] => ", process.argv[3]);
const ConfigAccountSession = require("./config");
let config = new ConfigAccountSession(process.argv[3]);
const MyDatabase = require("./database/database");
let database = new MyDatabase(config);
module.exports = config;
/**
* @description Create if not exist
* all defined collections.
* @collections
* - users
*/
database.populateDatabase();
database.checkInitiallyDatabaseSize();
/**
* @description Seed fake users
* all defined collections.
* No programibillity prevent double
* seed. Just comment line manually.
* @collections
* - users
*/
// database.seedDatabase(3);
// Check launch arguments: must receive URL (localhost) and the secret
if(process.argv.length != 4) {
console.log("Usage: node " + __filename + " . ");
process.exit(-1);
}
// For demo purposes we ignore self-signed certificate
// process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
// http2 protocol
const spdy = require("spdy");
const compression = require("compression");
const cors = require("cors");
// Node imports
var express = require("express");
var fs = require("fs");
const Crypto = require("./common/crypto");
var crypto = new Crypto();
// http1.1
var https = require('https');
var http = require('http');
// Pull information from HTML POST (express4)
var bodyParser = require("body-parser");
// const { endianness } = require( "os" );
var app = express();
var URL_ARG = process.argv[2];
var options = null;
var hostingHTTP = express();
vhost = require('vhost');
/**
* @description
* SWITCH TO SINGLE SERVER
* ALSO CAN BE USED LIKE API SERVER AND HOST SERVER
*/
// this.app.use(root.express.static(__dirname + "./../../admin-panel/dist"));
/**
* @description
* Subdomains and routes.
*/
routerSub = express.Router()
routerSub.use(express.static('/var/www/html/apps/ultimate-roulette/'));
hostingHTTP.use(vhost('roulette.maximumroulette.com', routerSub));
routerSub2 = express.Router()
routerSub2.use(express.static('/var/www/html/apps/barbarian-road-mashines/beta/'));
app.use(vhost('brm.maximumroulette.com', routerSub2));
hostingHTTP.use(vhost('brm.maximumroulette.com', routerSub2));
routerSub3 = express.Router()
routerSub3.use(express.static('/var/www/html/apps/ai/artificial-intelligence/tools/visual-sound/'));
hostingHTTP.use(vhost('ai.maximumroulette.com', routerSub3));
app.use(vhost('ai.maximumroulette.com', routerSub3));
routerSub4 = express.Router()
routerSub4.use(express.static('/var/applications/kure/kure-server-client/public/'));
hostingHTTP.use(vhost('kure.maximumroulette.com', routerSub4));
hostingHTTP.get('*', function(req, res, next) {
// console.log(":", req.hostname);
next();
});
hostingHTTP.use(express.static("/var/www/html/"));
hostingHTTP.use("/safir", express.static(config.hostSpecialRoute.route2));
hostingHTTP.use("/storage", express.static(config.hostSpecialRoute.route3));
// Local
// hostingHTTP.use(express.static("G:\\web_server\\xampp\\htdocs\\PRIVATE_SERVER\\ROCKET-SERVER\\rocket-craft-server\\admin-panel\\dist"));
/**
* @description Compression, Cors
* @example `express.compress()`
*/
hostingHTTP.use(cors());
hostingHTTP.use(function(req, res, next) {
// res.setHeader("Content-Type", "text/html")
// res.setHeader('Content-Encoding', 'gzip');
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', false);
// Pass to next layer of middleware
next();
});
app.use(bodyParser.json({limit: config.maxRequestSize}))
app.use(cors());
/**
* @description
* You can add samo special route for any proporse.
* Not work if you have 443 https started at same domain,
* it redirect to https and rocket crating server hostend on 80
* is not reached.
*/
if(config.hostSpecialRoute.active) {
app.use("/admin", express.static(config.hostSpecialRoute.route));
app.use("/safir", express.static(config.hostSpecialRoute.route2));
app.use("/storage", express.static(config.hostSpecialRoute.route3));
app.use("/brm", express.static('/var/www/html/apps/barbarian-road-mashines/beta/'));
app.use("/apps/barbarian-road-mashines/beta", express.static('/var/www/html/apps/barbarian-road-mashines/beta/'));
app.use("/apps/shooter/", express.static('/var/www/html/apps/shooter/'));
app.use("/apps/hang3d/", express.static('/var/www/html/apps/hang3d/'));
console.log("Rocket activate " + config.hostSpecialRoute.webAppName + " application host." +
" Application www folder is `" + config.hostSpecialRoute.route + " __dirname is " + __dirname);
}
/**
* @description Allow or disallow headers flags.
* Add headers
*/
app.use(function(req, res, next) {
// console.log("HOSTING SERVER PREVENT OTHER DOMAINS+")
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
// Parse application/x-www-form-urlencoded
app.use(
bodyParser.urlencoded({
extended: "true"
})
);
// Parse application/json
app.use(bodyParser.json());
app.use(
bodyParser.json({
type: "application/vnd.api+json"
})
);
// AdminPanel
let routerProfileDeleteAdmin = new require('./api/admin/admin-profile')(app, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot,
database: database
},
crypto);
// Parse application/vnd.api+json as json
// Paths like `./api/account/account` and `/api/users/users` are just path to the script
// nothing wiht routes !
let routerRocket = new require('./api/account/account')(app, express, database, crypto);
let routerUsers = new require('./api/users/users')(app, express, database, crypto);
let routerProfile = new require('./api/profile/profile')(app, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot,
database: database
},
crypto
);
let routerProfileWannaPlay = new require('./api/crafting/active-list')(app, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot
},
crypto
);
let routerVisitors = new require('./api/visitors/visitors')(app, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot
},
crypto
);
// hostingHTTP part.
// hostingHTTP.use(bodyParser.json({ limit: config.maxRequestSize }))
hostingHTTP.use(bodyParser.json());
/**
* @description
* GamePlay Servers part
*/
if(config.gameServers.matrixRoulette.active == true) {
// let clients = [];
let facts = [];
const matrixRouletteCore = require("./game-servers/matrix-roulette/core.js");
matrixRouletteCore.clients = [];
matrixRouletteCore.init();
hostingHTTP.get('/matrix-roulette-status', (request, response) => response.json({clients: matrixRouletteCore.clients.length}));
function eventsHandler(request, response, next) {
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
};
response.writeHead(200, headers);
const data = `data: ${JSON.stringify(facts)}\n\n`;
response.write(data);
const clientId = Date.now();
const newClient = {id: clientId, response};
matrixRouletteCore.clients.push(newClient);
request.on('close', () => {
console.log(`${clientId} Connection closed`);
matrixRouletteCore.clients = matrixRouletteCore.clients.filter(client => client.id !== clientId);
});
}
hostingHTTP.get('/matrix-roulette', eventsHandler);
async function addFact(request, respsonse, next) {
const newFact = request.body;
facts.push(newFact);
respsonse.json(newFact)
return sendEventsToAll(newFact);
}
//
// Update from clients
// hostingHTTP.post('/fact', addFact);
}
let routerProfileDeleteAdmin2 = new require('./api/admin/admin-profile')(
hostingHTTP, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot,
database: database
},
crypto);
// Parse application/vnd.api+json as json
let routerRocket2 = new require('./api/account/account')(hostingHTTP, express, database, crypto);
let routerUsers2 = new require('./api/users/users')(hostingHTTP, express, database, crypto);
let routerProfile2 = new require('./api/profile/profile')(
hostingHTTP,
express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot,
database: database
},
crypto
);
let routerProfileWannaPlay2 = new require('./api/crafting/active-list')(hostingHTTP, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot
},
crypto
);
let routerFOHBLogs = new require('./api/fohb/active-list')(hostingHTTP, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot
},
crypto
);
let routerVisitors2 = new require('./api/visitors/visitors')(hostingHTTP, express,
{
dbName: config.databaseName,
dbRoot: config.getDatabaseRoot
},
crypto
);
// Server configuration - compression.
// At the moment conflicted with client webworker cache feature.
// app.use(express.static(__dirname + "/public"));
// app.use(compression({ filter: shouldCompress }));
function shouldCompress(req, res) {
if(req.headers["x-no-compression"]) {
// don't compress responses with this request header
return false;
}
// fallback to standard filter function
return compression.filter(req, res);
}
/**
* @description
* Forsed https for now.
* For local pass arg `localhost`
*/
if(URL_ARG.indexOf("localhost") !== -1) {
options = {
key: fs.readFileSync(__dirname + "/self-cert/privatekey.pem"),
cert: fs.readFileSync(__dirname + "/self-cert/certificate.pem")
};
} else {
options = {
key: fs.readFileSync("/etc/letsencrypt/live/ai.maximumroulette.com-0001/privkey.pem"),
cert: fs.readFileSync("/etc/letsencrypt/live/ai.maximumroulette.com-0001/fullchain.pem")
};
}
/**
* @description
* Selector
*/
var serverRunner = null;
if(config.protocol == 'http') {
serverRunner = http;
} else if(config.protocol == 'https') {
serverRunner = https;
} else if(config.protocol == 'http2') {
serverRunner = spdy;
}
/**
* @description serverRunner
* Run `rocket-crafting-server` web server.
* [SINGLE SERVER DISABLED]
* DOUBLE - WE HOST ROCKET CRAFTING SERVER ON HTTP AND HTTPS.
*/
http.createServer(options, app).listen(config.connectorPort, error => {
if(error) {
console.warn("Something wrong with rocket-crafting server.");
console.error(error);
return process.exit(1);
} else {
console.log("Rocket started at " + config.connectorPort + " port.");
}
});
/**
* @description
* Secured with SSL.
*/
if(config.ownHttp) {
https.createServer(options, hostingHTTP).listen(config.ownHttpHostPort, error => {
if(error) {
console.warn("Something wrong with rocket-craft own host server.");
console.error(error);
return process.exit(1);
} else {
console.info("Rocket host started at " + config.ownHttpHostPort + " port.");
}
});
}