lt.js 2.7 KB

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