[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake/master] better checks on receiveOffer (#11)



commit 490b8b33b78e318267088d79e362a44d88cc8228
Author: Serene Han <keroserene+git@xxxxxxxxx>
Date:   Tue Mar 15 20:13:39 2016 -0700

    better checks on receiveOffer (#11)
---
 proxy/proxypair.coffee           |  4 +++-
 proxy/snowflake.coffee           | 35 +++++++++++++++--------------------
 proxy/spec/snowflake.spec.coffee | 16 ++++++++++++----
 3 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee
index 3a3fb01..3ed6c6c 100644
--- a/proxy/proxypair.coffee
+++ b/proxy/proxypair.coffee
@@ -51,7 +51,9 @@ class ProxyPair
       @client = channel
 
   receiveWebRTCOffer: (offer) ->
-    console.assert 'offer' == offer.type
+    if 'offer' != offer.type
+      log 'Invalid SDP received -- was not an offer.'
+      return false
     try
       err = @pc.setRemoteDescription offer
     catch e
diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee
index 7cc01c9..4f38029 100644
--- a/proxy/snowflake.coffee
+++ b/proxy/snowflake.coffee
@@ -50,12 +50,11 @@ CONFIRMATION_MESSAGE = "You're currently serving a Tor user via Snowflake."
 # Minimum viable snowflake for now - just 1 client.
 class Snowflake
 
-  relayAddr: null
+  relayAddr:  null
   proxyPairs: []
-
-  rateLimit: null
-  state: MODE.INIT
-  retries: 0
+  rateLimit:  null
+  state:      MODE.INIT
+  retries:    0
 
   constructor: (@broker, @ui) ->
     rateLimitBytes = undefined
@@ -108,16 +107,8 @@ class Snowflake
         log 'No more available ProxyPair slots.'
         countdown(err, DEFAULT_BROKER_POLL_INTERVAL / 1000)
         return
-      log 'Polling for ' + pair.id
       recv = @broker.getClientOffer pair.id
-      recv.then (desc) =>
-        offer = JSON.parse desc
-        dbg 'Received:\n\n' + offer.sdp + '\n'
-        console.log desc
-        sdp = new RTCSessionDescription offer
-        # @receiveOffer offer
-        if pair.receiveWebRTCOffer sdp
-          @sendAnswer pair if 'offer' == sdp.type
+      recv.then (desc) => @receiveOffer pair, desc
       , (err) ->
         countdown(err, DEFAULT_BROKER_POLL_INTERVAL / 1000)
       @retries++
@@ -129,12 +120,16 @@ class Snowflake
     return @proxyPairs.find (pp, i, arr) -> return !pp.active
 
   # Receive an SDP offer from some client assigned by the Broker,
-  # TODO: remove
-  receiveOffer: (desc) =>
-    sdp = new RTCSessionDescription desc
-    pair = @nextAvailableProxyPair()
-    if pair.receiveWebRTCOffer sdp
-      @sendAnswer pair if 'offer' == sdp.type
+  # |pair| - an available ProxyPair.
+  receiveOffer: (pair, desc) =>
+    console.assert !pair.active
+    try
+      offer = JSON.parse desc
+      dbg 'Received:\n\n' + offer.sdp + '\n'
+      sdp = new RTCSessionDescription offer
+      @sendAnswer pair if pair.receiveWebRTCOffer sdp
+    catch e
+      log 'ERROR: Unable to receive Offer: ' + e
 
   sendAnswer: (pair) ->
     next = (sdp) ->
diff --git a/proxy/spec/snowflake.spec.coffee b/proxy/spec/snowflake.spec.coffee
index ca7661b..417b6e6 100644
--- a/proxy/spec/snowflake.spec.coffee
+++ b/proxy/spec/snowflake.spec.coffee
@@ -47,14 +47,22 @@ describe 'Snowflake', ->
     expect(s.retries).toBe 1
     expect(s.broker.getClientOffer).toHaveBeenCalled()
 
-  it 'receives SDP offer', ->
+  it 'receives SDP offer and sends answer', ->
     s = new Snowflake(new FakeBroker(), fakeUI)
-    s.proxyPairs[0] = { receiveWebRTCOffer: -> }
-    spyOn(s.proxyPairs[0], 'receiveWebRTCOffer').and.returnValue true
+    pair = { receiveWebRTCOffer: -> }
+    spyOn(pair, 'receiveWebRTCOffer').and.returnValue true
     spyOn(s, 'sendAnswer')
-    s.receiveOffer { 'type': 'offer', 'sdp': 'foo' }
+    s.receiveOffer pair, '{"type":"offer","sdp":"foo"}'
     expect(s.sendAnswer).toHaveBeenCalled()
 
+  it 'does not send answer when receiving invalid offer', ->
+    s = new Snowflake(new FakeBroker(), fakeUI)
+    pair = { receiveWebRTCOffer: -> }
+    spyOn(pair, 'receiveWebRTCOffer').and.returnValue false
+    spyOn(s, 'sendAnswer')
+    s.receiveOffer pair, '{"type":"not a good offer","sdp":"foo"}'
+    expect(s.sendAnswer).not.toHaveBeenCalled()
+
   it 'can make a proxypair', ->
     s = new Snowflake(new FakeBroker(), fakeUI)
     s.makeProxyPair()



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits