client 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env node
  2. var lt_client = require('../client');
  3. var open_url = require('openurl');
  4. var argv = require('yargs')
  5. .usage('Usage: $0 --port [num] <options>')
  6. .env(true)
  7. .option('h', {
  8. alias: 'host',
  9. describe: 'Upstream server providing forwarding',
  10. default: 'https://localtunnel.me',
  11. })
  12. .option('s', {
  13. alias: 'subdomain',
  14. describe: 'Request this subdomain'
  15. })
  16. .option('l', {
  17. alias: 'local-host',
  18. describe: 'Tunnel traffic to this host instead of localhost, override Host header to this host'
  19. })
  20. .options('o', {
  21. alias: 'open',
  22. describe: 'opens url in your browser'
  23. })
  24. .option('p', {
  25. alias: 'port',
  26. describe: 'Internal http server port',
  27. })
  28. .option('print-requests', {
  29. describe: 'Print basic request info',
  30. })
  31. .require('port')
  32. .boolean('print-requests')
  33. .help('help', 'Show this help and exit')
  34. .version(require('../package').version)
  35. .argv;
  36. if (typeof argv.port !== 'number') {
  37. require('yargs').showHelp();
  38. console.error('port must be a number');
  39. process.exit(1);
  40. }
  41. var opt = {
  42. host: argv.host,
  43. port: argv.port,
  44. local_host: argv['local-host'],
  45. subdomain: argv.subdomain,
  46. };
  47. const PrintRequests = argv['print-requests'];
  48. lt_client(opt.port, opt, function(err, tunnel) {
  49. if (err) {
  50. throw err;
  51. }
  52. console.log('your url is: %s', tunnel.url);
  53. if (argv.open) {
  54. open_url.open(tunnel.url);
  55. }
  56. tunnel.on('error', function(err) {
  57. throw err;
  58. });
  59. if (PrintRequests) {
  60. tunnel.on('request', function(info) {
  61. console.log(new Date().toString(), info.method, info.path);
  62. });
  63. }
  64. });
  65. // vim: ft=javascript