mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
prepare for multiplexed snowflake (#11)
This commit is contained in:
parent
6b5a36ac84
commit
5e318b6219
4 changed files with 20 additions and 19 deletions
|
@ -33,7 +33,7 @@ class Broker
|
||||||
@url += '/' if '/' != @url.substr -1
|
@url += '/' if '/' != @url.substr -1
|
||||||
|
|
||||||
# Promises some client SDP Offer.
|
# 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
|
# waits for a response containing some client offer that the Broker chooses
|
||||||
# for this proxy..
|
# for this proxy..
|
||||||
# TODO: Actually support multiple clients.
|
# TODO: Actually support multiple clients.
|
||||||
|
|
|
@ -14,9 +14,11 @@ class ProxyPair
|
||||||
client: null # WebRTC Data channel
|
client: null # WebRTC Data channel
|
||||||
relay: null # websocket
|
relay: null # websocket
|
||||||
running: true
|
running: true
|
||||||
|
active: false # Whether serving a client.
|
||||||
flush_timeout_id: null
|
flush_timeout_id: null
|
||||||
|
|
||||||
constructor: (@clientAddr, @relayAddr, @rateLimit) ->
|
constructor: (@clientAddr, @relayAddr, @rateLimit) ->
|
||||||
|
@active = false
|
||||||
|
|
||||||
# Prepare a WebRTC PeerConnection and await for an SDP offer.
|
# Prepare a WebRTC PeerConnection and await for an SDP offer.
|
||||||
begin: ->
|
begin: ->
|
||||||
|
@ -51,6 +53,7 @@ class ProxyPair
|
||||||
log 'Invalid SDP message.'
|
log 'Invalid SDP message.'
|
||||||
return false
|
return false
|
||||||
dbg 'SDP ' + offer.type + ' successfully received.'
|
dbg 'SDP ' + offer.type + ' successfully received.'
|
||||||
|
@active = true
|
||||||
true
|
true
|
||||||
|
|
||||||
prepareDataChannel: (channel) =>
|
prepareDataChannel: (channel) =>
|
||||||
|
|
|
@ -51,10 +51,7 @@ CONFIRMATION_MESSAGE = "You're currently serving a Tor user via Snowflake."
|
||||||
class Snowflake
|
class Snowflake
|
||||||
|
|
||||||
relayAddr: null
|
relayAddr: null
|
||||||
# TODO: Actually support multiple ProxyPairs. (makes more sense once meek-
|
|
||||||
# signalling is ready)
|
|
||||||
proxyPairs: []
|
proxyPairs: []
|
||||||
proxyPair: null
|
|
||||||
|
|
||||||
rateLimit: null
|
rateLimit: null
|
||||||
state: MODE.INIT
|
state: MODE.INIT
|
||||||
|
@ -86,7 +83,6 @@ class Snowflake
|
||||||
@state = MODE.WEBRTC_CONNECTING
|
@state = MODE.WEBRTC_CONNECTING
|
||||||
for i in [1..CONNECTIONS_PER_CLIENT]
|
for i in [1..CONNECTIONS_PER_CLIENT]
|
||||||
@makeProxyPair @relayAddr
|
@makeProxyPair @relayAddr
|
||||||
@proxyPair = @proxyPairs[0]
|
|
||||||
return if COPY_PASTE_ENABLED
|
return if COPY_PASTE_ENABLED
|
||||||
timer = null
|
timer = null
|
||||||
# Temporary countdown.
|
# Temporary countdown.
|
||||||
|
@ -114,17 +110,19 @@ class Snowflake
|
||||||
|
|
||||||
findClients()
|
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) =>
|
receiveOffer: (desc) =>
|
||||||
sdp = new RTCSessionDescription desc
|
sdp = new RTCSessionDescription desc
|
||||||
if @proxyPair.receiveWebRTCOffer sdp
|
# Use the first proxyPair that's available.
|
||||||
@sendAnswer() if 'offer' == sdp.type
|
pair = @proxyPairs.find (pp, i, arr) -> return !pp.active
|
||||||
|
if pair.receiveWebRTCOffer sdp
|
||||||
|
@sendAnswer pair if 'offer' == sdp.type
|
||||||
|
|
||||||
sendAnswer: =>
|
sendAnswer: (pair) ->
|
||||||
next = (sdp) =>
|
next = (sdp) ->
|
||||||
dbg 'webrtc: Answer ready.'
|
dbg 'webrtc: Answer ready.'
|
||||||
@proxyPair.pc.setLocalDescription sdp
|
pair.pc.setLocalDescription sdp
|
||||||
promise = @proxyPair.pc.createAnswer next
|
promise = pair.pc.createAnswer next
|
||||||
promise.then next if promise
|
promise.then next if promise
|
||||||
|
|
||||||
makeProxyPair: (relay) ->
|
makeProxyPair: (relay) ->
|
||||||
|
|
|
@ -49,10 +49,10 @@ describe 'Snowflake', ->
|
||||||
|
|
||||||
it 'receives SDP offer', ->
|
it 'receives SDP offer', ->
|
||||||
s = new Snowflake(new FakeBroker(), fakeUI)
|
s = new Snowflake(new FakeBroker(), fakeUI)
|
||||||
s.proxyPair = { receiveWebRTCOffer: -> }
|
s.proxyPairs[0] = { receiveWebRTCOffer: -> }
|
||||||
spyOn(s.proxyPair, 'receiveWebRTCOffer').and.returnValue true
|
spyOn(s.proxyPairs[0], 'receiveWebRTCOffer').and.returnValue true
|
||||||
spyOn(s, 'sendAnswer')
|
spyOn(s, 'sendAnswer')
|
||||||
s.receiveOffer 'foo'
|
s.receiveOffer { 'type': 'offer', 'sdp': 'foo' }
|
||||||
expect(s.sendAnswer).toHaveBeenCalled()
|
expect(s.sendAnswer).toHaveBeenCalled()
|
||||||
|
|
||||||
it 'can make a proxypair', ->
|
it 'can make a proxypair', ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue