refactor with async/await

Trying to be more robust about error handling and failure.
This commit is contained in:
Roman Shtylman
2016-07-09 17:06:13 -07:00
parent f12f1c81b3
commit a2a58f4c6f
10 changed files with 290 additions and 221 deletions

267
proxy.js
View File

@@ -1,28 +1,51 @@
var net = require('net');
var EventEmitter = require('events').EventEmitter;
import net from 'net';
import EventEmitter from 'events';
import log from 'bookrc';
import Debug from 'debug';
var log = require('bookrc');
var debug = require('debug')('localtunnel-server');
const debug = Debug('localtunnel:server');
var Proxy = function(opt, cb) {
const Proxy = function(opt) {
if (!(this instanceof Proxy)) {
return new Proxy(opt, cb);
return new Proxy(opt);
}
var self = this;
const self = this;
self.sockets = [];
self.waiting = [];
var id = opt.id;
self.id = opt.id;
// default max is 10
var max_tcp_sockets = opt.max_tcp_sockets || 10;
self.max_tcp_sockets = opt.max_tcp_sockets || 10;
// new tcp server to service requests for this client
var client_server = net.createServer();
self.server = net.createServer();
client_server.on('error', function(err) {
// track initial user connection setup
self.conn_timeout = undefined;
self.debug = Debug(`localtunnel:server:${self.id}`);
};
Proxy.prototype.__proto__ = EventEmitter.prototype;
Proxy.prototype.start = function(cb) {
const self = this;
const server = self.server;
if (self.started) {
cb(new Error('already started'));
return;
}
self.started = true;
server.on('close', self._cleanup.bind(self));
server.on('connection', self._handle_socket.bind(self));
server.on('error', function(err) {
// where do these errors come from?
// other side creates a connection and then is killed?
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
@@ -30,129 +53,121 @@ var Proxy = function(opt, cb) {
log.error(err);
});
// track initial user connection setup
var conn_timeout;
// user has 5 seconds to connect before their slot is given up
function maybe_tcp_close() {
clearTimeout(conn_timeout);
conn_timeout = setTimeout(function() {
// sometimes the server is already closed but the event has not fired?
try {
clearTimeout(conn_timeout);
client_server.close();
} catch (err) {
cleanup();
}
}, 5000);
}
maybe_tcp_close();
function cleanup() {
debug('closed tcp socket for client(%s)', id);
clearTimeout(conn_timeout);
// clear waiting by ending responses, (requests?)
self.waiting.forEach(function(waiting) {
waiting(null);
});
self.emit('end');
}
// no longer accepting connections for this id
client_server.on('close', cleanup);
// new tcp connection from lt client
client_server.on('connection', function(socket) {
// no more socket connections allowed
if (self.sockets.length >= max_tcp_sockets) {
return socket.end();
}
debug('new connection on port: %s', id);
// a single connection is enough to keep client id slot open
clearTimeout(conn_timeout);
socket.once('close', function(had_error) {
debug('client %s closed socket (error: %s)', id, had_error);
// what if socket was servicing a request at this time?
// then it will be put back in available after right?
// remove this socket
var idx = self.sockets.indexOf(socket);
if (idx >= 0) {
self.sockets.splice(idx, 1);
}
// need to track total sockets, not just active available
debug('remaining client sockets: %s', self.sockets.length);
// no more sockets for this ident
if (self.sockets.length === 0) {
debug('all client(%s) sockets disconnected', id);
maybe_tcp_close();
}
});
// close will be emitted after this
socket.on('error', function(err) {
// we don't log here to avoid logging crap for misbehaving clients
socket.destroy();
});
self.sockets.push(socket);
var wait_cb = self.waiting.shift();
if (wait_cb) {
debug('handling queued request');
self.next_socket(wait_cb);
}
});
client_server.listen(function() {
var port = client_server.address().port;
debug('tcp server listening on port: %d', port);
server.listen(function() {
const port = server.address().port;
self.debug('tcp server listening on port: %d', port);
cb(null, {
// port for lt client tcp connections
port: port,
// maximum number of tcp connections allowed by lt client
max_conn_count: max_tcp_sockets
max_conn_count: self.max_tcp_sockets
});
});
self._maybe_destroy();
};
Proxy.prototype.__proto__ = EventEmitter.prototype;
Proxy.prototype._maybe_destroy = function() {
const self = this;
Proxy.prototype.next_socket = function(cb) {
var self = this;
clearTimeout(self.conn_timeout);
self.conn_timeout = setTimeout(function() {
// sometimes the server is already closed but the event has not fired?
try {
clearTimeout(self.conn_timeout);
self.server.close();
}
catch (err) {
self._cleanup();
}
}, 5000);
}
// socket is a tcp connection back to the user hosting the site
var sock = self.sockets.shift();
// new socket connection from client for tunneling requests to client
Proxy.prototype._handle_socket = function(socket) {
const self = this;
// TODO how to handle queue?
// queue request
if (!sock) {
debug('no more client, queue callback');
return self.waiting.push(cb);
// no more socket connections allowed
if (self.sockets.length >= self.max_tcp_sockets) {
return socket.end();
}
var done_called = false;
// put the socket back
function done() {
if (done_called) {
throw new Error('done called multiple times');
self.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// a single connection is enough to keep client id slot open
clearTimeout(self.conn_timeout);
socket.once('close', function(had_error) {
self.debug('closed socket (error: %s)', had_error);
// what if socket was servicing a request at this time?
// then it will be put back in available after right?
// we need a list of sockets servicing requests?
// remove this socket
const idx = self.sockets.indexOf(socket);
if (idx >= 0) {
self.sockets.splice(idx, 1);
}
done_called = true;
// need to track total sockets, not just active available
self.debug('remaining client sockets: %s', self.sockets.length);
// no more sockets for this ident
if (self.sockets.length === 0) {
self.debug('all sockets disconnected');
self._maybe_destroy();
}
});
// close will be emitted after this
socket.on('error', function(err) {
// we don't log here to avoid logging crap for misbehaving clients
socket.destroy();
});
self.sockets.push(socket);
const wait_cb = self.waiting.shift();
if (wait_cb) {
self.debug('handling queued request');
self.next_socket(wait_cb);
}
};
Proxy.prototype._cleanup = function() {
const self = this;
self.debug('closed tcp socket for client(%s)', self.id);
clearTimeout(self.conn_timeout);
// clear waiting by ending responses, (requests?)
self.waiting.forEach(handler => handler(null));
self.emit('end');
};
Proxy.prototype.next_socket = function(handler) {
const self = this;
// socket is a tcp connection back to the user hosting the site
const sock = self.sockets.shift();
if (!sock) {
self.debug('no more client, queue callback');
self.waiting.push(handler);
return;
}
self.debug('processing request');
handler(sock)
.catch((err) => {
log.error(err);
})
.finally(() => {
if (!sock.destroyed) {
debug('retuning socket');
self.debug('retuning socket');
self.sockets.push(sock);
}
@@ -161,19 +176,17 @@ Proxy.prototype.next_socket = function(cb) {
return;
}
var wait = self.waiting.shift();
debug('processing queued cb');
const wait = self.waiting.shift();
self.debug('processing queued cb');
if (wait) {
return self.next_socket(cb);
self.next_socket(cb);
return;
}
};
debug('processing request');
cb(sock, done);
});
};
Proxy.prototype._done = function() {
var self = this;
const self = this;
};
module.exports = Proxy;
export default Proxy;