commit 5e318b62199401b5ee82f8ce3ba3c05198f0ba8c Author: Serene Han keroserene+git@gmail.com Date: Sat Mar 12 22:29:56 2016 -0800
prepare for multiplexed snowflake (#11) --- proxy/broker.coffee | 2 +- proxy/proxypair.coffee | 11 +++++++---- proxy/snowflake.coffee | 20 +++++++++----------- proxy/spec/snowflake.spec.coffee | 6 +++--- 4 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/proxy/broker.coffee b/proxy/broker.coffee index 71c3186..78577e7 100644 --- a/proxy/broker.coffee +++ b/proxy/broker.coffee @@ -33,7 +33,7 @@ class Broker @url += '/' if '/' != @url.substr -1
# Promises some client SDP Offer. - # Registers this Snowfalke with the broker using an HTTP POST request, and + # Registers this Snowflake with the broker using an HTTP POST request, and # waits for a response containing some client offer that the Broker chooses # for this proxy.. # TODO: Actually support multiple clients. diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee index b8db513..c6478cb 100644 --- a/proxy/proxypair.coffee +++ b/proxy/proxypair.coffee @@ -8,15 +8,17 @@ Represents a single: class ProxyPair
MAX_BUFFER: 10 * 1024 * 1024 - pc: null + pc: null c2rSchedule: [] r2cSchedule: [] - client: null # WebRTC Data channel - relay: null # websocket - running: true + client: null # WebRTC Data channel + relay: null # websocket + running: true + active: false # Whether serving a client. flush_timeout_id: null
constructor: (@clientAddr, @relayAddr, @rateLimit) -> + @active = false
# Prepare a WebRTC PeerConnection and await for an SDP offer. begin: -> @@ -51,6 +53,7 @@ class ProxyPair log 'Invalid SDP message.' return false dbg 'SDP ' + offer.type + ' successfully received.' + @active = true true
prepareDataChannel: (channel) => diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee index 4a98472..31a553f 100644 --- a/proxy/snowflake.coffee +++ b/proxy/snowflake.coffee @@ -51,10 +51,7 @@ CONFIRMATION_MESSAGE = "You're currently serving a Tor user via Snowflake." class Snowflake
relayAddr: null - # TODO: Actually support multiple ProxyPairs. (makes more sense once meek- - # signalling is ready) proxyPairs: [] - proxyPair: null
rateLimit: null state: MODE.INIT @@ -86,7 +83,6 @@ class Snowflake @state = MODE.WEBRTC_CONNECTING for i in [1..CONNECTIONS_PER_CLIENT] @makeProxyPair @relayAddr - @proxyPair = @proxyPairs[0] return if COPY_PASTE_ENABLED timer = null # Temporary countdown. @@ -114,17 +110,19 @@ class Snowflake
findClients()
- # Receive an SDP offer from some client assigned by the Broker. + # Receive an SDP offer from some client assigned by the Broker, receiveOffer: (desc) => sdp = new RTCSessionDescription desc - if @proxyPair.receiveWebRTCOffer sdp - @sendAnswer() if 'offer' == sdp.type + # Use the first proxyPair that's available. + pair = @proxyPairs.find (pp, i, arr) -> return !pp.active + if pair.receiveWebRTCOffer sdp + @sendAnswer pair if 'offer' == sdp.type
- sendAnswer: => - next = (sdp) => + sendAnswer: (pair) -> + next = (sdp) -> dbg 'webrtc: Answer ready.' - @proxyPair.pc.setLocalDescription sdp - promise = @proxyPair.pc.createAnswer next + pair.pc.setLocalDescription sdp + promise = pair.pc.createAnswer next promise.then next if promise
makeProxyPair: (relay) -> diff --git a/proxy/spec/snowflake.spec.coffee b/proxy/spec/snowflake.spec.coffee index 9b63419..ca7661b 100644 --- a/proxy/spec/snowflake.spec.coffee +++ b/proxy/spec/snowflake.spec.coffee @@ -49,10 +49,10 @@ describe 'Snowflake', ->
it 'receives SDP offer', -> s = new Snowflake(new FakeBroker(), fakeUI) - s.proxyPair = { receiveWebRTCOffer: -> } - spyOn(s.proxyPair, 'receiveWebRTCOffer').and.returnValue true + s.proxyPairs[0] = { receiveWebRTCOffer: -> } + spyOn(s.proxyPairs[0], 'receiveWebRTCOffer').and.returnValue true spyOn(s, 'sendAnswer') - s.receiveOffer 'foo' + s.receiveOffer { 'type': 'offer', 'sdp': 'foo' } expect(s.sendAnswer).toHaveBeenCalled()
it 'can make a proxypair', ->