mirror of
https://github.com/bitinflow/localtunnel.git
synced 2026-03-14 22:15:55 +00:00
Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f97434a69 | ||
|
|
a46cd02fcb | ||
|
|
792d9f19bd | ||
|
|
d0b483b92b | ||
|
|
59d96a3cc6 | ||
|
|
b516ecccfa | ||
|
|
f68b1f06d9 | ||
|
|
4d09875163 | ||
|
|
2773fe6923 | ||
|
|
457bd64ecc | ||
|
|
eb31659345 | ||
|
|
3ee8b1b884 | ||
|
|
15aac729bb | ||
|
|
e73cfe3e45 | ||
|
|
790a642a83 | ||
|
|
e6539e1225 | ||
|
|
4c0a5dc4eb | ||
|
|
809262cf3d | ||
|
|
ddb47d2f90 | ||
|
|
a6845ec63b | ||
|
|
adecf03f41 | ||
|
|
9bdb40e97c | ||
|
|
006ce7733b | ||
|
|
624d279c26 | ||
|
|
c2b8f2b7ab | ||
|
|
4d9dcc1711 | ||
|
|
7b1fef982f | ||
|
|
b1ebef2b0b | ||
|
|
9ad43778b7 | ||
|
|
887c444543 | ||
|
|
828cb2afcb | ||
|
|
8768329fdd | ||
|
|
3026d6a42c | ||
|
|
abd461f83a | ||
|
|
2acea3d77f | ||
|
|
5d0eb3382a | ||
|
|
3b67c8a8ce | ||
|
|
71552a336e | ||
|
|
87a23bf28c | ||
|
|
3d54de851f | ||
|
|
92bb807908 | ||
|
|
afbdc3697e | ||
|
|
0049f21b55 | ||
|
|
509841104b | ||
|
|
92caf2f204 | ||
|
|
9487797e02 | ||
|
|
a42f6a8d8d | ||
|
|
14b4bcb96f | ||
|
|
4aa65002eb | ||
|
|
08676ba81d | ||
|
|
174e7f3982 | ||
|
|
44be55cd7b | ||
|
|
5c6cd2359c | ||
|
|
2f6f9459ad | ||
|
|
7217a08a05 | ||
|
|
fbfc923a7e | ||
|
|
d9bc11b520 | ||
|
|
ad64611bd1 |
1
.npmignore
Normal file
1
.npmignore
Normal file
@@ -0,0 +1 @@
|
|||||||
|
support
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- 0.8
|
- "0.10"
|
||||||
- 0.9
|
|
||||||
|
|||||||
32
History.md
Normal file
32
History.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# 1.7.0 (2015-07-22)
|
||||||
|
|
||||||
|
* add short arg options
|
||||||
|
|
||||||
|
# 1.6.0 (2015-05-15)
|
||||||
|
|
||||||
|
* keep sockets alive after connecting
|
||||||
|
* add --open param to CLI
|
||||||
|
|
||||||
|
# 1.5.0 (2014-10-25)
|
||||||
|
|
||||||
|
* capture all errors on remote socket and restart the tunnel
|
||||||
|
|
||||||
|
# 1.4.0 (2014-08-31)
|
||||||
|
|
||||||
|
* don't emit errors for ETIMEDOUT
|
||||||
|
|
||||||
|
# 1.2.0 / 2014-04-28
|
||||||
|
|
||||||
|
* return `client` from `localtunnel` API instantiation
|
||||||
|
|
||||||
|
# 1.1.0 / 2014-02-24
|
||||||
|
|
||||||
|
* add a host header transform to change the 'Host' header in requests
|
||||||
|
|
||||||
|
# 1.0.0 / 2014-02-14
|
||||||
|
|
||||||
|
* default to localltunnel.me for host
|
||||||
|
* remove exported `connect` method (just export one function that does the same thing)
|
||||||
|
* change localtunnel signature to (port, opt, fn)
|
||||||
|
|
||||||
|
# 0.2.2 / 2014-01-09
|
||||||
58
README.md
58
README.md
@@ -1,4 +1,4 @@
|
|||||||
# localtunnel #
|
# localtunnel [](https://travis-ci.org/defunctzombie/localtunnel) #
|
||||||
|
|
||||||
localtunnel exposes your localhost to the world for easy testing and sharing! No need to mess with DNS or deploy just to have others test out your changes.
|
localtunnel exposes your localhost to the world for easy testing and sharing! No need to mess with DNS or deploy just to have others test out your changes.
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ This will install the localtunnel module globally and add the 'lt' client cli to
|
|||||||
|
|
||||||
## use ##
|
## use ##
|
||||||
|
|
||||||
Super Easy! Assuming your local server is running on port 8000, just use the ```lt``` command to start the tunnel.
|
Assuming your local server is running on port 8000, just use the ```lt``` command to start the tunnel.
|
||||||
|
|
||||||
```
|
```
|
||||||
lt --port 8000
|
lt --port 8000
|
||||||
@@ -24,31 +24,57 @@ Thats it! It will connect to the tunnel server, setup the tunnel, and tell you w
|
|||||||
|
|
||||||
You can restart your local server all you want, ```lt``` is smart enough to detect this and reconnect once it is back.
|
You can restart your local server all you want, ```lt``` is smart enough to detect this and reconnect once it is back.
|
||||||
|
|
||||||
|
### arguments
|
||||||
|
|
||||||
|
Below are some common arguments. See `lt --help` for additional arguments
|
||||||
|
|
||||||
|
* `--subdomain` request a named subdomain on the localtunnel server (default is random characters)
|
||||||
|
* `--local-host` proxy to a hostname other than localhost
|
||||||
|
|
||||||
## API ##
|
## API ##
|
||||||
|
|
||||||
The localtunnel client is also usable through an API (for test integration, automation, etc)
|
The localtunnel client is also usable through an API (for test integration, automation, etc)
|
||||||
|
|
||||||
|
### localtunnel(port [,opts], fn)
|
||||||
|
|
||||||
|
Creates a new localtunnel to the specified local `port`. `fn` will be called once you have been assigned a public localtunnel url. `opts` can be used to request a specific `subdomain`.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
var localtunnel = require('localtunnel');
|
var localtunnel = require('localtunnel');
|
||||||
|
|
||||||
var client = localtunnel.connect({
|
var tunnel = localtunnel(port, function(err, tunnel) {
|
||||||
// the localtunnel server
|
if (err) ...
|
||||||
host: 'http://localtunnel.me',
|
|
||||||
// your local application port
|
// the assigned public url for your tunnel
|
||||||
port: 12345
|
// i.e. https://abcdefgjhij.localtunnel.me
|
||||||
|
tunnel.url;
|
||||||
});
|
});
|
||||||
|
|
||||||
// when your are assigned a url
|
tunnel.on('close', function() {
|
||||||
client.on('url', function(url) {
|
// tunnels are closed
|
||||||
// you can now make http requests to the url
|
|
||||||
// they will be proxied to your local server on port [12345]
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('error', function(err) {
|
|
||||||
// uh oh!
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### opts
|
||||||
|
|
||||||
|
* `subdomain` A *string* value requesting a specific subdomain on the proxy server. **Note** You may not actually receive this name depending on availablily.
|
||||||
|
* `local_host` Proxy to this hostname instead of `localhost`. This will also cause the `Host` header to be re-written to this value in proxied requests.
|
||||||
|
|
||||||
|
### Tunnel
|
||||||
|
|
||||||
|
The `tunnel` instance returned to your callback emits the following events
|
||||||
|
|
||||||
|
|event|args|description|
|
||||||
|
|----|----|----|
|
||||||
|
|error|err|fires when an error happens on the tunnel|
|
||||||
|
|close||fires when the tunnel has closed|
|
||||||
|
|
||||||
|
The `tunnel instance has the following methods
|
||||||
|
|
||||||
|
|method|args|description|
|
||||||
|
|----|----|----|
|
||||||
|
|close||close the tunnel|
|
||||||
|
|
||||||
## other clients ##
|
## other clients ##
|
||||||
|
|
||||||
Clients in other languages
|
Clients in other languages
|
||||||
@@ -57,7 +83,7 @@ Clients in other languages
|
|||||||
|
|
||||||
## server ##
|
## server ##
|
||||||
|
|
||||||
See shtylman/localtunnel-server for details on the server that powers localtunnel.
|
See defunctzombie/localtunnel-server for details on the server that powers localtunnel.
|
||||||
|
|
||||||
## License ##
|
## License ##
|
||||||
MIT
|
MIT
|
||||||
|
|||||||
63
bin/client
63
bin/client
@@ -1,39 +1,62 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
var lt_client = require('../client');
|
var lt_client = require('../client');
|
||||||
|
var open_url = require('openurl');
|
||||||
|
|
||||||
var argv = require('optimist')
|
var argv = require('yargs')
|
||||||
.usage('Usage: $0 --port [num]')
|
.usage('Usage: $0 --port [num] <options>')
|
||||||
.demand(['port'])
|
.option('h', {
|
||||||
.options('host', {
|
alias: 'host',
|
||||||
default: 'http://localtunnel.me',
|
describe: 'Upstream server providing forwarding',
|
||||||
describe: 'upstream server providing forwarding'
|
default: 'http://localtunnel.me'
|
||||||
})
|
})
|
||||||
.options('subdomain', {
|
.option('s', {
|
||||||
describe: 'request this subdomain'
|
alias: 'subdomain',
|
||||||
|
describe: 'Request this subdomain'
|
||||||
})
|
})
|
||||||
.options('local-host', {
|
.option('l', {
|
||||||
describe: 'tunnel traffic to this host instead of localhost'
|
alias: 'local-host',
|
||||||
|
describe: 'Tunnel traffic to this host instead of localhost, override Host header to this host'
|
||||||
})
|
})
|
||||||
.default('local-host', 'localhost')
|
.options('o', {
|
||||||
.describe('port', 'internal http server port')
|
alias: 'open',
|
||||||
|
describe: 'opens url in your browser'
|
||||||
|
})
|
||||||
|
.option('p', {
|
||||||
|
alias: 'port',
|
||||||
|
describe: 'Internal http server port',
|
||||||
|
})
|
||||||
|
.require('port')
|
||||||
|
.help('help', 'Show this help and exit')
|
||||||
|
.version(require('../package').version)
|
||||||
.argv;
|
.argv;
|
||||||
|
|
||||||
|
if (typeof argv.port !== 'number') {
|
||||||
|
require('yargs').showHelp();
|
||||||
|
console.error('port must be a number');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
var opt = {
|
var opt = {
|
||||||
host: argv.host,
|
host: argv.host,
|
||||||
port: argv.port,
|
port: argv.port,
|
||||||
local_host: argv['local-host'],
|
local_host: argv['local-host'],
|
||||||
subdomain: argv.subdomain,
|
subdomain: argv.subdomain,
|
||||||
}
|
};
|
||||||
|
|
||||||
var client = lt_client.connect(opt);
|
lt_client(opt.port, opt, function(err, tunnel) {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
// only emitted when the url changes
|
console.log('your url is: %s', tunnel.url);
|
||||||
client.on('url', function(url) {
|
|
||||||
console.log('your url is: %s', url);
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on('error', function(err) {
|
if (argv.open) {
|
||||||
console.error(err);
|
open_url.open(tunnel.url);
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnel.on('error', function(err) {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// vim: ft=javascript
|
// vim: ft=javascript
|
||||||
|
|||||||
253
client.js
253
client.js
@@ -1,247 +1,24 @@
|
|||||||
var net = require('net');
|
|
||||||
var url = require('url');
|
|
||||||
var EventEmitter = require('events').EventEmitter;
|
var EventEmitter = require('events').EventEmitter;
|
||||||
|
|
||||||
var request = require('request');
|
|
||||||
var debug = require('debug')('localtunnel:client');
|
var debug = require('debug')('localtunnel:client');
|
||||||
|
|
||||||
// manages groups of tunnels
|
var Tunnel = require('./lib/Tunnel');
|
||||||
var TunnelCluster = function(opt) {
|
|
||||||
if (!(this instanceof TunnelCluster)) {
|
module.exports = function localtunnel(port, opt, fn) {
|
||||||
return new TunnelCluster(opt);
|
if (typeof opt === 'function') {
|
||||||
|
fn = opt;
|
||||||
|
opt = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this;
|
opt = opt || {};
|
||||||
self._opt = opt;
|
opt.port = port;
|
||||||
|
|
||||||
EventEmitter.call(self);
|
|
||||||
};
|
|
||||||
|
|
||||||
TunnelCluster.prototype.__proto__ = EventEmitter.prototype;
|
|
||||||
|
|
||||||
// establish a new tunnel
|
|
||||||
TunnelCluster.prototype.open = function() {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
var opt = self._opt || {};
|
|
||||||
|
|
||||||
var remote_host = opt.remote_host;
|
|
||||||
var remote_port = opt.remote_port;
|
|
||||||
|
|
||||||
var local_host = opt.local_host;
|
|
||||||
var local_port = opt.local_port;
|
|
||||||
|
|
||||||
debug('establishing tunnel %s:%s <> %s:%s', local_host, local_port, remote_host, remote_port);
|
|
||||||
|
|
||||||
// connection to localtunnel server
|
|
||||||
var remote = net.connect({
|
|
||||||
host: remote_host,
|
|
||||||
port: remote_port
|
|
||||||
});
|
|
||||||
|
|
||||||
remote.once('error', function(err) {
|
|
||||||
// emit connection refused errors immediately, because they
|
|
||||||
// indicate that the tunnel can't be established.
|
|
||||||
if (err.code === 'ECONNREFUSED') {
|
|
||||||
self.emit('error', new Error('connection refused: ' + remote_host + ':' + remote_port + ' (check your firewall settings)'));
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
self.emit('error', err);
|
|
||||||
}
|
|
||||||
|
|
||||||
setTimeout(function() {
|
|
||||||
self.emit('dead');
|
|
||||||
}, 1000);
|
|
||||||
});
|
|
||||||
|
|
||||||
function conn_local() {
|
|
||||||
debug('connecting locally to %s:%d', local_host, local_port);
|
|
||||||
|
|
||||||
if (remote.destroyed) {
|
|
||||||
self.emit('dead');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
remote.pause();
|
|
||||||
|
|
||||||
// connection to local http server
|
|
||||||
var local = net.connect({
|
|
||||||
host: local_host,
|
|
||||||
port: local_port
|
|
||||||
});
|
|
||||||
|
|
||||||
function remote_close() {
|
|
||||||
self.emit('dead');
|
|
||||||
local.end();
|
|
||||||
};
|
|
||||||
|
|
||||||
remote.once('close', remote_close);
|
|
||||||
|
|
||||||
local.on('error', function(err) {
|
|
||||||
local.end();
|
|
||||||
|
|
||||||
remote.removeListener('close', remote_close);
|
|
||||||
|
|
||||||
if (err.code !== 'ECONNREFUSED') {
|
|
||||||
return local.emit('error', err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// retrying connection to local server
|
|
||||||
setTimeout(conn_local, 1000);
|
|
||||||
});
|
|
||||||
|
|
||||||
local.once('connect', function() {
|
|
||||||
debug('connected locally');
|
|
||||||
remote.resume();
|
|
||||||
remote.pipe(local).pipe(remote);
|
|
||||||
|
|
||||||
// when local closes, also get a new remote
|
|
||||||
local.once('close', function(had_error) {
|
|
||||||
debug('local connection closed [%s]', had_error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// tunnel is considered open when remote connects
|
|
||||||
remote.once('connect', function() {
|
|
||||||
self.emit('open', remote);
|
|
||||||
});
|
|
||||||
remote.once('connect', conn_local);
|
|
||||||
};
|
|
||||||
|
|
||||||
var Tunnel = function(opt) {
|
|
||||||
if (!(this instanceof Tunnel)) {
|
|
||||||
return new Tunnel(opt);
|
|
||||||
}
|
|
||||||
|
|
||||||
var self = this;
|
|
||||||
self._closed = false;
|
|
||||||
self._opt = opt;
|
|
||||||
};
|
|
||||||
|
|
||||||
Tunnel.prototype.__proto__ = EventEmitter.prototype;
|
|
||||||
|
|
||||||
// initialize connection
|
|
||||||
// callback with connection info
|
|
||||||
Tunnel.prototype._init = function(cb) {
|
|
||||||
var self = this;
|
|
||||||
var opt = self._opt;
|
|
||||||
|
|
||||||
var params = {
|
|
||||||
path: '/',
|
|
||||||
json: true
|
|
||||||
};
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
// where to quest
|
|
||||||
params.uri = base_uri + ((assigned_domain) ? assigned_domain : '?new');
|
|
||||||
|
|
||||||
(function get_url() {
|
|
||||||
request(params, function(err, res, body) {
|
|
||||||
if (err) {
|
|
||||||
// TODO (shtylman) don't print to stdout?
|
|
||||||
console.log('tunnel server offline: ' + err.message + ', retry 1s');
|
|
||||||
return setTimeout(get_url, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
var port = body.port;
|
|
||||||
var host = upstream.hostname;
|
|
||||||
|
|
||||||
var max_conn = body.max_conn_count || 1;
|
|
||||||
|
|
||||||
cb(null, {
|
|
||||||
remote_host: upstream.hostname,
|
|
||||||
remote_port: body.port,
|
|
||||||
name: body.id,
|
|
||||||
url: body.url,
|
|
||||||
max_conn: max_conn
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})();
|
|
||||||
};
|
|
||||||
|
|
||||||
Tunnel.prototype._establish = function(info) {
|
|
||||||
var self = this;
|
|
||||||
var opt = self._opt;
|
|
||||||
|
|
||||||
info.local_host = opt.local_host || 'localhost';
|
|
||||||
info.local_port = opt.port;
|
|
||||||
|
|
||||||
var tunnels = self.tunnel_cluster = TunnelCluster(info);
|
|
||||||
|
|
||||||
// only emit the url the first time
|
|
||||||
tunnels.once('open', function() {
|
|
||||||
self.emit('url', info.url);
|
|
||||||
});
|
|
||||||
|
|
||||||
var tunnel_count = 0;
|
|
||||||
|
|
||||||
// track open count
|
|
||||||
tunnels.on('open', function(tunnel) {
|
|
||||||
tunnel_count++;
|
|
||||||
debug('tunnel open [total: %d]', tunnel_count);
|
|
||||||
|
|
||||||
var close_handler = function() {
|
|
||||||
tunnel.destroy();
|
|
||||||
};
|
|
||||||
|
|
||||||
if (self._closed) {
|
|
||||||
return close_handler();
|
|
||||||
}
|
|
||||||
|
|
||||||
self.once('close', close_handler);
|
|
||||||
tunnel.once('close', function() {
|
|
||||||
self.removeListener('close', close_handler);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// when a tunnel dies, open a new one
|
|
||||||
tunnels.on('dead', function(tunnel) {
|
|
||||||
tunnel_count--;
|
|
||||||
debug('tunnel dead [total: %d]', tunnel_count);
|
|
||||||
|
|
||||||
if (self._closed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
tunnels.open();
|
|
||||||
});
|
|
||||||
|
|
||||||
// establish as many tunnels as allowed
|
|
||||||
for (var count = 0 ; count < info.max_conn ; ++count) {
|
|
||||||
tunnels.open();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Tunnel.prototype.open = function() {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
self._init(function(err, info) {
|
|
||||||
if (err) {
|
|
||||||
return self.emit('error', err);
|
|
||||||
}
|
|
||||||
|
|
||||||
self._establish(info);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// shutdown tunnels
|
|
||||||
Tunnel.prototype.close = function() {
|
|
||||||
var self = this;
|
|
||||||
|
|
||||||
self._closed = true;
|
|
||||||
self.emit('close');
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.connect = function(opt) {
|
|
||||||
var client = Tunnel(opt);
|
var client = Tunnel(opt);
|
||||||
client.open();
|
client.open(function(err) {
|
||||||
|
if (err) {
|
||||||
|
return fn(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn(null, client);
|
||||||
|
});
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
|||||||
39
lib/HeaderHostTransformer.js
Normal file
39
lib/HeaderHostTransformer.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
var stream = require('stream');
|
||||||
|
var util = require('util');
|
||||||
|
|
||||||
|
var Transform = stream.Transform;
|
||||||
|
|
||||||
|
var HeaderHostTransformer = function(opts) {
|
||||||
|
if (!(this instanceof HeaderHostTransformer)) {
|
||||||
|
return new HeaderHostTransformer(opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = opts || {}
|
||||||
|
Transform.call(this, opts);
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
self.host = opts.host || 'localhost';
|
||||||
|
self.replaced = false;
|
||||||
|
}
|
||||||
|
util.inherits(HeaderHostTransformer, Transform);
|
||||||
|
|
||||||
|
HeaderHostTransformer.prototype._transform = function (chunk, enc, cb) {
|
||||||
|
var self = this;
|
||||||
|
chunk = chunk.toString();
|
||||||
|
|
||||||
|
// after replacing the first instance of the Host header
|
||||||
|
// we just become a regular passthrough
|
||||||
|
if (!self.replaced) {
|
||||||
|
self.push(chunk.replace(/(\r\nHost: )\S+/, function(match, $1) {
|
||||||
|
self.replaced = true;
|
||||||
|
return $1 + self.host;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.push(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
cb();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = HeaderHostTransformer;
|
||||||
152
lib/Tunnel.js
Normal file
152
lib/Tunnel.js
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
var url = require('url');
|
||||||
|
var EventEmitter = require('events').EventEmitter;
|
||||||
|
var request = require('request');
|
||||||
|
var debug = require('debug')('localtunnel:client');
|
||||||
|
|
||||||
|
var TunnelCluster = require('./TunnelCluster');
|
||||||
|
|
||||||
|
var Tunnel = function(opt) {
|
||||||
|
if (!(this instanceof Tunnel)) {
|
||||||
|
return new Tunnel(opt);
|
||||||
|
}
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
self._closed = false;
|
||||||
|
self._opt = opt || {};
|
||||||
|
|
||||||
|
self._opt.host = self._opt.host || 'https://localtunnel.me';
|
||||||
|
};
|
||||||
|
|
||||||
|
Tunnel.prototype.__proto__ = EventEmitter.prototype;
|
||||||
|
|
||||||
|
// initialize connection
|
||||||
|
// callback with connection info
|
||||||
|
Tunnel.prototype._init = function(cb) {
|
||||||
|
var self = this;
|
||||||
|
var opt = self._opt;
|
||||||
|
|
||||||
|
var params = {
|
||||||
|
path: '/',
|
||||||
|
json: true
|
||||||
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// where to quest
|
||||||
|
params.uri = base_uri + ((assigned_domain) ? assigned_domain : '?new');
|
||||||
|
|
||||||
|
(function get_url() {
|
||||||
|
request(params, function(err, res, body) {
|
||||||
|
if (err) {
|
||||||
|
// TODO (shtylman) don't print to stdout?
|
||||||
|
console.log('tunnel server offline: ' + err.message + ', retry 1s');
|
||||||
|
return setTimeout(get_url, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.statusCode !== 200) {
|
||||||
|
var err = new Error((body && body.message) || 'localtunnel server returned an error, please try again');
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
var port = body.port;
|
||||||
|
var host = upstream.hostname;
|
||||||
|
|
||||||
|
var max_conn = body.max_conn_count || 1;
|
||||||
|
|
||||||
|
cb(null, {
|
||||||
|
remote_host: upstream.hostname,
|
||||||
|
remote_port: body.port,
|
||||||
|
name: body.id,
|
||||||
|
url: body.url,
|
||||||
|
max_conn: max_conn
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
};
|
||||||
|
|
||||||
|
Tunnel.prototype._establish = function(info) {
|
||||||
|
var self = this;
|
||||||
|
var opt = self._opt;
|
||||||
|
|
||||||
|
// increase max event listeners so that localtunnel consumers don't get
|
||||||
|
// warning messages as soon as they setup even one listener. See #71
|
||||||
|
self.setMaxListeners(info.max_conn + (EventEmitter.defaultMaxListeners || 10));
|
||||||
|
|
||||||
|
info.local_host = opt.local_host;
|
||||||
|
info.local_port = opt.port;
|
||||||
|
|
||||||
|
var tunnels = self.tunnel_cluster = TunnelCluster(info);
|
||||||
|
|
||||||
|
// only emit the url the first time
|
||||||
|
tunnels.once('open', function() {
|
||||||
|
self.emit('url', info.url);
|
||||||
|
});
|
||||||
|
|
||||||
|
var tunnel_count = 0;
|
||||||
|
|
||||||
|
// track open count
|
||||||
|
tunnels.on('open', function(tunnel) {
|
||||||
|
tunnel_count++;
|
||||||
|
debug('tunnel open [total: %d]', tunnel_count);
|
||||||
|
|
||||||
|
var close_handler = function() {
|
||||||
|
tunnel.destroy();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (self._closed) {
|
||||||
|
return close_handler();
|
||||||
|
}
|
||||||
|
|
||||||
|
self.once('close', close_handler);
|
||||||
|
tunnel.once('close', function() {
|
||||||
|
self.removeListener('close', close_handler);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// when a tunnel dies, open a new one
|
||||||
|
tunnels.on('dead', function(tunnel) {
|
||||||
|
tunnel_count--;
|
||||||
|
debug('tunnel dead [total: %d]', tunnel_count);
|
||||||
|
|
||||||
|
if (self._closed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnels.open();
|
||||||
|
});
|
||||||
|
|
||||||
|
// establish as many tunnels as allowed
|
||||||
|
for (var count = 0 ; count < info.max_conn ; ++count) {
|
||||||
|
tunnels.open();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Tunnel.prototype.open = function(cb) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self._init(function(err, info) {
|
||||||
|
if (err) {
|
||||||
|
return cb(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.url = info.url;
|
||||||
|
self._establish(info);
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// shutdown tunnels
|
||||||
|
Tunnel.prototype.close = function() {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
self._closed = true;
|
||||||
|
self.emit('close');
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Tunnel;
|
||||||
123
lib/TunnelCluster.js
Normal file
123
lib/TunnelCluster.js
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
var EventEmitter = require('events').EventEmitter;
|
||||||
|
var debug = require('debug')('localtunnel:client');
|
||||||
|
var net = require('net');
|
||||||
|
|
||||||
|
var HeaderHostTransformer = require('./HeaderHostTransformer');
|
||||||
|
|
||||||
|
// manages groups of tunnels
|
||||||
|
var TunnelCluster = function(opt) {
|
||||||
|
if (!(this instanceof TunnelCluster)) {
|
||||||
|
return new TunnelCluster(opt);
|
||||||
|
}
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
self._opt = opt;
|
||||||
|
|
||||||
|
EventEmitter.call(self);
|
||||||
|
};
|
||||||
|
|
||||||
|
TunnelCluster.prototype.__proto__ = EventEmitter.prototype;
|
||||||
|
|
||||||
|
// establish a new tunnel
|
||||||
|
TunnelCluster.prototype.open = function() {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
var opt = self._opt || {};
|
||||||
|
|
||||||
|
var remote_host = opt.remote_host;
|
||||||
|
var remote_port = opt.remote_port;
|
||||||
|
|
||||||
|
var local_host = opt.local_host || 'localhost';
|
||||||
|
var local_port = opt.local_port;
|
||||||
|
|
||||||
|
debug('establishing tunnel %s:%s <> %s:%s', local_host, local_port, remote_host, remote_port);
|
||||||
|
|
||||||
|
// connection to localtunnel server
|
||||||
|
var remote = net.connect({
|
||||||
|
host: remote_host,
|
||||||
|
port: remote_port
|
||||||
|
});
|
||||||
|
|
||||||
|
remote.setKeepAlive(true);
|
||||||
|
|
||||||
|
remote.on('error', function(err) {
|
||||||
|
// emit connection refused errors immediately, because they
|
||||||
|
// indicate that the tunnel can't be established.
|
||||||
|
if (err.code === 'ECONNREFUSED') {
|
||||||
|
self.emit('error', new Error('connection refused: ' + remote_host + ':' + remote_port + ' (check your firewall settings)'));
|
||||||
|
}
|
||||||
|
|
||||||
|
remote.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
function conn_local() {
|
||||||
|
if (remote.destroyed) {
|
||||||
|
debug('remote destroyed');
|
||||||
|
self.emit('dead');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug('connecting locally to %s:%d', local_host, local_port);
|
||||||
|
remote.pause();
|
||||||
|
|
||||||
|
// connection to local http server
|
||||||
|
var local = net.connect({
|
||||||
|
host: local_host,
|
||||||
|
port: local_port
|
||||||
|
});
|
||||||
|
|
||||||
|
function remote_close() {
|
||||||
|
debug('remote close');
|
||||||
|
self.emit('dead');
|
||||||
|
local.end();
|
||||||
|
};
|
||||||
|
|
||||||
|
remote.once('close', remote_close);
|
||||||
|
|
||||||
|
// TODO some languages have single threaded servers which makes opening up
|
||||||
|
// multiple local connections impossible. We need a smarter way to scale
|
||||||
|
// and adjust for such instances to avoid beating on the door of the server
|
||||||
|
local.once('error', function(err) {
|
||||||
|
debug('local error %s', err.message);
|
||||||
|
local.end();
|
||||||
|
|
||||||
|
remote.removeListener('close', remote_close);
|
||||||
|
|
||||||
|
if (err.code !== 'ECONNREFUSED') {
|
||||||
|
return remote.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
// retrying connection to local server
|
||||||
|
setTimeout(conn_local, 1000);
|
||||||
|
});
|
||||||
|
|
||||||
|
local.once('connect', function() {
|
||||||
|
debug('connected locally');
|
||||||
|
remote.resume();
|
||||||
|
|
||||||
|
var stream = remote;
|
||||||
|
|
||||||
|
// if user requested specific local host
|
||||||
|
// then we use host header transform to replace the host header
|
||||||
|
if (opt.local_host) {
|
||||||
|
debug('transform Host header to %s', opt.local_host);
|
||||||
|
stream = remote.pipe(HeaderHostTransformer({ host: opt.local_host }));
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.pipe(local).pipe(remote);
|
||||||
|
|
||||||
|
// when local closes, also get a new remote
|
||||||
|
local.once('close', function(had_error) {
|
||||||
|
debug('local connection closed [%s]', had_error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// tunnel is considered open when remote connects
|
||||||
|
remote.once('connect', function() {
|
||||||
|
self.emit('open', remote);
|
||||||
|
conn_local();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = TunnelCluster;
|
||||||
15
package.json
15
package.json
@@ -2,17 +2,24 @@
|
|||||||
"author": "Roman Shtylman <shtylman@gmail.com>",
|
"author": "Roman Shtylman <shtylman@gmail.com>",
|
||||||
"name": "localtunnel",
|
"name": "localtunnel",
|
||||||
"description": "expose localhost to the world",
|
"description": "expose localhost to the world",
|
||||||
"version": "0.2.2",
|
"version": "1.7.0",
|
||||||
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/shtylman/localtunnel.git"
|
"url": "git://github.com/shtylman/localtunnel.git"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"request": "2.11.4",
|
"request": "2.11.4",
|
||||||
"optimist": "0.3.4",
|
"yargs": "3.15.0",
|
||||||
"debug": "0.7.4"
|
"debug": "0.7.4",
|
||||||
|
"openurl": "1.1.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "~1.17.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "mocha --ui qunit --reporter list --timeout 10000 -- test/index.js"
|
||||||
},
|
},
|
||||||
"devDependencies": {},
|
|
||||||
"bin": {
|
"bin": {
|
||||||
"lt": "./bin/client"
|
"lt": "./bin/client"
|
||||||
},
|
},
|
||||||
|
|||||||
188
test/index.js
Normal file
188
test/index.js
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
var http = require('http');
|
||||||
|
var https = require('https');
|
||||||
|
var url = require('url');
|
||||||
|
var assert = require('assert');
|
||||||
|
|
||||||
|
var localtunnel = require('../');
|
||||||
|
|
||||||
|
test('setup local http server', function(done) {
|
||||||
|
var server = http.createServer();
|
||||||
|
server.on('request', function(req, res) {
|
||||||
|
res.write(req.headers.host);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
server.listen(function() {
|
||||||
|
var port = server.address().port;
|
||||||
|
|
||||||
|
test._fake_port = port;
|
||||||
|
console.log('local http on:', port);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('setup localtunnel client', function(done) {
|
||||||
|
localtunnel(test._fake_port, function(err, tunnel) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(tunnel.url));
|
||||||
|
test._fake_url = tunnel.url;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('query localtunnel server w/ ident', function(done) {
|
||||||
|
var uri = test._fake_url;
|
||||||
|
var parsed = url.parse(uri);
|
||||||
|
|
||||||
|
var opt = {
|
||||||
|
host: parsed.host,
|
||||||
|
port: 443,
|
||||||
|
headers: {
|
||||||
|
host: parsed.hostname
|
||||||
|
},
|
||||||
|
path: '/'
|
||||||
|
};
|
||||||
|
|
||||||
|
var req = https.request(opt, function(res) {
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
var body = '';
|
||||||
|
|
||||||
|
res.on('data', function(chunk) {
|
||||||
|
body += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', function() {
|
||||||
|
assert(/.*[.]localtunnel[.]me/.test(body), body);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('request specific domain', function(done) {
|
||||||
|
localtunnel(test._fake_port, { subdomain: 'abcd' }, function(err, tunnel) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.ok(new RegExp('^https:\/\/abcd.localtunnel.me' + '$').test(tunnel.url));
|
||||||
|
tunnel.close();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
suite('--local-host localhost');
|
||||||
|
|
||||||
|
test('setup localtunnel client', function(done) {
|
||||||
|
var opt = {
|
||||||
|
local_host: 'localhost'
|
||||||
|
};
|
||||||
|
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(tunnel.url));
|
||||||
|
test._fake_url = tunnel.url;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('override Host header with local-host', function(done) {
|
||||||
|
var uri = test._fake_url;
|
||||||
|
var parsed = url.parse(uri);
|
||||||
|
|
||||||
|
var opt = {
|
||||||
|
host: parsed.host,
|
||||||
|
port: 443,
|
||||||
|
headers: {
|
||||||
|
host: parsed.hostname
|
||||||
|
},
|
||||||
|
path: '/'
|
||||||
|
};
|
||||||
|
|
||||||
|
var req = https.request(opt, function(res) {
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
var body = '';
|
||||||
|
|
||||||
|
res.on('data', function(chunk) {
|
||||||
|
body += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', function() {
|
||||||
|
assert.equal(body, 'localhost');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
suite('--local-host 127.0.0.1');
|
||||||
|
|
||||||
|
test('setup localtunnel client', function(done) {
|
||||||
|
var opt = {
|
||||||
|
local_host: '127.0.0.1'
|
||||||
|
};
|
||||||
|
localtunnel(test._fake_port, opt, function(err, tunnel) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.ok(new RegExp('^https:\/\/.*localtunnel.me' + '$').test(tunnel.url));
|
||||||
|
test._fake_url = tunnel.url;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('override Host header with local-host', function(done) {
|
||||||
|
var uri = test._fake_url;
|
||||||
|
var parsed = url.parse(uri);
|
||||||
|
|
||||||
|
var opt = {
|
||||||
|
host: parsed.host,
|
||||||
|
port: 443,
|
||||||
|
headers: {
|
||||||
|
host: parsed.hostname
|
||||||
|
},
|
||||||
|
path: '/'
|
||||||
|
};
|
||||||
|
|
||||||
|
var req = https.request(opt, function(res) {
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
var body = '';
|
||||||
|
|
||||||
|
res.on('data', function(chunk) {
|
||||||
|
body += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', function() {
|
||||||
|
assert.equal(body, '127.0.0.1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('send chunked request', function(done) {
|
||||||
|
var uri = test._fake_url;
|
||||||
|
var parsed = url.parse(uri);
|
||||||
|
|
||||||
|
var opt = {
|
||||||
|
host: parsed.host,
|
||||||
|
port: 443,
|
||||||
|
headers: {
|
||||||
|
host: parsed.hostname,
|
||||||
|
'Transfer-Encoding': 'chunked'
|
||||||
|
},
|
||||||
|
path: '/'
|
||||||
|
};
|
||||||
|
|
||||||
|
var req = https.request(opt, function(res) {
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
var body = '';
|
||||||
|
|
||||||
|
res.on('data', function(chunk) {
|
||||||
|
body += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', function() {
|
||||||
|
assert.equal(body, '127.0.0.1');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.end(require('crypto').randomBytes(1024 * 8).toString('base64'));
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user