소스 검색

detect https: fixes #57

Michelle Bu 12 년 전
부모
커밋
8d57e8aa48
6개의 변경된 파일50개의 추가작업 그리고 15개의 파일을 삭제
  1. 27 10
      dist/peer.js
  2. 0 0
      dist/peer.min.js
  3. 1 0
      docs/api.md
  4. 14 3
      lib/peer.js
  5. 5 2
      lib/socket.js
  6. 3 0
      lib/util.js

+ 27 - 10
dist/peer.js

@@ -836,6 +836,9 @@ var util = {
       }
     }
     return false;
+  },
+  isSecure: function() {
+    return location.protocol === 'https:';
   }
 };
 /**
@@ -1162,7 +1165,7 @@ function Peer(id, options) {
   if (!(this instanceof Peer)) return new Peer(id, options);
   EventEmitter.call(this);
 
-  
+
   options = util.extend({
     debug: false,
     host: '0.peerjs.com',
@@ -1188,7 +1191,7 @@ function Peer(id, options) {
   if (options.host === '/') {
     options.host = window.location.hostname;
   }
-  
+
   // Ensure alphanumeric_-
   if (id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(id)) {
     util.setZeroTimeout(function() {
@@ -1203,6 +1206,16 @@ function Peer(id, options) {
     return;
   }
 
+  this._secure = util.isSecure();
+  // Errors for now because no support for SSL on cloud server.
+  if (this._secure && options.host === '0.peerjs.com') {
+    util.setZeroTimeout(function() {
+      self._abort('ssl-unavailable',
+        'The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS.');
+    });
+    return;
+  }
+
   // States.
   this.destroyed = false;
   this.disconnected = false;
@@ -1227,11 +1240,12 @@ function Peer(id, options) {
 
 util.inherits(Peer, EventEmitter);
 
-Peer.prototype._retrieveId = function(cb) {  
+Peer.prototype._retrieveId = function(cb) {
   var self = this;
   try {
     var http = new XMLHttpRequest();
-    var url = 'http://' + this._options.host + ':' + this._options.port + '/' + this._options.key + '/id';
+    var protocol = this._secure ? 'https://' : 'http://';
+    var url = protocol + this._options.host + ':' + this._options.port + '/' + this._options.key + '/id';
     var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
     url += queryString;
     // If there's no ID we need to wait for one before trying to init socket.
@@ -1990,14 +2004,17 @@ ConnectionManager.prototype.update = function(updates) {
 function Socket(host, port, key, id) {
   if (!(this instanceof Socket)) return new Socket(host, port, key, id);
   EventEmitter.call(this);
-  
+
   this._id = id;
   var token = util.randomToken();
 
   this.disconnected = false;
-  
-  this._httpUrl = 'http://' + host + ':' + port + '/' + key + '/' + id + '/' + token;
-  this._wsUrl = 'ws://' + host + ':' + port + '/peerjs?key='+key+'&id='+id+'&token='+token;
+
+  var secure = util.isSecure();
+  var protocol = secure ? 'https://' : 'http://';
+  var wsProtocol = secure ? 'wss://' : 'ws://';
+  this._httpUrl = protocol + host + ':' + port + '/' + key + '/' + id + '/' + token;
+  this._wsUrl = wsProtocol + host + ':' + port + '/peerjs?key='+key+'&id='+id+'&token='+token;
 };
 
 util.inherits(Socket, EventEmitter);
@@ -2019,7 +2036,7 @@ Socket.prototype._startWebSocket = function() {
   }
 
   this._socket = new WebSocket(this._wsUrl);
-  
+
   this._socket.onmessage = function(event) {
     var data;
     try {
@@ -2141,7 +2158,7 @@ Socket.prototype.send = function(data) {
     this.emit('error', 'Invalid message');
     return;
   }
-  
+
   var message = JSON.stringify(data);
   if (this._wsOpen()) {
     this._socket.send(message);

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
dist/peer.min.js


+ 1 - 0
docs/api.md

@@ -142,6 +142,7 @@ The `error` object also has a `type` parameter that may be helpful in responding
 * `invalid-key`: The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).
 * `unavailable-id`: The ID passed into the Peer constructor is already taken.
 * `firefoxism`: The operation you're trying to perform is not supported in firefox.
+* `ssl-unavailable`: PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.
 * Errors types that shouldn't regularly appear:
   * `server-error`: Unable to reach the server.
   * `socket-error`: An error from the underlying socket.

+ 14 - 3
lib/peer.js

@@ -50,6 +50,16 @@ function Peer(id, options) {
     return;
   }
 
+  this._secure = util.isSecure();
+  // Errors for now because no support for SSL on cloud server.
+  if (this._secure && options.host === '0.peerjs.com') {
+    util.setZeroTimeout(function() {
+      self._abort('ssl-unavailable',
+        'The cloud server currently does not support HTTPS. Please run your own PeerServer to use HTTPS.');
+    });
+    return;
+  }
+
   // States.
   this.destroyed = false;
   this.disconnected = false;
@@ -74,11 +84,12 @@ function Peer(id, options) {
 
 util.inherits(Peer, EventEmitter);
 
-Peer.prototype._retrieveId = function(cb) {  
+Peer.prototype._retrieveId = function(cb) {
   var self = this;
   try {
     var http = new XMLHttpRequest();
-    var url = 'http://' + this._options.host + ':' + this._options.port + '/' + this._options.key + '/id';
+    var protocol = this._secure ? 'https://' : 'http://';
+    var url = protocol + this._options.host + ':' + this._options.port + '/' + this._options.key + '/id';
     var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
     url += queryString;
     // If there's no ID we need to wait for one before trying to init socket.
@@ -86,7 +97,7 @@ Peer.prototype._retrieveId = function(cb) {
     http.onreadystatechange = function() {
       if (http.readyState === 4) {
         if (http.status !== 200) {
-          throw 'Retrieve id response not 200';
+          throw 'Retrieve ID response not 200';
           return;
         }
         self.id = http.responseText;

+ 5 - 2
lib/socket.js

@@ -11,8 +11,11 @@ function Socket(host, port, key, id) {
 
   this.disconnected = false;
 
-  this._httpUrl = 'http://' + host + ':' + port + '/' + key + '/' + id + '/' + token;
-  this._wsUrl = 'ws://' + host + ':' + port + '/peerjs?key='+key+'&id='+id+'&token='+token;
+  var secure = util.isSecure();
+  var protocol = secure ? 'https://' : 'http://';
+  var wsProtocol = secure ? 'wss://' : 'ws://';
+  this._httpUrl = protocol + host + ':' + port + '/' + key + '/' + id + '/' + token;
+  this._wsUrl = wsProtocol + host + ':' + port + '/peerjs?key='+key+'&id='+id+'&token='+token;
 };
 
 util.inherits(Socket, EventEmitter);

+ 3 - 0
lib/util.js

@@ -116,5 +116,8 @@ var util = {
       }
     }
     return false;
+  },
+  isSecure: function() {
+    return location.protocol === 'https:';
   }
 };

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.