mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 23:11:29 -04:00
per-proxypair Snowflake ID generation instead of just one for the Broker (#11)
This commit is contained in:
parent
422717f36f
commit
b1e76420bd
5 changed files with 44 additions and 23 deletions
|
@ -12,21 +12,16 @@ STATUS_GATEWAY_TIMEOUT = 504
|
|||
MESSAGE_TIMEOUT = 'Timed out waiting for a client offer.'
|
||||
MESSAGE_UNEXPECTED = 'Unexpected status.'
|
||||
|
||||
genSnowflakeID = ->
|
||||
Math.random().toString(36).substring(2)
|
||||
|
||||
# Represents a broker running remotely.
|
||||
class Broker
|
||||
|
||||
clients: 0
|
||||
id: null
|
||||
|
||||
# When interacting with the Broker, snowflake must generate a unique session
|
||||
# ID so the Broker can keep track of which signalling channel it's speaking
|
||||
# to.
|
||||
constructor: (@url) ->
|
||||
@clients = 0
|
||||
@id = genSnowflakeID()
|
||||
# Ensure url has the right protocol + trailing slash.
|
||||
@url = 'http://' + @url if 0 == @url.indexOf('localhost', 0)
|
||||
@url = 'https://' + @url if 0 != @url.indexOf('http', 0)
|
||||
|
@ -37,7 +32,7 @@ class Broker
|
|||
# waits for a response containing some client offer that the Broker chooses
|
||||
# for this proxy..
|
||||
# TODO: Actually support multiple clients.
|
||||
getClientOffer: =>
|
||||
getClientOffer: (id) =>
|
||||
new Promise (fulfill, reject) =>
|
||||
xhr = new XMLHttpRequest()
|
||||
xhr.onreadystatechange = ->
|
||||
|
@ -53,12 +48,12 @@ class Broker
|
|||
snowflake.ui.setStatus ' failure. Please refresh.'
|
||||
reject MESSAGE_UNEXPECTED
|
||||
@_xhr = xhr # Used by spec to fake async Broker interaction
|
||||
@_postRequest xhr, 'proxy', @id
|
||||
@_postRequest id, xhr, 'proxy', id
|
||||
|
||||
# Assumes getClientOffer happened, and a WebRTC SDP answer has been generated.
|
||||
# Sends it back to the broker, which passes it to back to the original client.
|
||||
sendAnswer: (answer) ->
|
||||
dbg @id + ' - Sending answer back to broker...\n'
|
||||
sendAnswer: (id, answer) ->
|
||||
dbg id + ' - Sending answer back to broker...\n'
|
||||
dbg answer.sdp
|
||||
xhr = new XMLHttpRequest()
|
||||
xhr.onreadystatechange = ->
|
||||
|
@ -73,14 +68,14 @@ class Broker
|
|||
dbg 'Broker ERROR: Unexpected ' + xhr.status +
|
||||
' - ' + xhr.statusText
|
||||
snowflake.ui.setStatus ' failure. Please refresh.'
|
||||
@_postRequest xhr, 'answer', JSON.stringify(answer)
|
||||
@_postRequest id, xhr, 'answer', JSON.stringify(answer)
|
||||
|
||||
# urlSuffix for the broker is different depending on what action
|
||||
# is desired.
|
||||
_postRequest: (xhr, urlSuffix, payload) =>
|
||||
_postRequest: (id, xhr, urlSuffix, payload) =>
|
||||
try
|
||||
xhr.open 'POST', @url + urlSuffix
|
||||
xhr.setRequestHeader('X-Session-ID', @id)
|
||||
xhr.setRequestHeader('X-Session-ID', id)
|
||||
catch err
|
||||
###
|
||||
An exception happens here when, for example, NoScript allows the domain
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue