-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
45 lines (37 loc) · 1.1 KB
/
server.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
var http = require('http');
var path = require('path');
var app = require('./lib/app');
var proxy = require('./lib/proxy');
var portList = [80, 2000, 3000, 4000, 5000, 8000, 8080, 12491, 12492];
portList.forEach(attachToPort);
function attachToPort (port) {
var server = http.createServer(app.callback());
server.listen(port);
server.on('error', handleServerError);
server.on('upgrade', handleUpgrade);
function handleServerError (err) {
console.log('server error', 'port:', port);
console.error(err);
}
}
function handleUpgrade (req, socket, head) {
var host = req.headers.host;
// if subdomain
if (host.split('.').length > 2) {
var name = host.split('.').shift();
var socketPath = path.join(__dirname, '../binds', name, 'proxy');
proxy.ws(req, socket, head, {
target: {
socketPath: socketPath
}
}, handleRequestErrors);
req.on('error', handleRequestErrors);
function handleRequestErrors (err) {
console.log('websocket request error', {
name: name,
socketPath: socketPath
});
console.error(err);
}
}
}