소스 검색

ssl options, closes #13

Michelle Bu 12 년 전
부모
커밋
70ee781154
2개의 변경된 파일35개의 추가작업 그리고 13개의 파일을 삭제
  1. 20 7
      README.md
  2. 15 6
      lib/server.js

+ 20 - 7
README.md

@@ -10,22 +10,35 @@ PeerServer helps broker connections between PeerJS clients. Data is not proxied
 
 ### Run PeerServer
 
-Install the library
+Install the library:
 
-    npm install peer     
+    npm install peer
 
-Run the server
+Run the server:
 
     var PeerServer = require('peer').PeerServer;
     var server = new PeerServer({ port: 9000 });
-    
-Connecting to the server from PeerJS
-    
+
+Connecting to the server from PeerJS:
+
     <script>
-        // No API key requried when not using cloud server
+        // No API key required when not using cloud server
         var peer = new Peer('someid', {host: 'localhost', port: 9000});
     </script>
 
+Using HTTPS: Simply pass in PEM-encoded certificate and key.
+
+    var fs = require('fs');
+    var PeerServer = require('peer').PeerServer;
+
+    var server = new PeerServer({
+      port: 9000,
+      ssl: {
+        key: fs.readFileSync('/path/to/your/ssl/key/here.key'),
+        certificate: fs.readFileSync('/path/to/your/ssl/certificate/here.crt')
+      }
+    });
+
 ## Problems?
 
 Discuss PeerJS on our Google Group:

+ 15 - 6
lib/server.js

@@ -9,20 +9,29 @@ function PeerServer(options) {
   if (!(this instanceof PeerServer)) return new PeerServer(options);
   EventEmitter.call(this);
 
-  this._app = restify.createServer();
-  this._httpServer = this._app;
-
   this._options = util.extend({
     port: 80,
     debug: false,
     timeout: 5000,
     key: 'peerjs',
     ip_limit: 5000,
-    concurrent_limit: 5000
+    concurrent_limit: 5000,
+    ssl: {}
   }, options);
 
   util.debug = this._options.debug;
 
+  // Set up HTTPS server if key and certificate are provided.
+  var secure = this._options.ssl.key && this._options.ssl.certificate;
+  // Print warning if only one of the two is given.
+  if (Object.keys(this._options.ssl).length === 1) {
+    util.prettyError('Warning: PeerServer will not run on an HTTPS server'
+        + ' because either the key or the certificate has not been provided.');
+  }
+
+  this._options.ssl['name'] = 'PeerServer';
+  this._app = restify.createServer(this._options.ssl);
+
   // Connected clients
   this._clients = {};
 
@@ -50,7 +59,7 @@ PeerServer.prototype._initializeWSS = function() {
   var self = this;
 
   // Create WebSocket server as well.
-  this._wss = new WebSocketServer({ path: '/peerjs', server: this._httpServer });
+  this._wss = new WebSocketServer({ path: '/peerjs', server: this._app});
 
   this._wss.on('connection', function(socket) {
     var query = url.parse(socket.upgradeReq.url, true).query;
@@ -253,7 +262,7 @@ PeerServer.prototype._initializeHTTP = function() {
   this._app.post('/:key/:id/:token/leave', handle);
 
   // Listen on user-specified port.
-  this._httpServer.listen(this._options.port);
+  this._app.listen(this._options.port);
 };
 
 /** Saves a streaming response and takes care of timeouts and headers. */