forked from 3drepo/3drepo.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3drepo.js
160 lines (132 loc) · 5.34 KB
/
3drepo.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
/**
* Copyright (C) 2014 3D Repo Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var express = require('express'),
app = express(),
vhost = require('vhost'),
path = require('path'),
cors = require('cors'),
fs = require('fs'),
constants = require('constants');
var log_iface = require('./js/core/logger.js');
var logger = log_iface.logger;
onError = log_iface.onError;
var config = require('./js/core/config.js');
var apiApp = require('./api.js').app;
var https = require("https");
var http = require('http');
if ('ssl' in config) {
var ssl_options = {
key: fs.readFileSync(config.ssl.key, 'utf8'),
cert: fs.readFileSync(config.ssl.cert, 'utf8'),
ciphers: 'ECDHE-ECDSA-AES128-GCM-SHA256|ECDHE-ECDSA-AES256-SHA:!RC4:!aNULL',
//ciphers: 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:!aNULL:!eNULL:!EXPORT:!DSS:!DES:!RC4:!3DES:!MD5:!PS:!SSLv3',
honorCipherOrder: true,
ecdhCurve: 'secp384r1',
secureOptions: constants.SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION|constants.SSL_OP_NO_SSLv2|constants.SSL_OP_NO_SSLv3
};
// This is the optional certificate authority
if (config.ssl.ca) {
ssl_options['ca'] = fs.readFileSync(config.ssl.ca, 'utf8');
}
var http_app = express();
http_app.get('*', function(req, res) {
res.redirect('https://' + req.headers.host + req.url);
});
http.createServer(http_app).listen(config.servers[0].http_port, config.servers[0].hostname, function() {
logger.log('info', 'Starting routing HTTP for ' + config.servers[0].hostname + ' service on port ' + config.servers[0].http_port);
});
}
if (config.vhost)
{
// We have to start virtual host if the API server and web service have
// different sub-domains.
if (!config.api_server.external)
{
if ('ssl' in config)
var api_server = https.createServer(ssl_options, apiApp);
else
var api_server = http.createServer(apiApp);
}
for(i in config.servers)
{
var frontApp = require('./frontend.js').createApp(config.servers[i].template);
if ('ssl' in config)
var frontServer = https.createServer(ssl_options, frontApp);
else
var frontServer = http.createServer(frontApp);
var serverPort = config.servers[i].port;
var serverHost = config.servers[i].hostname;
console.log(serverPort);
frontServer.listen(serverPort, serverHost, function() {
logger.log('info', 'Starting web service on ' + serverHost + ' port ' + serverPort);
});
}
if (!config.api_server.external)
{
api_server.listen(config.api_server.port, config.api_server.hostname, function() {
logger.log('info', 'Starting API service on ' + config.api_server.hostname + ' port ' + config.api_server.port);
});
}
} else if (config.subdirectory) {
// Here the API server and web service run on same host
// but different directory.
var app = express();
app.use("/" + config.api_server.host_dir, apiApp);
for(i in config.servers)
{
var frontApp = require('./frontend.js').createApp(config.servers[i].template);
app.use("/", frontApp);
}
if ('ssl' in config)
var server = https.createServer(ssl_options, app);
else
var server = http.createServer(app);
// Use '0.0.0.0' so that the website is accessible from all
// relevant addresses
server.listen(config.servers[0].port, '0.0.0.0', function() {
logger.log('info', 'Starting service on ' + config.servers[0].hostname + ' port ' + config.servers[0].port);
});
} else {
// This is an advanced configuration, which allows for different ports
// and/or different hosts currently this won't be automatically detected
// so you would have to set manually in config crossOrigin and vhost to false
var vhostApp = express();
if (!config.api_server.external)
{
logger.log('info', 'Starting VHOST for ' + config.api_server.hostname);
vhostApp.use(vhost(config.api_server.hostname, apiApp));
}
for(i in config.servers)
{
var frontApp = require('./frontend.js').createApp(config.servers[i].template);
logger.log('info', 'Starting VHOST for ' + config.servers[i].hostname);
vhostApp.use(vhost(config.servers[i].hostname, frontApp));
if('ssl' in config)
config.servers[i].port = config.servers[i].https_port;
else
config.servers[i].port = config.servers[i].http_port;
}
if ('ssl' in config)
{
var server = https.createServer(ssl_options, vhostApp);
} else {
var server = http.createServer(vhostApp);
}
server.listen(config.servers[0].port, function() {
logger.log('info', 'Started VHOST server on ' + config.servers[0].port);
});
}