mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 21:45:54 +00:00
expose client as a library
- Allows for using localtunnel from code instead of manually invoking - add tests - add travis config - add travis badge
This commit is contained in:
146
client.js
146
client.js
@@ -2,118 +2,132 @@
|
||||
var net = require('net');
|
||||
var url = require('url');
|
||||
var request = require('request');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var argv = require('optimist')
|
||||
.usage('Usage: $0 --port [num]')
|
||||
.demand(['port'])
|
||||
.options('host', {
|
||||
default: 'http://localtunnel.me',
|
||||
describe: 'upstream server providing forwarding'
|
||||
})
|
||||
.options('subdomain', {
|
||||
describe: 'request this subdomain'
|
||||
})
|
||||
.describe('port', 'internal http server port')
|
||||
.argv;
|
||||
|
||||
// local port
|
||||
var local_port = argv.port;
|
||||
|
||||
// optionally override the upstream server
|
||||
var upstream = url.parse(argv.host);
|
||||
|
||||
// query options
|
||||
var opt = {
|
||||
host: upstream.hostname,
|
||||
port: upstream.port || 80,
|
||||
path: '/',
|
||||
json: true
|
||||
};
|
||||
|
||||
var base_uri = 'http://' + opt.host + ':' + opt.port + opt.path;
|
||||
|
||||
var prev_id = argv.subdomain || '';
|
||||
|
||||
(function connect_proxy() {
|
||||
opt.uri = base_uri + ((prev_id) ? prev_id : '?new');
|
||||
|
||||
request(opt, function(err, res, body) {
|
||||
// request upstream url and connection info
|
||||
var request_url = function(params, cb) {
|
||||
request(params, function(err, res, body) {
|
||||
if (err) {
|
||||
console.error('tunnel server not available: %s, retry 1s', err.message);
|
||||
|
||||
// retry interval for id request
|
||||
return setTimeout(function() {
|
||||
connect_proxy();
|
||||
}, 1000);
|
||||
cb(err);
|
||||
}
|
||||
|
||||
// our assigned hostname and tcp port
|
||||
var port = body.port;
|
||||
var host = opt.host;
|
||||
var max_conn = body.max_conn_count || 1;
|
||||
cb(null, body);
|
||||
});
|
||||
};
|
||||
|
||||
// store the id so we can try to get the same one
|
||||
prev_id = body.id;
|
||||
var connect = function(opt) {
|
||||
var ev = new EventEmitter();
|
||||
|
||||
console.log('your url is: %s', body.url);
|
||||
// local port
|
||||
var local_port = opt.port;
|
||||
|
||||
var base_uri = opt.host + '/';
|
||||
|
||||
// optionally override the upstream server
|
||||
var upstream = url.parse(opt.host);
|
||||
|
||||
// no subdomain at first, maybe use requested domain
|
||||
var assigned_domain = opt.subdomain;
|
||||
|
||||
// connect to upstream given connection parameters
|
||||
var tunnel = function (remote_host, remote_port, max_conn) {
|
||||
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');
|
||||
var upstream = duplex(remote_host, remote_port, 'localhost', local_port);
|
||||
upstream.once('end', function() {
|
||||
// all upstream connections have been closed
|
||||
if (--count <= 0) {
|
||||
connect_proxy();
|
||||
tunnel(remote_host, remote_port, max_conn);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
})();
|
||||
|
||||
function duplex(port, host, local_port, local_host) {
|
||||
upstream.on('error', function(err) {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var params = {
|
||||
path: '/',
|
||||
json: true
|
||||
};
|
||||
|
||||
// where to quest
|
||||
params.uri = base_uri + ((assigned_domain) ? assigned_domain : '?new');
|
||||
|
||||
request_url(params, function(err, body) {
|
||||
|
||||
if (err) {
|
||||
ev.emit('error', new Error('tunnel server not available: %s, retry 1s', err.message));
|
||||
|
||||
// retry interval for id request
|
||||
return setTimeout(function() {
|
||||
connect_proxy(opt);
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
// our assigned hostname and tcp port
|
||||
var port = body.port;
|
||||
var host = upstream.hostname;
|
||||
|
||||
// store the id so we can try to get the same one
|
||||
assigned_domain = body.id;
|
||||
|
||||
tunnel(host, port, body.max_conn_count || 1);
|
||||
|
||||
ev.emit('url', body.url);
|
||||
});
|
||||
|
||||
return ev;
|
||||
};
|
||||
|
||||
var duplex = function(remote_host, remote_port, local_host, local_port) {
|
||||
var ev = new EventEmitter();
|
||||
|
||||
// connect to remote tcp server
|
||||
var upstream = net.createConnection(port, host);
|
||||
var internal = net.createConnection(local_port, local_host);
|
||||
var upstream = net.createConnection(remote_port, remote_host);
|
||||
var internal;
|
||||
|
||||
// when upstream connection is closed, close other associated connections
|
||||
upstream.on('end', function() {
|
||||
console.log('> upstream connection terminated');
|
||||
upstream.once('end', function() {
|
||||
ev.emit('error', new Error('upstream connection terminated'));
|
||||
|
||||
// sever connection to internal server
|
||||
// on reconnect we will re-establish
|
||||
internal.end();
|
||||
|
||||
ev.emit('end');
|
||||
});
|
||||
|
||||
upstream.on('error', function(err) {
|
||||
console.error(err);
|
||||
ev.emit('error', err);
|
||||
});
|
||||
|
||||
(function connect_internal() {
|
||||
|
||||
//internal = net.createConnection(local_port);
|
||||
internal.on('error', function(err) {
|
||||
console.log('error connecting to local server. retrying in 1s');
|
||||
internal = net.createConnection(local_port, local_host);
|
||||
internal.on('error', function() {
|
||||
ev.emit('error', new Error('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');
|
||||
ev.emit('error', new Error('disconnected from local server. retrying in 1s'));
|
||||
setTimeout(function() {
|
||||
connect_internal();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
upstream.pipe(internal);
|
||||
internal.pipe(upstream);
|
||||
upstream.pipe(internal).pipe(upstream);
|
||||
})();
|
||||
|
||||
return upstream;
|
||||
return ev;
|
||||
}
|
||||
|
||||
module.exports.connect = connect;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user