Browse Source

Merge pull request #24 from peers/yosssi-master

Server fixes
Michelle Bu 11 years ago
parent
commit
80dc77140d
2 changed files with 17 additions and 32 deletions
  1. 16 31
      lib/server.js
  2. 1 1
      lib/util.js

+ 16 - 31
lib/server.js

@@ -21,8 +21,6 @@ function PeerServer(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'
@@ -49,7 +47,7 @@ function PeerServer(options) {
   this._ips = {};
 
   this._setCleanupIntervals();
-};
+}
 
 util.inherits(PeerServer, EventEmitter);
 
@@ -126,35 +124,22 @@ PeerServer.prototype._configureWS = function(socket, key, id, token) {
     try {
       var message = JSON.parse(data);
 
-      switch (message.type) {
-        case 'LEAVE':
-          // Clean up if a Peer sends a LEAVE.
-          if (!message.dst) {
-            self._removePeer(key, id);
-            break;
-          }
-        // ICE candidates
-        case 'CANDIDATE':
-        // Offer or answer between peers.
-        case 'OFFER':
-        case 'ANSWER':
-          // Use the ID we know to be correct to prevent spoofing.
-          self._handleTransmission(key, {
-            type: message.type,
-            src: id,
-            dst: message.dst,
-            payload: message.payload
-          });
-          break;
-        default:
-          util.prettyError('Message unrecognized');
+      if (['LEAVE', 'CANDIDATE', 'OFFER', 'ANSWER'].indexOf(message.type) !== -1) {
+        self._handleTransmission(key, {
+          type: message.type,
+          src: id,
+          dst: message.dst,
+          payload: message.payload
+        });
+      } else {
+        util.prettyError('Message unrecognized');
       }
     } catch(e) {
-      throw e;
       util.log('Invalid message', data);
+      throw e;
     }
   });
-}
+};
 
 
 PeerServer.prototype._checkKey = function(key, ip, cb) {
@@ -181,14 +166,14 @@ PeerServer.prototype._checkKey = function(key, ip, cb) {
   } else {
     cb('Invalid key provided');
   }
-}
+};
 
 /** Initialize HTTP server routes. */
 PeerServer.prototype._initializeHTTP = function() {
   var self = this;
 
   this._app.use(restify.bodyParser({ mapParams: false }));
-  this._app.use(restify.queryParser())
+  this._app.use(restify.queryParser());
   this._app.use(util.allowCrossDomain);
 
   // Retrieve guaranteed random ID.
@@ -332,7 +317,7 @@ PeerServer.prototype._setCleanupIntervals = function() {
     var keys = Object.keys(self._ips);
     for (var i = 0, ii = keys.length; i < ii; i += 1) {
       var key = keys[i];
-      if (self._ips[key] == 0) {
+      if (self._ips[key] === 0) {
         delete self._ips[key];
       }
     }
@@ -383,7 +368,7 @@ PeerServer.prototype._handleTransmission = function(key, message) {
         destination.res.write(data);
       } else {
         // Neither socket no res available. Peer dead?
-        throw "Peer dead"
+        throw "Peer dead";
       }
     } catch (e) {
       // This happens when a peer disconnects without closing connections and

+ 1 - 1
lib/util.js

@@ -13,7 +13,7 @@ var util = {
     });
   },
   extend: function(dest, source) {
-    source = source || {}
+    source = source || {};
     for(var key in source) {
       if(source.hasOwnProperty(key)) {
         dest[key] = source[key];