[tor-commits] [snowflake/master] Snowflake proxy successfully reset and bootstrap a new client (#15)

serene at torproject.org serene at torproject.org
Fri Jan 22 20:22:49 UTC 2016


commit 77fbfe0e667886945df0755d19edfca11712f840
Author: Serene Han <keroserene+git at gmail.com>
Date:   Fri Jan 22 12:22:09 2016 -0800

    Snowflake proxy successfully reset and bootstrap a new client (#15)
---
 broker/snowflake-broker.go |    2 ++
 proxy/broker.coffee        |    1 -
 proxy/proxypair.coffee     |   18 +++++++++++++++---
 proxy/snowflake.coffee     |   37 ++++++++++++++++---------------------
 4 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/broker/snowflake-broker.go b/broker/snowflake-broker.go
index 13b0586..a6daa87 100644
--- a/broker/snowflake-broker.go
+++ b/broker/snowflake-broker.go
@@ -171,7 +171,9 @@ func proxyHandler(w http.ResponseWriter, r *http.Request) {
 		w.Write(offer)
 
 	case <-time.After(time.Second * 10):
+		// This snowflake is no longer available to serve clients.
 		heap.Remove(snowflakes, snowflake.index)
+		delete(snowflakeMap, snowflake.id)
 		w.WriteHeader(http.StatusGatewayTimeout)
 	}
 }
diff --git a/proxy/broker.coffee b/proxy/broker.coffee
index 91a81d2..34b5a88 100644
--- a/proxy/broker.coffee
+++ b/proxy/broker.coffee
@@ -69,7 +69,6 @@ class Broker
       return
     xhr.onreadystatechange = ->
       return if xhr.DONE != xhr.readyState
-      log xhr
       switch xhr.status
         when STATUS_OK
           log 'Broker: Successfully replied with answer.'
diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee
index f78e9f6..cd87208 100644
--- a/proxy/proxypair.coffee
+++ b/proxy/proxypair.coffee
@@ -19,7 +19,8 @@ class ProxyPair
 
   constructor: (@clientAddr, @relayAddr, @rateLimit) ->
 
-  connectClient: =>
+  # Prepare a WebRTC PeerConnection and await for an SDP offer.
+  begin: ->
     @pc = new PeerConnection config, {
       optional: [
         { DtlsSrtpKeyAgreement: true }
@@ -44,6 +45,16 @@ class ProxyPair
       @prepareDataChannel channel
       @client = channel
 
+  receiveWebRTCOffer: (offer) ->
+    console.assert 'offer' == offer.type
+    try
+      err = @pc.setRemoteDescription offer
+    catch e
+      log 'Invalid SDP message.'
+      return false
+    log 'SDP ' + offer.type + ' successfully received.'
+    true
+
   prepareDataChannel: (channel) =>
     channel.onopen = =>
       log 'Data channel opened!'
@@ -56,8 +67,9 @@ class ProxyPair
       log 'Data channel closed.'
       snowflake.state = MODE.INIT
       $msglog.className = ''
-    channel.onerror = ->
-      log 'Data channel error!'
+      # Change this for multiplexing.
+      snowflake.reset()
+    channel.onerror = -> log 'Data channel error!'
     channel.onmessage = @onClientToRelayMessage
 
   # Assumes WebRTC datachannel is connected.
diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee
index cc34f81..edf5e56 100644
--- a/proxy/snowflake.coffee
+++ b/proxy/snowflake.coffee
@@ -87,21 +87,17 @@ class Snowflake
       return false
     @relayAddr = addr
     log 'Using ' + relayAddr + ' as Relay.'
-    @beginWebRTC()
-    log 'Input offer from the snowflake client:'
+    log 'Input offer from the snowflake client:' if COPY_PASTE_ENABLED
     return true
 
   # Initialize WebRTC PeerConnection
   beginWebRTC: ->
-    log 'Starting up Snowflake...\n'
     @state = MODE.WEBRTC_CONNECTING
     for i in [1..CONNECTIONS_PER_CLIENT]
       @makeProxyPair @relayAddr
     @proxyPair = @proxyPairs[0]
-
-  # Poll broker for clients.
-  findClients: ->
-    poll = =>
+    # Poll broker for clients.
+    findClients = =>
       recv = broker.getClientOffer()
       recv.then((desc) =>
         offer = JSON.parse desc
@@ -109,21 +105,15 @@ class Snowflake
         @receiveOffer offer
       , (err) ->
         log err
-        setTimeout(poll, 1000)
+        setTimeout(findClients, 1000)
       )
-    poll()
+    findClients()
 
-  # Receive an SDP offer from client plugin.
+  # Receive an SDP offer from some client assigned by the Broker.
   receiveOffer: (desc) =>
     sdp = new RTCSessionDescription desc
-    try
-      err = @proxyPair.pc.setRemoteDescription sdp
-    catch e
-      log 'Invalid SDP message.'
-      return false
-    log 'SDP ' + sdp.type + ' successfully received.'
-    @sendAnswer() if 'offer' == sdp.type
-    true
+    if @proxyPair.receiveWebRTCOffer sdp
+      @sendAnswer() if 'offer' == sdp.type
 
   sendAnswer: =>
     next = (sdp) =>
@@ -132,7 +122,6 @@ class Snowflake
     promise = @proxyPair.pc.createAnswer next
     promise.then next if promise
 
-
   makeProxyPair: (relay) ->
     pair = new ProxyPair null, relay, @rateLimit
     @proxyPairs.push pair
@@ -141,7 +130,7 @@ class Snowflake
       @proxyPairs.splice(@proxyPairs.indexOf(pair), 1)
       @badge.endProxy() if @badge
     try
-      pair.connectClient()
+      pair.begin()
     catch err
       log 'ERROR: ProxyPair exception while connecting.'
       log err
@@ -162,6 +151,12 @@ class Snowflake
     @cease()
     @badge.die() if @badge
 
+  # Close all existing ProxyPairs and begin finding new clients from scratch.
+  reset: ->
+    @cease()
+    log '\nSnowflake resetting...'
+    @beginWebRTC()
+
 snowflake = null
 broker = null
 
@@ -241,6 +236,6 @@ init = ->
     log 'Input desired relay address:'
   else
     snowflake.setRelayAddr DEFAULT_WEBSOCKET
-    snowflake.findClients()
+    snowflake.beginWebRTC()
 
 window.onload = init if window



More information about the tor-commits mailing list