UNPKG

2.79 kBJavaScriptView Raw
1#!/usr/bin/env node
2/* eslint-disable no-console */
3
4const openurl = require('openurl');
5const yargs = require('yargs');
6
7const localtunnel = require('../localtunnel');
8const { version } = require('../package');
9
10const { argv } = yargs
11 .usage('Usage: lt --port [num] <options>')
12 .env(true)
13 .option('p', {
14 alias: 'port',
15 describe: 'Internal HTTP server port',
16 })
17 .option('h', {
18 alias: 'host',
19 describe: 'Upstream server providing forwarding',
20 default: 'https://localtunnel.me',
21 })
22 .option('s', {
23 alias: 'subdomain',
24 describe: 'Request this subdomain',
25 })
26 .option('l', {
27 alias: 'local-host',
28 describe: 'Tunnel traffic to this host instead of localhost, override Host header to this host',
29 })
30 .option('local-https', {
31 describe: 'Tunnel traffic to a local HTTPS server',
32 })
33 .option('local-cert', {
34 describe: 'Path to certificate PEM file for local HTTPS server',
35 })
36 .option('local-key', {
37 describe: 'Path to certificate key file for local HTTPS server',
38 })
39 .option('local-ca', {
40 describe: 'Path to certificate authority file for self-signed certificates',
41 })
42 .option('allow-invalid-cert', {
43 describe: 'Disable certificate checks for your local HTTPS server (ignore cert/key/ca options)',
44 })
45 .options('o', {
46 alias: 'open',
47 describe: 'Opens the tunnel URL in your browser',
48 })
49 .option('print-requests', {
50 describe: 'Print basic request info',
51 })
52 .require('port')
53 .boolean('local-https')
54 .boolean('allow-invalid-cert')
55 .boolean('print-requests')
56 .help('help', 'Show this help and exit')
57 .version(version);
58
59if (typeof argv.port !== 'number') {
60 yargs.showHelp();
61 console.error('\nInvalid argument: `port` must be a number');
62 process.exit(1);
63}
64
65(async () => {
66 const tunnel = await localtunnel({
67 port: argv.port,
68 host: argv.host,
69 subdomain: argv.subdomain,
70 local_host: argv.localHost,
71 local_https: argv.localHttps,
72 local_cert: argv.localCert,
73 local_key: argv.localKey,
74 local_ca: argv.localCa,
75 allow_invalid_cert: argv.allowInvalidCert,
76 }).catch(err => {
77 throw err;
78 });
79
80 tunnel.on('error', err => {
81 throw err;
82 });
83
84 console.log('your url is: %s', tunnel.url);
85
86 /**
87 * `cachedUrl` is set when using a proxy server that support resource caching.
88 * This URL generally remains available after the tunnel itself has closed.
89 * @see https://github.com/localtunnel/localtunnel/pull/319#discussion_r319846289
90 */
91 if (tunnel.cachedUrl) {
92 console.log('your cachedUrl is: %s', tunnel.cachedUrl);
93 }
94
95 if (argv.open) {
96 openurl.open(tunnel.url);
97 }
98
99 if (argv['print-requests']) {
100 tunnel.on('request', info => {
101 console.log(new Date().toString(), info.method, info.path);
102 });
103 }
104})();