cloudcms-server 3.2.284 → 3.2.285

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,9 +14,9 @@ module.exports = function()
14
14
  if (!process.env.CLOUDCMS_BROADCAST_TYPE) {
15
15
  process.env.CLOUDCMS_BROADCAST_TYPE = "local";
16
16
 
17
- if (process.configuration.setup !== "single") {
18
- process.env.CLOUDCMS_BROADCAST_TYPE = "redis";
19
- }
17
+ // if (process.configuration.setup !== "single") {
18
+ // process.env.CLOUDCMS_BROADCAST_TYPE = "redis";
19
+ // }
20
20
  }
21
21
 
22
22
  var config = process.configuration;
package/d1/index.js ADDED
@@ -0,0 +1,629 @@
1
+ delete process.env.NODE_DEBUG;
2
+
3
+ var moment = require("moment");
4
+ var cluster = require("cluster");
5
+ var http = require("http");
6
+ var os = require("os");
7
+ var numCPUs = os.cpus().length;
8
+
9
+ // default agents
10
+ process.defaultHttpTimeoutMs = 60000;
11
+ var HttpKeepAliveAgent = require('agentkeepalive');
12
+ http.globalAgent = new HttpKeepAliveAgent({
13
+ keepAlive: true,
14
+ keepAliveMsecs: 5000,
15
+ maxSockets: 16000,
16
+ maxFreeSockets: 256,
17
+ timeout: process.defaultHttpTimeoutMs,
18
+ freeSocketTimeout: 4000
19
+ });
20
+
21
+ require("ssl-root-cas").inject();
22
+
23
+ var REDIS_URL = "redis://redis.default.svc.cluster.local:6379";
24
+
25
+ // track temporary files
26
+ var temp = require('temp');
27
+ const https = require("https");
28
+ const {setupMaster} = require("@socket.io/sticky");
29
+ temp.track();
30
+
31
+ var initWorker = function(allDone)
32
+ {
33
+ var initSession = function (initDone) {
34
+ var sessionSecret = "secret";
35
+ var sessionConfig = {
36
+ secret: sessionSecret,
37
+ resave: false,
38
+ saveUninitialized: false
39
+ };
40
+
41
+ const connectRedis = require('connect-redis');
42
+ var session = require('express-session');
43
+ var RedisStore = connectRedis(session);
44
+ var IORedis = require("ioredis");
45
+ var redisClient = new IORedis(REDIS_URL);
46
+ sessionConfig.store = new RedisStore({client: redisClient});
47
+ initDone(null, session(sessionConfig));
48
+ //
49
+ // var options = {};
50
+ // options.checkPeriod = 86400000; // prune expired entries every 24h
51
+ //
52
+ // // session memory store
53
+ // var MemoryStore = require('memorystore')(session);
54
+ // sessionConfig.store = new MemoryStore(options);
55
+ // initDone(null, session(sessionConfig));
56
+ };
57
+
58
+ var setHeaderOnce = exports.setHeaderOnce = function (response, name, value) {
59
+ var existing = response.getHeader(name);
60
+ if (typeof (existing) === "undefined") {
61
+ setHeader(response, name, value);
62
+ }
63
+ };
64
+
65
+ var setHeader = exports.setHeader = function (response, name, value) {
66
+ try {
67
+ response.setHeader(name, value);
68
+ } catch (e) {
69
+ }
70
+ };
71
+
72
+ var createProxyHandler = function (protocol, hostname, port, pathPrefix) {
73
+ const proxy = require("http2-proxy");
74
+ const finalhandler = require('finalhandler')
75
+
76
+ const defaultWebHandler = function (err, req, res) {
77
+ if (err) {
78
+ console.log("A web proxy error was caught, path: " + req.path + ", err: ", err);
79
+ try {
80
+ res.status(500);
81
+ } catch (e) {
82
+ }
83
+ try {
84
+ res.end('Something went wrong while proxying the request.');
85
+ } catch (e) {
86
+ }
87
+ }
88
+
89
+ finalhandler(req, res)(err);
90
+ };
91
+
92
+ // web
93
+ var webConfig = {};
94
+ webConfig.hostname = hostname;
95
+ webConfig.port = port;
96
+ webConfig.protocol = protocol;
97
+ //webConfig.path = null;
98
+ webConfig.timeout = 120000;
99
+ webConfig.proxyTimeout = 120000;
100
+ webConfig.proxyName = "Cloud CMS UI Proxy";
101
+ webConfig.onReq = function (req, options) {
102
+
103
+ if (!options.headers) {
104
+ options.headers = {};
105
+ }
106
+ var headers = options.headers;
107
+
108
+ console.log("path: " + options.path);
109
+
110
+ if (options.path && options.path.startsWith("/proxy")) {
111
+ options.path = options.path.substring(6);
112
+ }
113
+
114
+ if (pathPrefix) {
115
+ options.path = path.join(pathPrefix, options.path);
116
+ }
117
+
118
+ // used to auto-assign the client header for /oauth/token requests
119
+ //oauth2.autoProxy(req);
120
+
121
+ // copy domain host into "x-cloudcms-domainhost"
122
+ if (req.domainHost) {
123
+ headers["x-cloudcms-domainhost"] = req.domainHost; // this could be "localhost"
124
+ }
125
+
126
+ // copy virtual host into "x-cloudcms-virtualhost"
127
+ if (req.virtualHost) {
128
+ headers["x-cloudcms-virtualhost"] = req.virtualHost; // this could be "root.cloudcms.net" or "abc.cloudcms.net"
129
+ }
130
+
131
+ // copy deployment descriptor info
132
+ if (req.descriptor) {
133
+ if (req.descriptor.tenant) {
134
+ if (req.descriptor.tenant.id) {
135
+ headers["x-cloudcms-tenant-id"] = req.descriptor.tenant.id;
136
+ }
137
+
138
+ if (req.descriptor.tenant.title) {
139
+ headers["x-cloudcms-tenant-title"] = req.descriptor.tenant.title;
140
+ }
141
+ }
142
+
143
+ if (req.descriptor.application) {
144
+ if (req.descriptor.application.id) {
145
+ headers["x-cloudcms-application-id"] = req.descriptor.application.id;
146
+ }
147
+
148
+ if (req.descriptor.application.title) {
149
+ headers["x-cloudcms-application-title"] = req.descriptor.application.title;
150
+ }
151
+ }
152
+ }
153
+
154
+ // set optional "x-cloudcms-origin" header
155
+ var cloudcmsOrigin = null;
156
+ if (req.virtualHost) {
157
+ cloudcmsOrigin = req.virtualHost;
158
+ }
159
+ if (cloudcmsOrigin) {
160
+ headers["x-cloudcms-origin"] = cloudcmsOrigin;
161
+ }
162
+
163
+ // set x-cloudcms-server-version header
164
+ //headers["x-cloudcms-server-version"] = process.env.CLOUDCMS_APPSERVER_PACKAGE_VERSION;
165
+
166
+ // keep alive
167
+ //req.headers["connection"] = "keep-alive";
168
+
169
+ // if the incoming request didn't have an "Authorization" header
170
+ // and we have a logged in Gitana User via Auth, then set authorization header to Bearer Access Token
171
+ if (!req.headers["authorization"]) {
172
+ if (req.gitana_user) {
173
+ headers["authorization"] = "Bearer " + req.gitana_user.getDriver().http.accessToken();
174
+ } else if (req.gitana_proxy_access_token) {
175
+ headers["authorization"] = "Bearer " + req.gitana_proxy_access_token;
176
+ }
177
+ }
178
+ };
179
+ webConfig.onRes = function (req, res, proxyRes) {
180
+
181
+ var chunks = [];
182
+
183
+ // triggers on data receive
184
+ proxyRes.on('data', function (chunk) {
185
+ // add received chunk to chunks array
186
+ chunks.push(chunk);
187
+ });
188
+
189
+ proxyRes.on("end", function () {
190
+
191
+ if (proxyRes.statusCode === 401) {
192
+ var text = "" + Buffer.concat(chunks);
193
+ if (text && (text.indexOf("invalid_token") > -1) || (text.indexOf("invalid_grant") > -1)) {
194
+ console.log("ah1");
195
+ }
196
+ }
197
+ });
198
+
199
+ //res.setHeader('x-powered-by', 'cloudcms');
200
+ res.writeHead(proxyRes.statusCode, proxyRes.headers)
201
+ proxyRes.pipe(res)
202
+ };
203
+
204
+ var proxyRequestHandler = function (req, res) {
205
+ proxy.web(req, res, webConfig, function (err, req, res) {
206
+ defaultWebHandler(err, req, res);
207
+ });
208
+ };
209
+
210
+ return proxyRequestHandler;
211
+ };
212
+
213
+ var express = require('express');
214
+
215
+ var app = express();
216
+ app.disable('x-powered-by');
217
+
218
+ var bodyParser = require("body-parser");
219
+ var multipart = require("connect-multiparty");
220
+ var flash = require("connect-flash");
221
+
222
+ var bodyParserFn = function () {
223
+ return function (req, res, next) {
224
+ if (req._body) {
225
+ return next();
226
+ }
227
+
228
+ var contentType = req.get("Content-Type");
229
+ //if (contentType == "application/json" && req.method.toLowerCase() == "post") {
230
+ if (req.method.toLowerCase() == "post") {
231
+
232
+ req._body = true;
233
+
234
+ var responseString = "";
235
+
236
+ req.on('data', function (data) {
237
+ responseString += data;
238
+ });
239
+
240
+ req.on('end', function () {
241
+
242
+ if (responseString.length > 0) {
243
+
244
+ try {
245
+ var b = JSON.parse(responseString);
246
+ if (b) {
247
+ req.body = b;
248
+ }
249
+ } catch (e) {
250
+ }
251
+ }
252
+
253
+ next();
254
+ });
255
+ } else {
256
+ next();
257
+ }
258
+ };
259
+ };
260
+
261
+ initSession(function () {
262
+
263
+ // middleware
264
+ app.enable('strict routing');
265
+ app.set('port', 3000);
266
+ // custom morgan logger
267
+ var morgan = require("morgan");
268
+ morgan(function (tokens, req, res) {
269
+
270
+ var status = res.statusCode;
271
+ var len = parseInt(res.getHeader('Content-Length'), 10);
272
+ var host = req.domainHost;
273
+ if (req.virtualHost) {
274
+ host = req.virtualHost;
275
+ }
276
+
277
+ len = isNaN(len) ? '0b' : len = bytes(len);
278
+
279
+ var d = new Date();
280
+ var dateString = d.toDateString();
281
+ var timeString = d.toTimeString();
282
+
283
+ // gray color
284
+ var grayColor = "\x1b[90m";
285
+
286
+ // status color
287
+ var color = 32;
288
+ if (status >= 500) {
289
+ color = 31;
290
+ } else if (status >= 400) {
291
+ color = 33;
292
+ } else if (status >= 300) {
293
+ color = 36;
294
+ }
295
+ var statusColor = "\x1b[" + color + "m";
296
+
297
+ // final color
298
+ var finalColor = "\x1b[0m";
299
+
300
+ if (process.env.CLOUDCMS_APPSERVER_MODE === "production") {
301
+ grayColor = "";
302
+ statusColor = "";
303
+ finalColor = "";
304
+ }
305
+
306
+ var message = '';
307
+ message += grayColor + '<' + req.id + '> ';
308
+ message += grayColor + '[' + dateString + ' ' + timeString + '] ';
309
+ message += grayColor + host + ' ';
310
+ //message += grayColor + '(' + req.ip + ') ';
311
+ message += statusColor + res.statusCode + ' ';
312
+ message += statusColor + (new Date - req._startTime) + ' ms ';
313
+ message += grayColor + '"' + req.method + ' ';
314
+ message += grayColor + req.originalUrl + '" ';
315
+ message += grayColor + len + ' ';
316
+ message += finalColor;
317
+
318
+ return message;
319
+ });
320
+ // add req.id
321
+ var requestCounter = 0;
322
+ app.use(function (req, res, next) {
323
+ requestCounter++;
324
+ req.id = requestCounter;
325
+ next();
326
+ });
327
+ // retain originalUrl and originalPath since these can get modified along the way
328
+ app.use(function (req, res, next) {
329
+ req.originalUrl = req.url;
330
+ req.originalPath = req.path;
331
+ next();
332
+ });
333
+ // req.param method
334
+ var requestParam = require("request-param")();
335
+ app.use(requestParam);
336
+ // add req.log function
337
+ app.use(function (req, res, next) {
338
+
339
+ req._log = req.log = function (text/*, warn*/) {
340
+
341
+ var host = req.domainHost;
342
+ if (req.virtualHost) {
343
+ host = req.virtualHost;
344
+ }
345
+
346
+ var timestamp = moment(new Date()).format("MM/DD/YYYY HH:mm:ss Z");
347
+ var grayColor = "\x1b[90m";
348
+ var finalColor = "\x1b[0m";
349
+
350
+ // in production, don't use colors
351
+ if (process.env.CLOUDCMS_APPSERVER_MODE === "production") {
352
+ grayColor = "";
353
+ finalColor = "";
354
+ }
355
+
356
+ var message = '';
357
+ message += grayColor + '<' + req.id + '> ';
358
+ if (cluster.worker && cluster.worker.id) {
359
+ message += grayColor + '(' + cluster.worker.id + ') ';
360
+ }
361
+ message += grayColor + '[' + timestamp + '] ';
362
+ message += grayColor + host + ' ';
363
+ message += grayColor + text + '';
364
+ message += finalColor;
365
+
366
+ /*
367
+ if (warn)
368
+ {
369
+ message = "\r\n**** SLOW RESPONSE ****\r\n" + message + "\r\n";
370
+ }
371
+ */
372
+
373
+ console.log(message);
374
+ };
375
+
376
+ next();
377
+ });
378
+
379
+ // TODO: SKIP THIS FOR NOW
380
+ // common interceptors and config
381
+ //main.common1(app);
382
+
383
+ // general logging of requests
384
+ // gather statistics on response time
385
+ var responseTime = require("response-time");
386
+ app.use(responseTime(function (req, res, time) {
387
+
388
+ var warn = false;
389
+ if (time > 1000) {
390
+ warn = true;
391
+ }
392
+
393
+ var requestPath = req.originalPath;
394
+ if (requestPath) {
395
+ var filter = false;
396
+ if (requestPath.indexOf("/login") > -1) {
397
+ filter = true;
398
+ }
399
+ if (requestPath.indexOf("/token") > -1) {
400
+ filter = true;
401
+ }
402
+ // if (filter)
403
+ // {
404
+ // requestPath = util.stripQueryStringFromUrl(requestPath);
405
+ // }
406
+ }
407
+
408
+ req.log(req.method + " " + requestPath + " [" + res.statusCode + "] (" + time.toFixed(2) + " ms)", warn);
409
+ }));
410
+
411
+ // TODO
412
+ // // set up CORS allowances
413
+ // // this lets CORS requests float through the proxy
414
+ app.use(function (req, res, next) {
415
+
416
+ var origin = null;
417
+ if (!origin) {
418
+ origin = req.headers["origin"];
419
+ }
420
+ if (!origin) {
421
+ origin = req.headers["x-cloudcms-origin"];
422
+ }
423
+ if (!origin) {
424
+ origin = "*";
425
+ }
426
+
427
+ // var methods = null
428
+ // var headers = null;
429
+ // var credentials = null;
430
+
431
+ setHeaderOnce(res, "Access-Control-Allow-Origin", origin);
432
+
433
+ // if (methods)
434
+ // {
435
+ // setHeaderOnce(res, "Access-Control-Allow-Methods", methods);
436
+ // }
437
+ //
438
+ // if (headers)
439
+ // {
440
+ // setHeaderOnce(res, "Access-Control-Allow-Headers", headers);
441
+ // }
442
+ //
443
+ // if (credentials)
444
+ // {
445
+ // setHeaderOnce(res, "Access-Control-Allow-Credentials", "" + credentials);
446
+ // }
447
+
448
+ // res.set('Access-Control-Allow-Max-Age', 3600);
449
+
450
+ if ('OPTIONS' === req.method) {
451
+ return res.sendStatus(200);
452
+ }
453
+
454
+ next();
455
+ });
456
+
457
+ //
458
+ // set up default security headers
459
+ app.use(function (req, res, next) {
460
+
461
+ // defaults
462
+ var xFrameOptions = "SAMEORIGIN";
463
+ var xXssProtection = "1; mode=block";
464
+
465
+ // TODO: allow overrides here?
466
+
467
+ if (xFrameOptions) {
468
+ setHeaderOnce(res, "X-Frame-Options", xFrameOptions);
469
+ }
470
+
471
+ if (xXssProtection) {
472
+ setHeaderOnce(res, "X-XSS-Protection", xXssProtection)
473
+ }
474
+
475
+ setHeaderOnce(res, "X-Powered-By", "Cloud CMS");
476
+
477
+ next();
478
+ });
479
+
480
+ // TODO: SKIP THIS FOR NOW
481
+ // // common interceptors and config
482
+ // main.common2(app);
483
+
484
+ // TODO: SKIP THIS FOR NOW
485
+ // binds gitana driver into place
486
+ // main.common3(app);
487
+
488
+ // parse cookies
489
+ var cookieParser = require('cookie-parser');
490
+ app.use(cookieParser());
491
+
492
+ // TODO: SKIP THIS FOR NOW
493
+ // // cloudcms things need to run here
494
+ // main.common4(app, true);
495
+
496
+ // TODO
497
+ // PATH BASED PERFORMANCE CACHING
498
+ //main.perf1(app);
499
+
500
+ var proxyRequestHandler = createProxyHandler("http", "api.default.svc.cluster.local", 80);
501
+ app.use(function (req, res) {
502
+ req.virtualHost = "mt85.us1.cloudcms.net";
503
+ proxyRequestHandler(req, res);
504
+ });
505
+
506
+ // standard body parsing + a special cloud cms body parser that makes a last ditch effort for anything
507
+ // that might be JSON (regardless of content type)
508
+ app.use(function (req, res, next) {
509
+
510
+ multipart({})(req, res, function (err) {
511
+ bodyParser.json({})(req, res, function (err) {
512
+ bodyParser.urlencoded({})(req, res, function (err) {
513
+ bodyParserFn()(req, res, function (err) {
514
+ next(err);
515
+ });
516
+ });
517
+ });
518
+ });
519
+
520
+ });
521
+
522
+ app.use(flash());
523
+
524
+ // var server = app.listen(3000, function() {
525
+ // console.log('Process ' + process.pid + ' is listening to all incoming requests');
526
+ // });
527
+ //const server = http.createServer()
528
+ var server = http.Server(app);
529
+
530
+ // request timeout
531
+ var requestTimeout = 30000; // 30 seconds
532
+ server.setTimeout(requestTimeout);
533
+
534
+ // socket
535
+ server.on("connection", function (socket) {
536
+ console.log("server connection");
537
+ socket.setNoDelay(true);
538
+ });
539
+
540
+ // configure socket IO
541
+ const { setupWorker } = require("@socket.io/sticky");
542
+ const { Server } = require("socket.io");
543
+ const { createAdapter } = require("@socket.io/redis-adapter");
544
+ const IORedis = require("ioredis");
545
+
546
+ var pubClient = new IORedis(REDIS_URL);
547
+ var subClient = pubClient.duplicate();
548
+
549
+ const io = new Server(server);
550
+ server.io = io;
551
+
552
+ io.engine.on("connection_error", function(err) {
553
+ // console.log("CONNECTION ERROR");
554
+ // console.log("REQUEST: ", err.req); // the request object
555
+ // console.log("CODE: " + err.code); // the error code, for example 1
556
+ // console.log("MESSAGE: ", err.message); // the error message, for example "Session ID unknown"
557
+ // console.log("CONTEXT: ", err.context); // some additional error context
558
+ });
559
+
560
+ // use the redis adapter
561
+ io.adapter(createAdapter(pubClient, subClient, {
562
+ //publishOnSpecificResponseChannel: true
563
+ }));
564
+
565
+ // setup connection with the primary process
566
+ setupWorker(io);
567
+
568
+ // on connect
569
+ io.on("connection", (socket) => {
570
+ //console.log("Redis Launcher on('connection') - socket id:" + socket.id);
571
+ socket.on('message', function(m) {
572
+ console.log("Socket Connection message: " + m);
573
+ });
574
+
575
+ // always catch err
576
+ socket.on("error", function(err) {
577
+ console.log("Caught socket error");
578
+ console.log(err.stack);
579
+ });
580
+
581
+ // TODO
582
+ });
583
+
584
+ allDone(null, server);
585
+ });
586
+ }
587
+
588
+ if (cluster.isMaster)
589
+ {
590
+ console.log(`Primary ${process.pid} is starting`);
591
+
592
+ var server = http.createServer();
593
+ //server.listen(3000);
594
+
595
+ const { setupMaster } = require("@socket.io/sticky");
596
+ const { setupPrimary } = require("@socket.io/cluster-adapter");
597
+
598
+ // setup connections between the workers
599
+ setupPrimary();
600
+
601
+ // needed for packets containing buffers
602
+ cluster.setupMaster({
603
+ serialization: "advanced"
604
+ });
605
+
606
+ // setup sticky sessions
607
+ setupMaster(server, {
608
+ //loadBalancingMethod: "least-connection"
609
+ loadBalancingMethod: "round-robin"
610
+ });
611
+
612
+ // Fork workers.
613
+ for (let i = 0; i < numCPUs; i++) {
614
+ cluster.fork();
615
+ }
616
+
617
+ cluster.on('exit', (worker, code, signal) => {
618
+ console.log(`Worker ${worker.process.pid} died`);
619
+ });
620
+
621
+ console.log(`Primary ${process.pid} is running`);
622
+ }
623
+ else
624
+ {
625
+ initWorker(function(err, server) {
626
+ server.listen(3000);
627
+ console.log(`Worker ${process.pid} started`);
628
+ });
629
+ }
@@ -0,0 +1,203 @@
1
+ var createProxyHandler = function(protocol, hostname, port, pathPrefix)
2
+ {
3
+ const proxy = require("http2-proxy");
4
+ const finalhandler = require('finalhandler')
5
+
6
+ const defaultWebHandler = function(err, req, res) {
7
+ if (err)
8
+ {
9
+ console.log("A web proxy error was caught, path: " + req.path + ", err: ", err);
10
+ try { res.status(500); } catch (e) { }
11
+ try { res.end('Something went wrong while proxying the request.'); } catch (e) { }
12
+ }
13
+
14
+ finalhandler(req, res)(err);
15
+ };
16
+
17
+ // web
18
+ var webConfig = {};
19
+ webConfig.hostname = hostname;
20
+ webConfig.port = port;
21
+ webConfig.protocol = protocol;
22
+ //webConfig.path = null;
23
+ webConfig.timeout = 120000;
24
+ webConfig.proxyTimeout = 120000;
25
+ webConfig.proxyName = "Cloud CMS UI Proxy";
26
+ webConfig.onReq = function(req, options) {
27
+
28
+ if (!options.headers) {
29
+ options.headers = {};
30
+ }
31
+ var headers = options.headers;
32
+
33
+ if (options.path && options.path.startsWith("/proxy")) {
34
+ options.path = options.path.substring(6);
35
+ }
36
+
37
+ if (pathPrefix) {
38
+ options.path = path.join(pathPrefix, options.path);
39
+ }
40
+
41
+ // used to auto-assign the client header for /oauth/token requests
42
+ //oauth2.autoProxy(req);
43
+
44
+ // copy domain host into "x-cloudcms-domainhost"
45
+ if (req.domainHost) {
46
+ headers["x-cloudcms-domainhost"] = req.domainHost; // this could be "localhost"
47
+ }
48
+
49
+ // copy virtual host into "x-cloudcms-virtualhost"
50
+ if (req.virtualHost) {
51
+ headers["x-cloudcms-virtualhost"] = req.virtualHost; // this could be "root.cloudcms.net" or "abc.cloudcms.net"
52
+ }
53
+
54
+ // copy deployment descriptor info
55
+ if (req.descriptor)
56
+ {
57
+ if (req.descriptor.tenant)
58
+ {
59
+ if (req.descriptor.tenant.id)
60
+ {
61
+ headers["x-cloudcms-tenant-id"] = req.descriptor.tenant.id;
62
+ }
63
+
64
+ if (req.descriptor.tenant.title)
65
+ {
66
+ headers["x-cloudcms-tenant-title"] = req.descriptor.tenant.title;
67
+ }
68
+ }
69
+
70
+ if (req.descriptor.application)
71
+ {
72
+ if (req.descriptor.application.id)
73
+ {
74
+ headers["x-cloudcms-application-id"] = req.descriptor.application.id;
75
+ }
76
+
77
+ if (req.descriptor.application.title)
78
+ {
79
+ headers["x-cloudcms-application-title"] = req.descriptor.application.title;
80
+ }
81
+ }
82
+ }
83
+
84
+ // set optional "x-cloudcms-origin" header
85
+ var cloudcmsOrigin = null;
86
+ if (req.virtualHost)
87
+ {
88
+ cloudcmsOrigin = req.virtualHost;
89
+ }
90
+ if (cloudcmsOrigin)
91
+ {
92
+ headers["x-cloudcms-origin"] = cloudcmsOrigin;
93
+ }
94
+
95
+ // set x-cloudcms-server-version header
96
+ //headers["x-cloudcms-server-version"] = process.env.CLOUDCMS_APPSERVER_PACKAGE_VERSION;
97
+
98
+ // keep alive
99
+ //req.headers["connection"] = "keep-alive";
100
+
101
+ // if the incoming request didn't have an "Authorization" header
102
+ // and we have a logged in Gitana User via Auth, then set authorization header to Bearer Access Token
103
+ if (!req.headers["authorization"])
104
+ {
105
+ if (req.gitana_user)
106
+ {
107
+ headers["authorization"] = "Bearer " + req.gitana_user.getDriver().http.accessToken();
108
+ }
109
+ else if (req.gitana_proxy_access_token)
110
+ {
111
+ headers["authorization"] = "Bearer " + req.gitana_proxy_access_token;
112
+ }
113
+ }
114
+ };
115
+ webConfig.onRes = function(req, res, proxyRes) {
116
+
117
+ // if (req.gitana_user)
118
+ // {
119
+ // var chunks = [];
120
+ //
121
+ // // triggers on data receive
122
+ // proxyRes.on('data', function(chunk) {
123
+ // // add received chunk to chunks array
124
+ // chunks.push(chunk);
125
+ // });
126
+ //
127
+ // proxyRes.on("end", function () {
128
+ //
129
+ // if (proxyRes.statusCode === 401)
130
+ // {
131
+ // var text = "" + Buffer.concat(chunks);
132
+ // if (text && (text.indexOf("invalid_token") > -1) || (text.indexOf("invalid_grant") > -1))
133
+ // {
134
+ // var identifier = req.identity_properties.provider_id + "/" + req.identity_properties.user_identifier;
135
+ //
136
+ // _LOCK([identifier], function(err, releaseLockFn) {
137
+ //
138
+ // if (err)
139
+ // {
140
+ // // failed to acquire lock
141
+ // console.log("FAILED TO ACQUIRE LOCK", err);
142
+ // req.log("FAILED TO ACQUIRE LOCK", err);
143
+ // try { releaseLockFn(); } catch (e) { }
144
+ // return;
145
+ // }
146
+ //
147
+ // var cleanup = function (full)
148
+ // {
149
+ // delete Gitana.APPS[req.identity_properties.token];
150
+ // delete Gitana.PLATFORM_CACHE[req.identity_properties.token];
151
+ //
152
+ // if (full) {
153
+ // auth.removeUserCacheEntry(identifier);
154
+ // }
155
+ // };
156
+ //
157
+ // // null out the access token
158
+ // // this will force the refresh token to be used to get a new one on the next request
159
+ // req.gitana_user.getDriver().http.refresh(function (err) {
160
+ //
161
+ // if (err) {
162
+ // cleanup(true);
163
+ // req.log("Invalidated auth state for gitana user: " + req.identity_properties.token);
164
+ // return releaseLockFn();
165
+ // }
166
+ //
167
+ // req.gitana_user.getDriver().reloadAuthInfo(function () {
168
+ // cleanup(true);
169
+ // req.log("Refreshed token for gitana user: " + req.identity_properties.token);
170
+ // releaseLockFn();
171
+ // });
172
+ // });
173
+ // });
174
+ // }
175
+ //
176
+ // }
177
+ // });
178
+ // }
179
+
180
+ //res.setHeader('x-powered-by', 'cloudcms');
181
+ res.writeHead(proxyRes.statusCode, proxyRes.headers)
182
+ proxyRes.pipe(res)
183
+ };
184
+
185
+ var proxyRequestHandler = function(req, res) {
186
+ proxy.web(req, res, webConfig, function(err, req, res) {
187
+ defaultWebHandler(err, req, res);
188
+ });
189
+ };
190
+
191
+ return proxyRequestHandler;
192
+ };
193
+
194
+ const http = require("http");
195
+ const server = http.createServer()
196
+ server.listen(3000);
197
+
198
+ var proxyRequestHandler = createProxyHandler("http", "api.default.svc.cluster.local", 80);
199
+ server.on('request', (req, res) => {
200
+ req.virtualHost = "mt85.us1.cloudcms.net";
201
+
202
+ proxyRequestHandler(req, res);
203
+ });
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "d1",
3
+ "description": "D1",
4
+ "version": "0.0.1",
5
+ "dependencies": {
6
+ "@socket.io/cluster-adapter": "^0.2.0",
7
+ "@socket.io/redis-adapter": "^7.2.0",
8
+ "@socket.io/sticky": "^1.0.1",
9
+ "accepts": "^1.3.8",
10
+ "agentkeepalive": "^4.2.1",
11
+ "alpaca": "^1.5.27",
12
+ "archiver": "^1.3.0",
13
+ "async": "^3.2.3",
14
+ "async-lock": "^1.3.2",
15
+ "aws-sdk": "^2.1208.0",
16
+ "basic-auth": "^2.0.1",
17
+ "body-parser": "^1.20.0",
18
+ "bytes": "^2.5.0",
19
+ "canoe": "^0.3.3",
20
+ "clone": "^2.1.2",
21
+ "connect-flash": "^0.1.1",
22
+ "connect-multiparty": "^2.2.0",
23
+ "connect-redis": "^6.1.3",
24
+ "consolidate": "^0.14.5",
25
+ "cookie-parser": "^1.4.4",
26
+ "debug": "^2.6.9",
27
+ "dustjs-helpers": "1.7.4",
28
+ "dustjs-linkedin": "3.0.1",
29
+ "errorhandler": "^1.5.1",
30
+ "express": "^4.18.1",
31
+ "express-session": "^1.17.3",
32
+ "express-useragent": "^1.0.15",
33
+ "extend-with-super": "^2.0.0",
34
+ "finalhandler": "^1.2.0",
35
+ "gitana": "^1.0.322",
36
+ "handlebars": "^4.4.2",
37
+ "hbs": "^4.0.5",
38
+ "helmet": "^4.6.0",
39
+ "http2-proxy": "^5.0.53",
40
+ "ioredis": "4.28.5",
41
+ "json5": "^1.0.1",
42
+ "jsonwebtoken": "^8.5.1",
43
+ "klaw": "^1.3.1",
44
+ "lru-cache": "^4.1.5",
45
+ "marked": "^4.0.14",
46
+ "memorystore": "^1.6.1",
47
+ "mime": "^1.6.0",
48
+ "mkdirp": "^0.5.1",
49
+ "moment": "^2.24.0",
50
+ "morgan": "^1.9.1",
51
+ "object-hash": "^1.3.1",
52
+ "object-merge": "^2.5.1",
53
+ "on-headers": "^1.0.2",
54
+ "passport": "^0.4.0",
55
+ "passport-cas": "^0.0.3",
56
+ "passport-facebook": "^2.1.1",
57
+ "passport-github": "^1.1.0",
58
+ "passport-google-oauth": "^1.0.0",
59
+ "passport-linkedin": "^1.0.0",
60
+ "passport-local": "^1.0.0",
61
+ "passport-oauth": "^1.0.0",
62
+ "passport-saml": "^2.2.0",
63
+ "passport-twitter": "^0.1.5",
64
+ "pkginfo": "^0.4.1",
65
+ "random-js": "^1.0.8",
66
+ "recursive-readdir": "^2.2.2",
67
+ "redis": "^4.2.0",
68
+ "redlock": "4.2.0",
69
+ "request": "^2.88.0",
70
+ "request-param": "^1.0.1",
71
+ "response-time": "^2.3.2",
72
+ "semver": "^7.3.7",
73
+ "serve-favicon": "^2.5.0",
74
+ "session-file-store": "^0.2.2",
75
+ "sha1": "^1.1.1",
76
+ "socket.io": "^4.5.1",
77
+ "ssl-root-cas": "^1.3.1",
78
+ "stomp-client": "^0.9.0",
79
+ "targz": "^1.0.1",
80
+ "temp": "^0.8.3",
81
+ "uuid": "^3.3.2",
82
+ "vm2": "^3.8.4",
83
+ "watch": "^0.13.0",
84
+ "winston": "^3.3.3"
85
+ }
86
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "author": {
3
+ "name": "Gitana Software, Inc.",
4
+ "email": "info@cloudcms.com",
5
+ "url": "https://www.cloudcms.com"
6
+ },
7
+ "name": "d1",
8
+ "description": "D1",
9
+ "version": "0.0.1",
10
+ "dependencies": {
11
+ "http2-proxy": "^5.0.53",
12
+ "finalhandler": "^1.2.0"
13
+ }
14
+ }
@@ -49,26 +49,57 @@ module.exports = function(type, config, options)
49
49
  fork = false;
