snowflake.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* global log, dbg, DummyRateLimit, BucketRateLimit, SessionDescription, ProxyPair */
  2. /*
  3. A JavaScript WebRTC snowflake proxy
  4. Uses WebRTC from the client, and Websocket to the server.
  5. Assume that the webrtc client plugin is always the offerer, in which case
  6. this proxy must always act as the answerer.
  7. TODO: More documentation
  8. */
  9. // Minimum viable snowflake for now - just 1 client.
  10. class Snowflake {
  11. // Prepare the Snowflake with a Broker (to find clients) and optional UI.
  12. constructor(config, ui, broker) {
  13. this.receiveOffer = this.receiveOffer.bind(this);
  14. this.config = config;
  15. this.ui = ui;
  16. this.broker = broker;
  17. this.proxyPairs = [];
  18. if (void 0 === this.config.rateLimitBytes) {
  19. this.rateLimit = new DummyRateLimit();
  20. } else {
  21. this.rateLimit = new BucketRateLimit(this.config.rateLimitBytes * this.config.rateLimitHistory, this.config.rateLimitHistory);
  22. }
  23. this.retries = 0;
  24. }
  25. // Set the target relay address spec, which is expected to be websocket.
  26. // TODO: Should potentially fetch the target from broker later, or modify
  27. // entirely for the Tor-independent version.
  28. setRelayAddr(relayAddr) {
  29. this.relayAddr = relayAddr;
  30. log('Using ' + relayAddr.host + ':' + relayAddr.port + ' as Relay.');
  31. return true;
  32. }
  33. // Initialize WebRTC PeerConnection, which requires beginning the signalling
  34. // process. |pollBroker| automatically arranges signalling.
  35. beginWebRTC() {
  36. this.pollBroker();
  37. return this.pollInterval = setInterval((() => {
  38. return this.pollBroker();
  39. }), this.config.defaultBrokerPollInterval);
  40. }
  41. // Regularly poll Broker for clients to serve until this snowflake is
  42. // serving at capacity, at which point stop polling.
  43. pollBroker() {
  44. var msg, pair, recv;
  45. // Poll broker for clients.
  46. pair = this.makeProxyPair();
  47. if (!pair) {
  48. log('At client capacity.');
  49. return;
  50. }
  51. log('Polling broker..');
  52. // Do nothing until a new proxyPair is available.
  53. msg = 'Polling for client ... ';
  54. if (this.retries > 0) {
  55. msg += '[retries: ' + this.retries + ']';
  56. }
  57. this.ui.setStatus(msg);
  58. recv = this.broker.getClientOffer(pair.id);
  59. recv.then((desc) => {
  60. if (!this.receiveOffer(pair, desc)) {
  61. return pair.close();
  62. }
  63. //set a timeout for channel creation
  64. return setTimeout((() => {
  65. if (!pair.webrtcIsReady()) {
  66. log('proxypair datachannel timed out waiting for open');
  67. return pair.close();
  68. }
  69. }), 20000); // 20 second timeout
  70. }, function() {
  71. //on error, close proxy pair
  72. return pair.close();
  73. });
  74. return this.retries++;
  75. }
  76. // Receive an SDP offer from some client assigned by the Broker,
  77. // |pair| - an available ProxyPair.
  78. receiveOffer(pair, desc) {
  79. var e, offer, sdp;
  80. try {
  81. offer = JSON.parse(desc);
  82. dbg('Received:\n\n' + offer.sdp + '\n');
  83. sdp = new SessionDescription(offer);
  84. if (pair.receiveWebRTCOffer(sdp)) {
  85. this.sendAnswer(pair);
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. } catch (error) {
  91. e = error;
  92. log('ERROR: Unable to receive Offer: ' + e);
  93. return false;
  94. }
  95. }
  96. sendAnswer(pair) {
  97. var fail, next;
  98. next = function(sdp) {
  99. dbg('webrtc: Answer ready.');
  100. return pair.pc.setLocalDescription(sdp).catch(fail);
  101. };
  102. fail = function() {
  103. pair.close();
  104. return dbg('webrtc: Failed to create or set Answer');
  105. };
  106. return pair.pc.createAnswer().then(next).catch(fail);
  107. }
  108. makeProxyPair() {
  109. if (this.proxyPairs.length >= this.config.maxNumClients) {
  110. return null;
  111. }
  112. var pair;
  113. pair = new ProxyPair(this.relayAddr, this.rateLimit, this.config.pcConfig);
  114. this.proxyPairs.push(pair);
  115. log('Snowflake IDs: ' + (this.proxyPairs.map(function(p) {
  116. return p.id;
  117. })).join(' | '));
  118. pair.onCleanup = () => {
  119. var ind;
  120. // Delete from the list of proxy pairs.
  121. ind = this.proxyPairs.indexOf(pair);
  122. if (ind > -1) {
  123. return this.proxyPairs.splice(ind, 1);
  124. }
  125. };
  126. pair.begin();
  127. return pair;
  128. }
  129. // Stop all proxypairs.
  130. disable() {
  131. var results;
  132. log('Disabling Snowflake.');
  133. clearInterval(this.pollInterval);
  134. results = [];
  135. while (this.proxyPairs.length > 0) {
  136. results.push(this.proxyPairs.pop().close());
  137. }
  138. return results;
  139. }
  140. }
  141. Snowflake.prototype.relayAddr = null;
  142. Snowflake.prototype.rateLimit = null;
  143. Snowflake.prototype.pollInterval = null;
  144. Snowflake.MESSAGE = {
  145. CONFIRMATION: 'You\'re currently serving a Tor user via Snowflake.'
  146. };