Permalink
Browse files

refactor server tcp handling

- limit on number of tcp connections
- preliminary support for websockets
  • Loading branch information...
1 parent ab28444 commit 51d91ce0e8ba63ac9943c1ae91462a806e89635b @defunctzombie defunctzombie committed Oct 18, 2012
Showing with 247 additions and 164 deletions.
  1. +59 −35 client.js
  2. +12 −0 lib/rand_id.js
  3. +176 −129 server.js
View
@@ -10,6 +10,9 @@ var argv = require('optimist')
default: 'http://localtunnel.me',
describe: 'upstream server providing forwarding'
})
+ .options('subdomain', {
+ describe: 'request this subdomain'
+ })
.describe('port', 'internal http server port')
.argv;
@@ -29,67 +32,88 @@ var opt = {
var base_uri = 'http://' + opt.host + ':' + opt.port + opt.path;
-var internal;
-var upstream;
-var prev_id;
+var prev_id = argv.subdomain || '';
(function connect_proxy() {
opt.uri = base_uri + ((prev_id) ? prev_id : '?new');
request(opt, function(err, res, body) {
if (err) {
- console.error('upstream not available: %s', err.message);
- return process.exit(-1);
+ console.error('tunnel server not available: %s, retry 1s', err.message);
+
+ // retry interval for id request
+ return setTimeout(function() {
+ connect_proxy();
+ }, 1000);
}
// our assigned hostname and tcp port
var port = body.port;
var host = opt.host;
+ var max_conn = body.max_conn_count || 1;
// store the id so we can try to get the same one
prev_id = body.id;
console.log('your url is: %s', body.url);
- // connect to remote tcp server
- upstream = net.createConnection(port, host);
+ var count = 0;
+
+ // open 5 connections to the localtunnel server
+ // allows for resources to be served faster
+ for (var count = 0 ; count < max_conn ; ++count) {
+ var upstream = duplex(port, host, local_port, 'localhost');
+ upstream.once('end', function() {
+ // all upstream connections have been closed
+ if (--count <= 0) {
+ connect_proxy();
+ }
+ });
+ }
+ });
+})();
- // reconnect internal
- connect_internal();
+function duplex(port, host, local_port, local_host) {
- upstream.on('end', function() {
- console.log('> upstream connection terminated');
+ // connect to remote tcp server
+ var upstream = net.createConnection(port, host);
+ var internal = net.createConnection(local_port, local_host);
- // sever connection to internal server
- // on reconnect we will re-establish
- internal.end();
+ // when upstream connection is closed, close other associated connections
+ upstream.on('end', function() {
+ console.log('> upstream connection terminated');
- setTimeout(function() {
- connect_proxy();
- }, 1000);
- });
+ // sever connection to internal server
+ // on reconnect we will re-establish
+ internal.end();
});
-})();
-function connect_internal() {
+ upstream.on('error', function(err) {
+ console.error(err);
+ });
- internal = net.createConnection(local_port);
- internal.on('error', function(err) {
- console.log('error connecting to local server. retrying in 1s');
+ (function connect_internal() {
- setTimeout(function() {
- connect_internal();
- }, 1000);
- });
+ //internal = net.createConnection(local_port);
+ internal.on('error', function(err) {
+ console.log('error connecting to local server. retrying in 1s');
+ setTimeout(function() {
+ connect_internal();
+ }, 1000);
+ });
- internal.on('end', function() {
- console.log('disconnected from local server. retrying in 1s');
- setTimeout(function() {
- connect_internal();
- }, 1000);
- });
+ internal.on('end', function() {
+ console.log('disconnected from local server. retrying in 1s');
+ setTimeout(function() {
+ connect_internal();
+ }, 1000);
+ });
- upstream.pipe(internal);
- internal.pipe(upstream);
+ upstream.pipe(internal);
+ internal.pipe(upstream);
+ })();
+
+ return upstream;
}
+
View
@@ -0,0 +1,12 @@
+
+var chars = 'abcdefghiklmnopqrstuvwxyz';
+module.exports = function rand_id() {
+ var randomstring = '';
+ for (var i=0; i<4; ++i) {
+ var rnum = Math.floor(Math.random() * chars.length);
+ randomstring += chars[rnum];
+ }
+
+ return randomstring;
+}
+
Oops, something went wrong.

0 comments on commit 51d91ce

Please sign in to comment.