50
50
  }
51
51
 
52
+ var bindToListeningPort = function(app, httpServer)
53
+ {
54
+ var httpServerPort = -1;
55
+ if (app) {
56
+ httpServerPort = app.get("port");
57
+ }
58
+ if (httpServerPort === -1) {
59
+ httpServerPort = process.env.PORT;
60
+ }
61
+ if (httpServerPort === -1) {
62
+ httpServerPort = 3000;
63
+ }
64
+
65
+ httpServer.listen(httpServerPort);
66
+ }
67
+
68
+ var bindSignalHandler = function()
69
+ {
70
+ var signal = false;
71
+ process.on('SIGINT', function() {
72
+ if (!signal) {
73
+ signal = true;
74
+ console.log("-------");
75
+ console.log("Heard SIGINT - shutting down in 10 seconds...");
76
+ console.log("-------");
77
+ setTimeout(function() { process.exit(0); }, 10000);
78
+ }
79
+ });
80
+ process.on('SIGTERM', function() {
81
+ if (!signal) {
82
+ signal = true;
83
+ console.log("-------");
84
+ console.log("Heard SIGTERM - shutting down in 10 seconds...");
85
+ console.log("-------");
86
+ setTimeout(function() { process.exit(0); }, 10000);
87
+ }
88
+ });
89
+ };
90
+
52
91
  if (!fork)
