-
Notifications
You must be signed in to change notification settings - Fork 848
/
server.js
123 lines (107 loc) · 4.04 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
var WebSocketServer = require('websocket').server;
var WebSocketRouter = require('websocket').router;
var http = require('http');
var static = require('node-static');
var parseUrl = require('url').parse;
var fs = require('fs');
var path = require('path');
var less = require('less');
var coffeeCompile = require("coffee-script").compile;
var logger = require('../../lib/logger');
var server = http.createServer(function(request, response) {
var url = parseUrl(request.url);
var protocol = request.headers["porwarded-proto"] || "http:";
var host = request.headers["host"];
var base = protocol + "//" + host;
if (url.path == '/status'){
response.end("OK");
}
else{
write404(response);
}
});
function write500(error, response) {
response.writeHead(500, {"Content-Type": "text/plain"});
if (typeof error != "string") {
error = "\n" + JSON.stringify(error, null, " ");
}
response.end("Error: " + error);
}
function write404(response) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end("Resource not found");
}
function startServer(port, host) {
server.listen(port, host, function() {
logger.info('HUB Server listening on port ' + port + " (Should be " + process.env.HUB_BASE + ")");
});
}
var wsServer = new WebSocketServer({
httpServer: server,
// 10Mb max size (1Mb is default, maybe this bump is unnecessary)
maxReceivedMessageSize: 0x1000000,
// The browser doesn't seem to break things up into frames (not sure what this means)
// and the default of 64Kb was exceeded; raised to 1Mb
maxReceivedFrameSize: 0x100000,
// Using autoaccept because the origin is somewhat dynamic
// FIXME: make this smarter?
autoAcceptConnections: false
});
function originIsAllowed(origin) {
// Unfortunately the origin will be whatever page you are sharing,
// which could be any origin
return true;
}
var allConnections = {};
var ID = 0;
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
logger.info((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}
var id = request.httpRequest.url.replace(/^\/hub\/+/, '').replace(/\/.*/, '');
// FIXME: we should use a protocol here instead of null, but I can't
// get it to work. "Protocol" is what the two clients are using
// this channel for, "towtruck" in this case.
var connection = request.accept(null, request.origin);
connection.ID = ID++;
if (! allConnections[id]) {
allConnections[id] = [];
}
allConnections[id].push(connection);
logger.info((new Date()) + ' Connection accepted to ' + JSON.stringify(id) + ' ID:' + connection.ID);
connection.on('message', function(message) {
var parsed = JSON.parse(message.utf8Data);
logger.debug('Message on ' + id + ' bytes: '
+ (message.utf8Data && message.utf8Data.length)
+ ' conn ID: ' + connection.ID + ' data:' + message.utf8Data.substr(0, 20)
+ ' connections: ' + allConnections[id].length);
for (var i=0; i<allConnections[id].length; i++) {
var c = allConnections[id][i];
if (c == connection) {
continue;
}
if (message.type === 'utf8') {
c.sendUTF(message.utf8Data);
} else if (message.type === 'binary') {
c.sendBytes(message.binaryData);
}
}
});
connection.on('close', function(reasonCode, description) {
var index = allConnections[id].indexOf(connection);
if (index != -1) {
allConnections[id].splice(index, 1);
}
logger.info((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected, ID: ' + connection.ID);
});
});
if (require.main == module) {
startServer(process.env.HUB_SERVER_PORT || 8080, process.env.HOST || '127.0.0.1');
}
exports.startServer = startServer;