move disable check to snowflakeIsDisabled

This commit is contained in:
Serene H 2017-03-01 18:51:24 -08:00
parent 3fe10f3e7c
commit 6d2b0ac420
2 changed files with 30 additions and 15 deletions

View file

@ -18,8 +18,9 @@ class Broker
clients: 0 clients: 0
# When interacting with the Broker, snowflake must generate a unique session # 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 # ID so the Broker can keep track of each proxy's signalling channels.
# to. # On construction, this Broker object does not do anything until
# |getClientOffer| is called.
constructor: (@url) -> constructor: (@url) ->
@clients = 0 @clients = 0
# Ensure url has the right protocol + trailing slash. # Ensure url has the right protocol + trailing slash.

View file

@ -4,7 +4,9 @@ A Coffeescript WebRTC snowflake proxy
Uses WebRTC from the client, and Websocket to the server. Uses WebRTC from the client, and Websocket to the server.
Assume that the webrtc client plugin is always the offerer, in which case Assume that the webrtc client plugin is always the offerer, in which case
this must always act as the answerer. this proxy must always act as the answerer.
TODO: More documentation
### ###
# General snowflake proxy constants. # General snowflake proxy constants.
@ -70,16 +72,18 @@ class Snowflake
RATE_LIMIT_HISTORY) RATE_LIMIT_HISTORY)
@retries = 0 @retries = 0
# TODO: Should potentially fetch from broker later. # Set the target relay address spec, which is expected to be websocket.
# Set the target relay address spec, which is expected to be a websocket # TODO: Should potentially fetch the target from broker later, or modify
# relay. # entirely for the Tor-independent version.
setRelayAddr: (relayAddr) -> setRelayAddr: (relayAddr) ->
@relayAddr = relayAddr @relayAddr = relayAddr
log 'Using ' + relayAddr.host + ':' + relayAddr.port + ' as Relay.' log 'Using ' + relayAddr.host + ':' + relayAddr.port + ' as Relay.'
log 'Input offer from the snowflake client:' if COPY_PASTE_ENABLED log 'Input offer from the snowflake client:' if COPY_PASTE_ENABLED
return true return true
# Initialize WebRTC PeerConnection # Initialize WebRTC PeerConnection, which requires beginning the signalling
# process. If in copy paste mode, the user will need to copy and paste the SDP
# blobs. Otherwise, |pollBroker| automatically arranges signalling.
beginWebRTC: -> beginWebRTC: ->
@state = MODE.WEBRTC_CONNECTING @state = MODE.WEBRTC_CONNECTING
for i in [1..CONNECTIONS_PER_CLIENT] for i in [1..CONNECTIONS_PER_CLIENT]
@ -204,12 +208,22 @@ Signalling =
snowflake.receiveOffer pair, msg snowflake.receiveOffer pair, msg
# Log to both console and UI if applicable. # Log to both console and UI if applicable.
# Requires that the snowflake and UI objects are hooked up in order to
# log to console.
log = (msg) -> log = (msg) ->
console.log 'Snowflake: ' + msg console.log 'Snowflake: ' + msg
snowflake.ui?.log msg snowflake?.ui?.log msg
dbg = (msg) -> log msg if DEBUG or snowflake.ui?.debug dbg = (msg) -> log msg if DEBUG or snowflake.ui?.debug
snowflakeIsDisabled = ->
cookies = Parse.cookie document.cookie
# Do nothing if snowflake has not been opted in by user.
if cookies[COOKIE_NAME] != '1'
log 'Not opted-in. Please click the badge to change options.'
return true
return false
### ###
Entry point. Entry point.
### ###
@ -217,21 +231,21 @@ init = (isNode) ->
# Hook up to the debug UI if available. # Hook up to the debug UI if available.
ui = if isNode then null else new UI() ui = if isNode then null else new UI()
silenceNotifications = Params.getBool(query, 'silent', false) silenceNotifications = Params.getBool(query, 'silent', false)
# Establish connectivity information with the Broker. # Retrieve connectivity information for the Broker and
# initialize Snowflake contexts.
brokerUrl = Params.getString(query, 'broker', DEFAULT_BROKER) brokerUrl = Params.getString(query, 'broker', DEFAULT_BROKER)
broker = new Broker brokerUrl broker = new Broker brokerUrl
snowflake = new Snowflake broker, ui snowflake = new Snowflake broker, ui
cookies = Parse.cookie document.cookie log '== snowflake proxy =='
# Do nothing if snowflake has not been opted in. if snowflakeIsDisabled()
if cookies[COOKIE_NAME] != "1" # Do not activate the proxy if any number of conditions are true.
log 'Not activate. Please click the badge to change options.' log 'Currently not active.'
return return
log '== snowflake proxy ==' # Otherwise, begin setting up WebRTC and acting as a proxy.
log 'Copy-Paste mode detected.' if COPY_PASTE_ENABLED log 'Copy-Paste mode detected.' if COPY_PASTE_ENABLED
dbg 'Contacting Broker at ' + broker.url if not COPY_PASTE_ENABLED dbg 'Contacting Broker at ' + broker.url if not COPY_PASTE_ENABLED
relayAddr = Params.getAddress(query, 'relay', DEFAULT_RELAY) relayAddr = Params.getAddress(query, 'relay', DEFAULT_RELAY)
snowflake.setRelayAddr relayAddr snowflake.setRelayAddr relayAddr
snowflake.beginWebRTC() snowflake.beginWebRTC()