-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·138 lines (113 loc) · 3.91 KB
/
server.js
File metadata and controls
executable file
·138 lines (113 loc) · 3.91 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
// Serves up index.html, dist/*, socket.io and plugins!
var options = { // defaults
http: false,
ip: "0.0.0.0",
port: 9001,
plugin: [],
};
var getopts = require("node-getopt").create([
['', 'http', 'Disable SSL'],
['', 'ip=', 'Set IP'],
['', 'port=', 'Set port'],
['', 'watch', 'Recompile assets on file modification'],
['', 'plugin=ARG+', 'Add "connect-style" plugins from ./lib'],
['h', 'help', '']
]).bindHelp();
var opt = getopts.parseSystem();
if (opt.argv.length > 0) {
console.error("ERROR: Unexpected argument(s): " + opt.argv.join(', '));
process.stdout.write(getopts.getHelp());
process.exit(1);
}
// Merge opts into options
for (var attrname in opt.options) { options[attrname] = opt.options[attrname]; }
var fs = require('fs'),
// url = require('url'),
path = require('path');
var server_opts = {};
var connect = require('connect')();
var server = require(options.http ? 'http' : 'https');
var watcher;
if (!options.http) {
try {
server_opts = {
key: fs.readFileSync(path.resolve('keys/privatekey.pem')),
cert: fs.readFileSync(path.resolve('keys/certificate.pem'))
};
}
catch (err) {
console.warn("WARNING: failed to find valid SSL keys, falling back to fake-keys..");
server_opts = {
key: fs.readFileSync(path.join(__dirname, 'node_modules/rtcmulticonnection-v3/fake-keys/privatekey.pem')),
cert: fs.readFileSync(path.join(__dirname, 'node_modules/rtcmulticonnection-v3/fake-keys/certificate.pem'))
};
}
// HTTP Strict Transport Security. (keep using SSL for at least a year)
// if (!options.http)
// response.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
// Setup HTTP-redirect server.
require('http').createServer(function(req, res) {
res.writeHead(301, { "Location": "https://" + req.headers.host + req.url });
res.end();
}).on('error', function(err) {
console.warn("WARNING: unable to start http-redirect server. GOT:", err.toString());
}).listen(80);
}
var app = server.createServer(server_opts, connect).
listen(options.port, options.ip, function() {
var addr = app.address();
console.log("Server listening at", (options.http ? "http://" : "https://" ) + addr.address + ":" + addr.port);
});
app.on('error', function(err) {
console.error('ServerError:', err.code);
process.exit(1);
});
// Add signaling server - Copied from github.com/muaz-khan/RTCMultiConnection/server.js
require('rtcmulticonnection-v3/Signaling-Server.js')(app, function(socket) {
try {
var params = socket.handshake.query;
// "socket" object is totally in your own hands!
// do whatever you want!
// in your HTML page, you can access socket as following:
// connection.socketCustomEvent = 'custom-message';
// var socket = connection.getSocket();
// socket.emit(connection.socketCustomEvent, { test: true });
if (!params.socketCustomEvent) {
params.socketCustomEvent = 'custom-message';
}
socket.on(params.socketCustomEvent, function(message) {
try {
socket.broadcast.emit(params.socketCustomEvent, message);
} catch (e) {}
});
} catch (e) {}
});
// HTTP Strict Transport Security. (keep using SSL for at least a year)
if (!options.http) {
connect.use(function(req, res, next) {
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
next();
});
}
// Load file_server
options.plugin.push('file_server');
// Load plugins
for (var i in options.plugin) {
try { // load module from local lib.
connect.use(require(path.join(__dirname, 'lib', options.plugin[i])));
} catch(err) {
if (err.code !== 'MODULE_NOT_FOUND') throw err;
// load the module from './lib/'
connect.use(require(path.join(path.resolve('lib'), options.plugin[i])));
}
}
// --Watch
if (options.watch) {
watcher = require('child_process').spawn('webpack', ['--watch', '--colors']);
watcher.stdout.on('data', function(data) {
console.log(data.toString());
});
watcher.stderr.on('data', function(data) {
console.log(data.toString());
});
}