-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
164 lines (119 loc) · 3.34 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
"use strict";
var fs = require('fs');
var http = require('http');
var https = require('https');
var express = require('express');
var constants = require('constants');
var app = express();
var httpapp = express();
var nconf = require('nconf');
var bodyParser = require('body-parser');
var APIrouter = require('./lib/apirouter').APIrouter;
// First consider commandline arguments and environment variables, respectively.
nconf.argv().env('__');
// Then load configuration from a designated file.
nconf.file({ file: 'etc/config.js' });
// Provide default values for settings not provided above.
nconf.defaults({
'http': {
'port': 80,
'securePort': 443,
'host': 'httpjs.net'
}
});
var privateKey = fs.readFileSync('etc/certs/httpjs.pem', 'utf8');
var certificate = fs.readFileSync('etc/certs/httpjs.crt', 'utf8');
var credentials = {key: privateKey, cert: certificate};
credentials.secureProtocol = 'SSLv23_method';
credentials.secureOptions = constants.SSL_OP_NO_SSLv3;
var server = http.createServer(httpapp);
var httpsServer = https.createServer(credentials, app);
// var httpsServer = https.createServer(app);
var io = require('socket.io')(httpsServer);
var port = nconf.get('http:port');
var securePort = nconf.get('http:securePort');
var host = nconf.get('http:host');
var baseURL = '.' + host;
console.log("Setting up server to listen at port " + port + " and " + securePort + " on host " + host);
server.listen(port);
httpsServer.listen(securePort);
httpapp.use(function(req,res,next) {
if (!/https/.test(req.protocol)){
res.redirect(301, "https://" + req.headers.host + req.url);
} else {
return next();
}
});
var ar = new APIrouter();
function getX(host) {
var i = host.indexOf(baseURL);
// if (i === -1) return null;
if (i < 1) {
return null;
}
var sub = host.substring(0, i);
return sub;
}
app.use(function(req, res, next) {
if (req.host !== host) {
return next();
}
return express.static(__dirname + '/public')(req, res, next);
});
app.use(bodyParser.urlencoded({ "extended": false }));
app.use(bodyParser.json());
app.use(function(req, res, next) {
req.rawBody = '';
req.on('data', function(chunk) {
req.rawBody += chunk;
});
// setTimeout(function() {
next();
// }, 10);
});
app.use(function(req, res, next) {
var x = getX(req.host);
if (!x) {
return next();
}
ar.route(x, req, res);
});
// io.on('disconnect', function(socket) {
// console.log("Disconnected");
// if (socket.hasOwnProperty('x')) {
// console.log("Disconnected; " + x);
// }
// });
io.on('connection', function (socket) {
var x = null;
setInterval(function() {
socket.emit('ping', { hello: x });
}, 5000);
socket.on('ping', function() {
if (x !== null) {
ar.ping(x);
}
});
socket.on('register', function (data) {
if (data.hasOwnProperty('hostKey')) {
x = ar.get(socket, data.hostKey);
} else {
x = ar.get(socket);
}
socket.x = x;
console.log("Frontend initiated a requeset for registration", data);
socket.emit('registered', {
"url": 'https://' + x + baseURL + '/',
"hostKey": x
});
console.log("Successfully registered as " + x);
});
socket.on('response', function (msg) {
console.log("frontend responded back"); console.log(msg);
ar.response(msg);
});
socket.on('disconnect', function () {
console.log("Disconnect (" + x + ")");
ar.disconnect(x);
});
});