lt.js 3.7 KB

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