mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-13 20:11:19 -04:00
simplify proxypair relay onclose
This commit is contained in:
parent
5e318b6219
commit
3339b9f172
2 changed files with 24 additions and 35 deletions
|
@ -16,6 +16,7 @@ class ProxyPair
|
||||||
running: true
|
running: true
|
||||||
active: false # Whether serving a client.
|
active: false # Whether serving a client.
|
||||||
flush_timeout_id: null
|
flush_timeout_id: null
|
||||||
|
onCleanup: null
|
||||||
|
|
||||||
constructor: (@clientAddr, @relayAddr, @rateLimit) ->
|
constructor: (@clientAddr, @relayAddr, @rateLimit) ->
|
||||||
@active = false
|
@active = false
|
||||||
|
@ -56,6 +57,7 @@ class ProxyPair
|
||||||
@active = true
|
@active = true
|
||||||
true
|
true
|
||||||
|
|
||||||
|
# Given a WebRTC DataChannel, prepare callbacks.
|
||||||
prepareDataChannel: (channel) =>
|
prepareDataChannel: (channel) =>
|
||||||
channel.onopen = =>
|
channel.onopen = =>
|
||||||
log 'WebRTC DataChannel opened!'
|
log 'WebRTC DataChannel opened!'
|
||||||
|
@ -64,12 +66,14 @@ class ProxyPair
|
||||||
# This is the point when the WebRTC datachannel is done, so the next step
|
# This is the point when the WebRTC datachannel is done, so the next step
|
||||||
# is to establish websocket to the server.
|
# is to establish websocket to the server.
|
||||||
@connectRelay()
|
@connectRelay()
|
||||||
channel.onclose = ->
|
channel.onclose = =>
|
||||||
log 'WebRTC DataChannel closed.'
|
log 'WebRTC DataChannel closed.'
|
||||||
snowflake.ui.setStatus 'disconnected by webrtc.'
|
snowflake.ui.setStatus 'disconnected by webrtc.'
|
||||||
snowflake.ui.setActive false
|
snowflake.ui.setActive false
|
||||||
snowflake.state = MODE.INIT
|
snowflake.state = MODE.INIT
|
||||||
# Change this for multiplexing.
|
@flush()
|
||||||
|
@close()
|
||||||
|
# TODO: Change this for multiplexing.
|
||||||
snowflake.reset()
|
snowflake.reset()
|
||||||
channel.onerror = -> log 'Data channel error!'
|
channel.onerror = -> log 'Data channel error!'
|
||||||
channel.onmessage = @onClientToRelayMessage
|
channel.onmessage = @onClientToRelayMessage
|
||||||
|
@ -82,7 +86,14 @@ class ProxyPair
|
||||||
@relay.onopen = =>
|
@relay.onopen = =>
|
||||||
log @relay.label + ' connected!'
|
log @relay.label + ' connected!'
|
||||||
snowflake.ui.setStatus 'connected'
|
snowflake.ui.setStatus 'connected'
|
||||||
@relay.onclose = @onClose
|
@relay.onclose = (event) =>
|
||||||
|
ws = event.target
|
||||||
|
log ws.label + ' closed.'
|
||||||
|
snowflake.ui.setStatus 'disconnected.'
|
||||||
|
snowflake.ui.setActive false
|
||||||
|
snowflake.state = MODE.INIT
|
||||||
|
@flush()
|
||||||
|
@close()
|
||||||
@relay.onerror = @onError
|
@relay.onerror = @onError
|
||||||
@relay.onmessage = @onRelayToClientMessage
|
@relay.onmessage = @onRelayToClientMessage
|
||||||
|
|
||||||
|
@ -105,47 +116,25 @@ class ProxyPair
|
||||||
# log 'websocket-->WebRTC data: ' + event.data
|
# log 'websocket-->WebRTC data: ' + event.data
|
||||||
@flush()
|
@flush()
|
||||||
|
|
||||||
onClose: (event) =>
|
|
||||||
ws = event.target
|
|
||||||
log ws.label + ' closed.'
|
|
||||||
snowflake.ui.setStatus 'disconnected.'
|
|
||||||
snowflake.ui.setActive false
|
|
||||||
snowflake.state = MODE.INIT
|
|
||||||
@flush()
|
|
||||||
@maybeCleanup()
|
|
||||||
|
|
||||||
onError: (event) =>
|
onError: (event) =>
|
||||||
ws = event.target
|
ws = event.target
|
||||||
log ws.label + ' error.'
|
log ws.label + ' error.'
|
||||||
@close()
|
@close()
|
||||||
# we can't rely on onclose_callback to cleanup, since one common error
|
|
||||||
# case is when the client fails to connect and the relay never starts.
|
|
||||||
# in that case close() is a NOP and onclose_callback is never called.
|
|
||||||
@maybeCleanup()
|
|
||||||
|
|
||||||
webrtcIsReady: -> null != @client && 'open' == @client.readyState
|
# Close both WebRTC and websocket.
|
||||||
relayIsReady: -> (null != @relay) && (WebSocket.OPEN == @relay.readyState)
|
|
||||||
isClosed: (ws) -> undefined == ws || WebSocket.CLOSED == ws.readyState
|
|
||||||
close: ->
|
close: ->
|
||||||
|
@running = false
|
||||||
@client.close() if @webrtcIsReady()
|
@client.close() if @webrtcIsReady()
|
||||||
@relay.close() if @relayIsReady()
|
@relay.close() if @relayIsReady()
|
||||||
relay = null
|
relay = null
|
||||||
|
|
||||||
maybeCleanup: =>
|
# Send as much data in both directions as the rate limit currently allows.
|
||||||
if @running
|
|
||||||
@running = false
|
|
||||||
# TODO: Call external callback
|
|
||||||
true
|
|
||||||
false
|
|
||||||
|
|
||||||
# Send as much data as the rate limit currently allows.
|
|
||||||
flush: =>
|
flush: =>
|
||||||
clearTimeout @flush_timeout_id if @flush_timeout_id
|
clearTimeout @flush_timeout_id if @flush_timeout_id
|
||||||
@flush_timeout_id = null
|
@flush_timeout_id = null
|
||||||
busy = true
|
busy = true
|
||||||
checkChunks = =>
|
checkChunks = =>
|
||||||
busy = false
|
busy = false
|
||||||
|
|
||||||
# WebRTC --> websocket
|
# WebRTC --> websocket
|
||||||
if @relayIsReady() &&
|
if @relayIsReady() &&
|
||||||
@relay.bufferedAmount < @MAX_BUFFER &&
|
@relay.bufferedAmount < @MAX_BUFFER &&
|
||||||
|
@ -154,7 +143,6 @@ class ProxyPair
|
||||||
@rateLimit.update chunk.length
|
@rateLimit.update chunk.length
|
||||||
@relay.send chunk
|
@relay.send chunk
|
||||||
busy = true
|
busy = true
|
||||||
|
|
||||||
# websocket --> WebRTC
|
# websocket --> WebRTC
|
||||||
if @webrtcIsReady() &&
|
if @webrtcIsReady() &&
|
||||||
@client.bufferedAmount < @MAX_BUFFER &&
|
@client.bufferedAmount < @MAX_BUFFER &&
|
||||||
|
@ -170,3 +158,8 @@ class ProxyPair
|
||||||
(@relayIsReady() && @relay.bufferedAmount > 0) ||
|
(@relayIsReady() && @relay.bufferedAmount > 0) ||
|
||||||
(@webrtcIsReady() && @client.bufferedAmount > 0)
|
(@webrtcIsReady() && @client.bufferedAmount > 0)
|
||||||
@flush_timeout_id = setTimeout @flush, @rateLimit.when() * 1000
|
@flush_timeout_id = setTimeout @flush, @rateLimit.when() * 1000
|
||||||
|
|
||||||
|
webrtcIsReady: -> null != @client && 'open' == @client.readyState
|
||||||
|
relayIsReady: -> (null != @relay) && (WebSocket.OPEN == @relay.readyState)
|
||||||
|
isClosed: (ws) -> undefined == ws || WebSocket.CLOSED == ws.readyState
|
||||||
|
|
||||||
|
|
|
@ -131,13 +131,9 @@ class Snowflake
|
||||||
pair.onCleanup = (event) =>
|
pair.onCleanup = (event) =>
|
||||||
# Delete from the list of active proxy pairs.
|
# Delete from the list of active proxy pairs.
|
||||||
@proxyPairs.splice(@proxyPairs.indexOf(pair), 1)
|
@proxyPairs.splice(@proxyPairs.indexOf(pair), 1)
|
||||||
try
|
pair.begin()
|
||||||
pair.begin()
|
|
||||||
catch err
|
|
||||||
log 'ERROR: ProxyPair exception while connecting.'
|
|
||||||
log err
|
|
||||||
return
|
|
||||||
|
|
||||||
|
# Stop all proxypairs.
|
||||||
cease: ->
|
cease: ->
|
||||||
while @proxyPairs.length > 0
|
while @proxyPairs.length > 0
|
||||||
@proxyPairs.pop().close()
|
@proxyPairs.pop().close()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue