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:
Gert Hengeveld
2019-09-16 16:30:13 +02:00
committed by Roman Shtylman
parent d7330a7121
commit 2a74d6be9f
16 changed files with 719 additions and 663 deletions

View File

@@ -1,75 +0,0 @@
#!/usr/bin/env node
var lt_client = require('../client');
var open_url = require('openurl');
var argv = require('yargs')
.usage('Usage: $0 --port [num] <options>')
.env(true)
.option('h', {
alias: 'host',
describe: 'Upstream server providing forwarding',
default: 'https://localtunnel.me',
})
.option('s', {
alias: 'subdomain',
describe: 'Request this subdomain'
})
.option('l', {
alias: 'local-host',
describe: 'Tunnel traffic to this host instead of localhost, override Host header to this host'
})
.options('o', {
alias: 'open',
describe: 'opens url in your browser'
})
.option('p', {
alias: 'port',
describe: 'Internal http server port',
})
.option('print-requests', {
describe: 'Print basic request info',
})
.require('port')
.boolean('print-requests')
.help('help', 'Show this help and exit')
.version(require('../package').version)
.argv;
if (typeof argv.port !== 'number') {
require('yargs').showHelp();
console.error('port must be a number');
process.exit(1);
}
var opt = {
host: argv.host,
port: argv.port,
local_host: argv['local-host'],
subdomain: argv.subdomain,
};
const PrintRequests = argv['print-requests'];
lt_client(opt.port, opt, function(err, tunnel) {
if (err) {
throw err;
}
console.log('your url is: %s', tunnel.url);
if (argv.open) {
open_url.open(tunnel.url);
}
tunnel.on('error', function(err) {
throw err;
});
if (PrintRequests) {
tunnel.on('request', function(info) {
console.log(new Date().toString(), info.method, info.path);
});
}
});
// vim: ft=javascript

104
bin/lt.js Executable file
View File

@@ -0,0 +1,104 @@
#!/usr/bin/env node
/* eslint-disable no-console */
const openurl = require('openurl');
const yargs = require('yargs');
const localtunnel = require('../localtunnel');
const { version } = require('../package');
const { argv } = yargs
.usage('Usage: lt --port [num] <options>')
.env(true)
.option('p', {
alias: 'port',
describe: 'Internal HTTP server port',
})
.option('h', {
alias: 'host',
describe: 'Upstream server providing forwarding',
default: 'https://localtunnel.me',
})
.option('s', {
alias: 'subdomain',
describe: 'Request this subdomain',
})
.option('l', {
alias: 'local-host',
describe: 'Tunnel traffic to this host instead of localhost, override Host header to this host',
})
.option('local-https', {
describe: 'Tunnel traffic to a local HTTPS server',
})
.option('local-cert', {
describe: 'Path to certificate PEM file for local HTTPS server',
})
.option('local-key', {
describe: 'Path to certificate key file for local HTTPS server',
})
.option('local-ca', {
describe: 'Path to certificate authority file for self-signed certificates',
})
.option('allow-invalid-cert', {
describe: 'Disable certificate checks for your local HTTPS server (ignore cert/key/ca options)',
})
.options('o', {
alias: 'open',
describe: 'Opens the tunnel URL in your browser',
})
.option('print-requests', {
describe: 'Print basic request info',
})
.require('port')
.boolean('local-https')
.boolean('allow-invalid-cert')
.boolean('print-requests')
.help('help', 'Show this help and exit')
.version(version);
if (typeof argv.port !== 'number') {
yargs.showHelp();
console.error('\nInvalid argument: `port` must be a number');
process.exit(1);
}
(async () => {
const tunnel = await localtunnel({
port: argv.port,
host: argv.host,
subdomain: argv.subdomain,
local_host: argv.localHost,
local_https: argv.localHttps,
local_cert: argv.localCert,
local_key: argv.localKey,
local_ca: argv.localCa,
allow_invalid_cert: argv.allowInvalidCert,
}).catch(err => {
throw err;
});
tunnel.on('error', err => {
throw err;
});
console.log('your url is: %s', tunnel.url);
/**
* `cachedUrl` is set when using a proxy server that support resource caching.
* This URL generally remains available after the tunnel itself has closed.
* @see https://github.com/localtunnel/localtunnel/pull/319#discussion_r319846289
*/
if (tunnel.cachedUrl) {
console.log('your cachedUrl is: %s', tunnel.cachedUrl);
}
if (argv.open) {
openurl.open(tunnel.url);
}
if (argv['print-requests']) {
tunnel.on('request', info => {
console.log(new Date().toString(), info.method, info.path);
});
}
})();