mirror of
https://github.com/moparisthebest/mail
synced 2024-12-21 14:58:49 -05:00
[WO-765] do not multiplex sockets through single websocket
This commit is contained in:
parent
a07ee38fdb
commit
2be7beb3a1
63
server.js
63
server.js
@ -109,60 +109,50 @@ app.use(express.static(__dirname + '/dist'));
|
|||||||
// Socket.io proxy
|
// Socket.io proxy
|
||||||
//
|
//
|
||||||
|
|
||||||
// TODO:test origin constraint
|
|
||||||
//io.origins(config.server.inboundOrigins.join(' '));
|
|
||||||
|
|
||||||
io.on('connection', function(socket) {
|
io.on('connection', function(socket) {
|
||||||
|
log.info('io', 'New connection [%s] from %s', socket.conn.id, socket.conn.remoteAddress);
|
||||||
log.info('io', 'New connection [%s]', socket.conn.id);
|
|
||||||
|
|
||||||
var idCounter = 0;
|
|
||||||
|
|
||||||
socket.on('open', function(data, fn) {
|
socket.on('open', function(data, fn) {
|
||||||
var socketId = ++idCounter;
|
|
||||||
var tcp;
|
|
||||||
|
|
||||||
if (!development && config.server.outboundPorts.indexOf(data.port) < 0) {
|
if (!development && config.server.outboundPorts.indexOf(data.port) < 0) {
|
||||||
log.warn('io', 'Open request to %s:%s was rejected, closing [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
log.info('io', 'Open request to %s:%s was rejected, closing [%s]', data.host, data.port, socket.conn.id);
|
||||||
socket.disconnect();
|
socket.disconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log.verbose('io', 'Open request to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
log.verbose('io', 'Open request to %s:%s [%s]', data.host, data.port, socket.conn.id);
|
||||||
|
var tcp = net.connect(data.port, data.host, function() {
|
||||||
tcp = net.connect(data.port, data.host, function() {
|
log.verbose('io', 'Opened tcp connection to %s:%s [%s]', data.host, data.port, socket.conn.id);
|
||||||
log.verbose('io', 'Opened tcp connection to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
|
||||||
|
|
||||||
tcp.on('data', function(chunk) {
|
tcp.on('data', function(chunk) {
|
||||||
log.silly('io', 'Received %s bytes from %s:%s [%s:%s]', chunk.length, data.host, data.port, socket.conn.id, socketId);
|
log.silly('io', 'Received %s bytes from %s:%s [%s]', chunk.length, data.host, data.port, socket.conn.id);
|
||||||
socket.emit('data-' + socketId, chunk);
|
socket.emit('data', chunk);
|
||||||
});
|
});
|
||||||
|
|
||||||
tcp.on('error', function(err) {
|
tcp.on('error', function(err) {
|
||||||
log.verbose('io', 'Error for %s:%s [%s:%s]: %s', data.host, data.port, socket.conn.id, socketId, err.message);
|
log.verbose('io', 'Error for %s:%s [%s]: %s', data.host, data.port, socket.conn.id, err.message);
|
||||||
socket.emit('error-' + socketId, err.message);
|
socket.emit('error', err.message);
|
||||||
});
|
});
|
||||||
|
|
||||||
tcp.on('end', function() {
|
tcp.on('end', function() {
|
||||||
socket.emit('end-' + socketId);
|
socket.emit('end');
|
||||||
});
|
});
|
||||||
|
|
||||||
tcp.on('close', function() {
|
tcp.on('close', function() {
|
||||||
log.verbose('io', 'Closed tcp connection to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
log.verbose('io', 'Closed tcp connection to %s:%s [%s]', data.host, data.port, socket.conn.id);
|
||||||
socket.emit('close-' + socketId);
|
socket.emit('close');
|
||||||
|
|
||||||
socket.removeAllListeners('data-' + socketId);
|
socket.removeAllListeners('data');
|
||||||
socket.removeAllListeners('end-' + socketId);
|
socket.removeAllListeners('end');
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('data-' + socketId, function(chunk, fn) {
|
socket.on('data', function(chunk, fn) {
|
||||||
if (!chunk || !chunk.length) {
|
if (!chunk || !chunk.length) {
|
||||||
if (typeof fn === 'function') {
|
if (typeof fn === 'function') {
|
||||||
fn();
|
fn();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log.silly('io', 'Sending %s bytes to %s:%s [%s:%s]', chunk.length, data.host, data.port, socket.conn.id, socketId);
|
log.silly('io', 'Sending %s bytes to %s:%s [%s]', chunk.length, data.host, data.port, socket.conn.id);
|
||||||
tcp.write(chunk, function() {
|
tcp.write(chunk, function() {
|
||||||
if (typeof fn === 'function') {
|
if (typeof fn === 'function') {
|
||||||
fn();
|
fn();
|
||||||
@ -170,25 +160,22 @@ io.on('connection', function(socket) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('end-' + socketId, function() {
|
socket.on('end', function() {
|
||||||
log.verbose('io', 'Received request to close connection to %s:%s [%s:%s]', data.host, data.port, socket.conn.id, socketId);
|
log.verbose('io', 'Received request to close connection to %s:%s [%s]', data.host, data.port, socket.conn.id);
|
||||||
tcp.end();
|
tcp.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (typeof fn === 'function') {
|
if (typeof fn === 'function') {
|
||||||
fn(socketId);
|
fn(os.hostname());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
socket.on('disconnect', function() {
|
||||||
|
log.verbose('io', 'Closed connection [%s], closing connection to %s:%s ', socket.conn.id, data.host, data.port);
|
||||||
|
tcp.end();
|
||||||
|
socket.removeAllListeners();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('disconnect', function() {
|
|
||||||
log.info('io', 'Closed connection [%s]', socket.conn.id);
|
|
||||||
socket.removeAllListeners();
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('hostname', function(fn) {
|
|
||||||
fn(os.hostname());
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user