mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 21:45:54 +00:00
refactor server tcp handling
- limit on number of tcp connections - preliminary support for websockets
This commit is contained in:
94
client.js
94
client.js
@@ -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;
|
||||
|
||||
// reconnect internal
|
||||
connect_internal();
|
||||
|
||||
upstream.on('end', function() {
|
||||
console.log('> upstream connection terminated');
|
||||
|
||||
// sever connection to internal server
|
||||
// on reconnect we will re-establish
|
||||
internal.end();
|
||||
|
||||
setTimeout(function() {
|
||||
connect_proxy();
|
||||
}, 1000);
|
||||
});
|
||||
// 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
function connect_internal() {
|
||||
function duplex(port, host, local_port, local_host) {
|
||||
|
||||
internal = net.createConnection(local_port);
|
||||
internal.on('error', function(err) {
|
||||
console.log('error connecting to local server. retrying in 1s');
|
||||
// connect to remote tcp server
|
||||
var upstream = net.createConnection(port, host);
|
||||
var internal = net.createConnection(local_port, local_host);
|
||||
|
||||
setTimeout(function() {
|
||||
connect_internal();
|
||||
}, 1000);
|
||||
// when upstream connection is closed, close other associated connections
|
||||
upstream.on('end', function() {
|
||||
console.log('> upstream connection terminated');
|
||||
|
||||
// sever connection to internal server
|
||||
// on reconnect we will re-establish
|
||||
internal.end();
|
||||
});
|
||||
|
||||
internal.on('end', function() {
|
||||
console.log('disconnected from local server. retrying in 1s');
|
||||
setTimeout(function() {
|
||||
connect_internal();
|
||||
}, 1000);
|
||||
upstream.on('error', function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
|
||||
upstream.pipe(internal);
|
||||
internal.pipe(upstream);
|
||||
(function connect_internal() {
|
||||
|
||||
//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);
|
||||
});
|
||||
|
||||
upstream.pipe(internal);
|
||||
internal.pipe(upstream);
|
||||
})();
|
||||
|
||||
return upstream;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user