mirror of
https://github.com/bitinflow/localtunnel.git
synced 2026-03-13 21:45:54 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a9b0274ff4 | ||
|
|
83ecb29eff | ||
|
|
21df257d16 | ||
|
|
18ada0854a | ||
|
|
34afd6537d | ||
|
|
2c38aefb9d | ||
|
|
aa488f6e76 | ||
|
|
f6618953f9 | ||
|
|
092d050fa0 |
@@ -49,6 +49,12 @@ client.on('error', function(err) {
|
||||
});
|
||||
```
|
||||
|
||||
## other clients ##
|
||||
|
||||
Clients in other languages
|
||||
|
||||
*go* [gotunnelme](https://github.com/NoahShen/gotunnelme)
|
||||
|
||||
## server ##
|
||||
|
||||
See shtylman/localtunnel-server for details on the server that powers localtunnel.
|
||||
|
||||
@@ -11,12 +11,17 @@ var argv = require('optimist')
|
||||
.options('subdomain', {
|
||||
describe: 'request this subdomain'
|
||||
})
|
||||
.options('local-host', {
|
||||
describe: 'tunnel traffic to this host instead of localhost'
|
||||
})
|
||||
.default('local-host', 'localhost')
|
||||
.describe('port', 'internal http server port')
|
||||
.argv;
|
||||
|
||||
var opt = {
|
||||
host: argv.host,
|
||||
port: argv.port,
|
||||
local_host: argv['local-host'],
|
||||
subdomain: argv.subdomain,
|
||||
}
|
||||
|
||||
|
||||
64
client.js
64
client.js
@@ -2,13 +2,14 @@ var net = require('net');
|
||||
var url = require('url');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
|
||||
var after = require('after');
|
||||
var request = require('request');
|
||||
|
||||
// request upstream url and connection info
|
||||
var request_url = function(params, cb) {
|
||||
request(params, function(err, res, body) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
cb(null, body);
|
||||
@@ -18,6 +19,9 @@ var request_url = function(params, cb) {
|
||||
var connect = function(opt) {
|
||||
var ev = new EventEmitter();
|
||||
|
||||
// local host
|
||||
var local_host = opt.local_host;
|
||||
|
||||
// local port
|
||||
var local_port = opt.port;
|
||||
|
||||
@@ -30,7 +34,7 @@ var connect = function(opt) {
|
||||
var assigned_domain = opt.subdomain;
|
||||
|
||||
// connect to upstream given connection parameters
|
||||
var tunnel = function (remote_host, remote_port) {
|
||||
var tunnel = function (remote_host, remote_port, dead) {
|
||||
|
||||
var remote_opt = {
|
||||
host: remote_host,
|
||||
@@ -38,7 +42,7 @@ var connect = function(opt) {
|
||||
};
|
||||
|
||||
var local_opt = {
|
||||
host: 'localhost',
|
||||
host: local_host,
|
||||
port: local_port
|
||||
};
|
||||
|
||||
@@ -49,9 +53,9 @@ var connect = function(opt) {
|
||||
return;
|
||||
}
|
||||
|
||||
// we need a new tunnel
|
||||
if (++remote_attempts >= 3) {
|
||||
console.error('localtunnel server offline - try again');
|
||||
process.exit(-1);
|
||||
return dead();
|
||||
}
|
||||
|
||||
// connection to localtunnel server
|
||||
@@ -108,32 +112,42 @@ var connect = function(opt) {
|
||||
// where to quest
|
||||
params.uri = base_uri + ((assigned_domain) ? assigned_domain : '?new');
|
||||
|
||||
// get an id from lt server and setup forwarding tcp connections
|
||||
request_url(params, function(err, body) {
|
||||
function init_tunnel() {
|
||||
// get an id from lt server and setup forwarding tcp connections
|
||||
request_url(params, function(err, body) {
|
||||
if (err) {
|
||||
ev.emit('error', new Error('tunnel server not available: ' + err.message + ', retry 1s'));
|
||||
|
||||
if (err) {
|
||||
ev.emit('error', new Error('tunnel server not available: %s, retry 1s', err.message));
|
||||
// retry interval for id request
|
||||
return setTimeout(function() {
|
||||
init_tunnel();
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// store the id so we can try to get the same one
|
||||
assigned_domain = body.id;
|
||||
var max_conn = body.max_conn_count || 1;
|
||||
|
||||
var max_conn = body.max_conn_count || 1;
|
||||
for (var count = 0 ; count < max_conn ; ++count) {
|
||||
tunnel(host, port);
|
||||
}
|
||||
// after all our tunnels die, we ask for new ones
|
||||
// this might happen if the upstream server dies
|
||||
var dead = after(max_conn, function() {
|
||||
init_tunnel();
|
||||
});
|
||||
|
||||
ev.emit('url', body.url);
|
||||
});
|
||||
for (var count = 0 ; count < max_conn ; ++count) {
|
||||
tunnel(host, port, dead);
|
||||
}
|
||||
|
||||
ev.emit('url', body.url);
|
||||
});
|
||||
}
|
||||
|
||||
init_tunnel();
|
||||
|
||||
return ev;
|
||||
};
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
"author": "Roman Shtylman <shtylman@gmail.com>",
|
||||
"name": "localtunnel",
|
||||
"description": "expose localhost to the world",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.3",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/shtylman/localtunnel.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"request": "2.11.4",
|
||||
"optimist": "0.3.4"
|
||||
"optimist": "0.3.4",
|
||||
"after": "0.8.1"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"bin": {
|
||||
|
||||
Reference in New Issue
Block a user