peerjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env node
  2. var path = require('path')
  3. , pkg = require('../package.json')
  4. , fs = require('fs')
  5. , version = pkg.version
  6. , PeerServer = require('../lib').PeerServer
  7. , util = require('../lib/util')
  8. , opts = require('optimist')
  9. .usage('Usage: $0')
  10. .options({
  11. debug: {
  12. demand: false,
  13. alias: 'd',
  14. description: 'debug',
  15. default: false
  16. },
  17. timeout: {
  18. demand: false,
  19. alias: 't',
  20. description: 'timeout (milliseconds)',
  21. default: 5000
  22. },
  23. ip_limit: {
  24. demand: false,
  25. alias: 'i',
  26. description: 'IP limit',
  27. default: 5000
  28. },
  29. concurrent_limit: {
  30. demand: false,
  31. alias: 'c',
  32. description: 'concurrent limit',
  33. default: 5000
  34. },
  35. key: {
  36. demand: false,
  37. alias: 'k',
  38. description: 'connection key',
  39. default: 'peerjs'
  40. },
  41. sslkey: {
  42. demand: false,
  43. description: 'path to SSL key'
  44. },
  45. sslcert: {
  46. demand: false,
  47. description: 'path to SSL certificate'
  48. },
  49. port: {
  50. demand: true,
  51. alias: 'p',
  52. description: 'port'
  53. },
  54. path: {
  55. demand: false,
  56. description: 'custom path',
  57. default: '/'
  58. },
  59. allow_discovery: {
  60. demand: false,
  61. description: 'allow discovery of peers'
  62. }
  63. })
  64. .boolean('allow_discovery')
  65. .argv;
  66. process.on('uncaughtException', function(e) {
  67. console.error('Error: ' + e);
  68. });
  69. if (opts.sslkey || opts.sslcert) {
  70. if (opts.sslkey && opts.sslcert) {
  71. opts.ssl = {
  72. key: fs.readFileSync(path.resolve(opts.sslkey)),
  73. cert: fs.readFileSync(path.resolve(opts.sslcert))
  74. }
  75. delete opts.sslkey;
  76. delete opts.sslcert;
  77. } else {
  78. util.prettyError('Warning: PeerServer will not run because either ' +
  79. 'the key or the certificate has not been provided.');
  80. process.exit(1);
  81. }
  82. }
  83. var userPath = opts.path;
  84. var server = PeerServer(opts, function(server) {
  85. var host = server.address().address;
  86. var port = server.address().port;
  87. console.log(
  88. 'Started PeerServer on %s, port: %s, path: %s (v. %s)',
  89. host, port, userPath || '/', version
  90. );
  91. });