53
92
  {
93
+ bindSignalHandler();
94
+
54
95
  return launchWorker(launcher, config, options, function(err, app, httpServer) {
55
96
 
56
97
  if (err) {
57
98
  return completionFn(config, err);
58
99
  }
59
100
 
60
- var httpServerPort = -1;
61
- // if (app) {
62
- // httpServerPort = app.get("port");
63
- // }
64
- if (httpServerPort === -1) {
65
- httpServerPort = process.env.PORT;
66
- }
67
- if (httpServerPort === -1) {
68
- httpServerPort = 3000;
69
- }
70
-
71
- httpServer.listen(httpServerPort);
101
+ // bind to listening port
102
+ bindToListeningPort(app, httpServer);
72
103
 
73
104
  reportFn(config);
74
105
  completionFn(config);
@@ -76,21 +107,29 @@ module.exports = function(type, config, options)
76
107
  }
77
108
  else
78
109
  {
110
+ // in cluster mode, we have a single master listening to the port which distributes work to the workers
111
+
79
112
  if (cluster.isMaster)
80
113
  {
81
- return launchMaster(launcher, config, options, function(err, workers) {
114
+ bindSignalHandler();
115
+
116
+ return launchMaster(launcher, config, options, function(err, workers, httpServer) {
82
117
 
83
118
  if (err) {
84
119
  return completionFn(config, err);
85
120
  }
86
-
121
+
87
122
  //reportFn(config);
88
123
  completionFn(config);
89
124
  });
90
125
  }
91
126
  else
92
127
  {
93
- return launchWorker(launcher, config, options, function(err) {
128
+ return launchWorker(launcher, config, options, function(err, app, httpServer) {
129
+
130
+ // bind to listening port
131
+ bindToListeningPort(app, httpServer);
132
+
94
133
  completionFn(config, err);
95
134
  });
96
135
  }
@@ -112,22 +151,10 @@ var launchMaster = function(launcher, config, options, done)
112
151
  if (err) {
113
152
  return done(err);
114
153
  }
115
-
116
- var httpServerPort = -1;
117
- // if (app) {
118
- // httpServerPort = app.get("port");
119
- // }
120
- if (httpServerPort === -1) {
121
- httpServerPort = process.env.PORT;
122
- }
123
- if (httpServerPort === -1) {
124
- httpServerPort = 3000;
125
- }
126
-
127
- httpServer.listen(httpServerPort);
128
-
154
+
129
155
  launcher.afterStartCluster(httpServer, function(err, workers) {
130
- done(err, workers);
156
+ console.log("LaunchPad started Master: " + process.pid);
157
+ done(err, workers, httpServer);
131
158
  });
132
159
  });
133
160
  });
@@ -161,7 +188,7 @@ var launchWorker = function(launcher, config, options, done)
161
188
  if (err) {
162
189
  return done(err);
163
190
  }
164
-
191
+
165
192
  // if we are on a worker process, then inform the master that we completed
166
193
  if (process.send) {
167
194
  process.send("worker-startup");
@@ -173,6 +200,8 @@ var launchWorker = function(launcher, config, options, done)
173
200
  reportFn(config);
174
201
  }
175
202
 
203
+ console.log("LaunchPad started Worker: " + process.pid);
204
+
176
205
  done(null, app, httpServer);
177
206
  });
178
207
  });
