mirror of
https://github.com/bitinflow/localtunnel.git
synced 2026-03-13 13:35:54 +00:00
HTTPS support, Promise API, modern ES syntax
* Add support for tunneling a local HTTPS server. * Return a Promise from localtunnel.
This commit is contained in:
committed by
Roman Shtylman
parent
d7330a7121
commit
2a74d6be9f
235
lib/Tunnel.js
235
lib/Tunnel.js
@@ -1,158 +1,163 @@
|
||||
var url = require('url');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var axios = require('axios');
|
||||
var debug = require('debug')('localtunnel:client');
|
||||
/* eslint-disable consistent-return, no-underscore-dangle */
|
||||
|
||||
var TunnelCluster = require('./TunnelCluster');
|
||||
const { parse } = require('url');
|
||||
const { EventEmitter } = require('events');
|
||||
const axios = require('axios');
|
||||
const debug = require('debug')('localtunnel:client');
|
||||
|
||||
var Tunnel = function(opt) {
|
||||
if (!(this instanceof Tunnel)) {
|
||||
return new Tunnel(opt);
|
||||
const TunnelCluster = require('./TunnelCluster');
|
||||
|
||||
module.exports = class Tunnel extends EventEmitter {
|
||||
constructor(opts = {}) {
|
||||
super(opts);
|
||||
this.opts = opts;
|
||||
this.closed = false;
|
||||
if (!this.opts.host) {
|
||||
this.opts.host = 'https://localtunnel.me';
|
||||
}
|
||||
}
|
||||
|
||||
var self = this;
|
||||
self._closed = false;
|
||||
self._opt = opt || {};
|
||||
_getInfo(body) {
|
||||
/* eslint-disable camelcase */
|
||||
const { id, ip, port, url, cached_url, max_conn_count } = body;
|
||||
const { host, port: local_port, local_host } = this.opts;
|
||||
const { local_https, local_cert, local_key, local_ca, allow_invalid_cert } = this.opts;
|
||||
return {
|
||||
name: id,
|
||||
url,
|
||||
cached_url,
|
||||
max_conn: max_conn_count || 1,
|
||||
remote_host: parse(host).hostname,
|
||||
remote_ip: ip,
|
||||
remote_port: port,
|
||||
local_port,
|
||||
local_host,
|
||||
local_https,
|
||||
local_cert,
|
||||
local_key,
|
||||
local_ca,
|
||||
allow_invalid_cert,
|
||||
};
|
||||
/* eslint-enable camelcase */
|
||||
}
|
||||
|
||||
self._opt.host = self._opt.host || 'https://localtunnel.me';
|
||||
};
|
||||
// initialize connection
|
||||
// callback with connection info
|
||||
_init(cb) {
|
||||
const opt = this.opts;
|
||||
const getInfo = this._getInfo.bind(this);
|
||||
|
||||
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 = {
|
||||
responseType: 'json'
|
||||
const params = {
|
||||
responseType: 'json',
|
||||
};
|
||||
|
||||
var base_uri = opt.host + '/';
|
||||
|
||||
// optionally override the upstream server
|
||||
var upstream = url.parse(opt.host);
|
||||
|
||||
const baseUri = `${opt.host}/`;
|
||||
// no subdomain at first, maybe use requested domain
|
||||
var assigned_domain = opt.subdomain;
|
||||
|
||||
const assignedDomain = opt.subdomain;
|
||||
// where to quest
|
||||
var uri = base_uri + ((assigned_domain) ? assigned_domain : '?new');
|
||||
const uri = baseUri + (assignedDomain || '?new');
|
||||
|
||||
(function get_url() {
|
||||
axios.get(uri, params)
|
||||
.then(function(res){
|
||||
var body = res.data;
|
||||
if (res.status !== 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
|
||||
});
|
||||
})
|
||||
.catch(function(err){
|
||||
// TODO (shtylman) don't print to stdout?
|
||||
console.log('tunnel server offline: ' + err.message + ', retry 1s');
|
||||
return setTimeout(get_url, 1000);
|
||||
(function getUrl() {
|
||||
axios
|
||||
.get(uri, params)
|
||||
.then(res => {
|
||||
const body = res.data;
|
||||
debug('got tunnel information', res.data);
|
||||
if (res.status !== 200) {
|
||||
const err = new Error(
|
||||
(body && body.message) || 'localtunnel server returned an error, please try again'
|
||||
);
|
||||
return cb(err);
|
||||
}
|
||||
cb(null, getInfo(body));
|
||||
})
|
||||
.catch(err => {
|
||||
debug(`tunnel server offline: ${err.message}, retry 1s`);
|
||||
return setTimeout(getUrl, 1000);
|
||||
});
|
||||
})();
|
||||
};
|
||||
|
||||
Tunnel.prototype._establish = function(info) {
|
||||
var self = this;
|
||||
var opt = self._opt;
|
||||
}
|
||||
|
||||
_establish(info) {
|
||||
// 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));
|
||||
this.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);
|
||||
this.tunnelCluster = new TunnelCluster(info);
|
||||
|
||||
// only emit the url the first time
|
||||
tunnels.once('open', function() {
|
||||
self.emit('url', info.url);
|
||||
this.tunnelCluster.once('open', () => {
|
||||
this.emit('url', info.url);
|
||||
});
|
||||
|
||||
// re-emit socket error
|
||||
tunnels.on('error', function(err) {
|
||||
self.emit('error', err);
|
||||
this.tunnelCluster.on('error', err => {
|
||||
debug('got socket error', err.message);
|
||||
this.emit('error', err);
|
||||
});
|
||||
|
||||
var tunnel_count = 0;
|
||||
let tunnelCount = 0;
|
||||
|
||||
// track open count
|
||||
tunnels.on('open', function(tunnel) {
|
||||
tunnel_count++;
|
||||
debug('tunnel open [total: %d]', tunnel_count);
|
||||
this.tunnelCluster.on('open', tunnel => {
|
||||
tunnelCount++;
|
||||
debug('tunnel open [total: %d]', tunnelCount);
|
||||
|
||||
var close_handler = function() {
|
||||
tunnel.destroy();
|
||||
};
|
||||
const closeHandler = () => {
|
||||
tunnel.destroy();
|
||||
};
|
||||
|
||||
if (self._closed) {
|
||||
return close_handler();
|
||||
}
|
||||
if (this.closed) {
|
||||
return closeHandler();
|
||||
}
|
||||
|
||||
self.once('close', close_handler);
|
||||
tunnel.once('close', function() {
|
||||
self.removeListener('close', close_handler);
|
||||
});
|
||||
this.once('close', closeHandler);
|
||||
tunnel.once('close', () => {
|
||||
this.removeListener('close', closeHandler);
|
||||
});
|
||||
});
|
||||
|
||||
// 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();
|
||||
this.tunnelCluster.on('dead', () => {
|
||||
tunnelCount--;
|
||||
debug('tunnel dead [total: %d]', tunnelCount);
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.tunnelCluster.open();
|
||||
});
|
||||
|
||||
tunnels.on('request', function(info) {
|
||||
self.emit('request', info);
|
||||
this.tunnelCluster.on('request', req => {
|
||||
this.emit('request', req);
|
||||
});
|
||||
|
||||
// establish as many tunnels as allowed
|
||||
for (var count = 0 ; count < info.max_conn ; ++count) {
|
||||
tunnels.open();
|
||||
for (let count = 0; count < info.max_conn; ++count) {
|
||||
this.tunnelCluster.open();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Tunnel.prototype.open = function(cb) {
|
||||
var self = this;
|
||||
open(cb) {
|
||||
this._init((err, info) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
self._init(function(err, info) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
this.clientId = info.name;
|
||||
this.url = info.url;
|
||||
|
||||
self.url = info.url;
|
||||
self._establish(info);
|
||||
cb();
|
||||
// `cached_url` is only returned by proxy servers that support resource caching.
|
||||
if (info.cached_url) {
|
||||
this.cachedUrl = info.cached_url;
|
||||
}
|
||||
|
||||
this._establish(info);
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
close() {
|
||||
this.closed = true;
|
||||
this.emit('close');
|
||||
}
|
||||
};
|
||||
|
||||
// shutdown tunnels
|
||||
Tunnel.prototype.close = function() {
|
||||
var self = this;
|
||||
|
||||
self._closed = true;
|
||||
self.emit('close');
|
||||
};
|
||||
|
||||
module.exports = Tunnel;
|
||||
|
||||
Reference in New Issue
Block a user