@@ -65,7 +65,7 @@ module.exports = function(config) {
65
65
 
66
66
  cluster.on("exit", (worker) => {
67
67
  console.log(`Worker ${worker.process.pid} died`);
68
- cluster.fork();
68
+ //cluster.fork();
69
69
  });
70
70
 
71
71
  // wait for workers to start
package/locks/locks.js CHANGED
@@ -40,9 +40,9 @@ exports = module.exports = function()
40
40
  {
41
41
  process.env.CLOUDCMS_LOCKS_TYPE = "memory";
42
42
 
43
- if (process.configuration.setup !== "single") {
44
- process.env.CLOUDCMS_LOCKS_TYPE = "redis";
45
- }
43
+ // if (process.configuration.setup !== "single") {
44
+ // process.env.CLOUDCMS_LOCKS_TYPE = "redis";
45
+ // }
46
46
  }
47
47
 
48
48
  if (!process.configuration.locks) {
@@ -44,9 +44,9 @@ exports = module.exports = function()
44
44
  {
45
45
  process.env.CLOUDCMS_AWARENESS_TYPE = "memory";
46
46
 
47
- if (process.configuration.setup !== "single") {
48
- process.env.CLOUDCMS_AWARENESS_TYPE = "redis";
49
- }
47
+ // if (process.configuration.setup !== "single") {
48
+ // process.env.CLOUDCMS_AWARENESS_TYPE = "redis";
49
+ // }
50
50
  }
51
51
 
52
52
  if (!process.configuration.awareness) {
@@ -28,9 +28,9 @@ exports = module.exports = function()
28
28
 
29
29
  process.env.CLOUDCMS_CACHE_TYPE = "memory";
30
30
 
31
- if (process.configuration.setup !== "single") {
32
- process.env.CLOUDCMS_CACHE_TYPE = "redis";
33
- }
31
+ // if (process.configuration.setup !== "single") {
32
+ // process.env.CLOUDCMS_CACHE_TYPE = "redis";
33
+ // }
34
34
  }
35
35
 
36
36
  if (!process.configuration.cache.type)
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "name": "cloudcms-server",
8
8
  "description": "Cloud CMS Application Server Module",
9
- "version": "3.2.284",
9
+ "version": "3.2.285",
10
10
  "repository": {
11
11
  "type": "git",
12
12
  "url": "git://github.com/gitana/cloudcms-server.git"
package/server/index.js CHANGED
@@ -886,7 +886,7 @@ var startServer = function(config, startServerFinishedFn)
886
886
  });
887
887
  */
888
888
 
889
- // add req.id re
889
+ // increment and assign request id
890
890
  app.use(function (req, res, next) {
891
891
  requestCounter++;
892
892
  req.id = requestCounter;