[tor-commits] [snowflake/master] Standalone snowflake

arlo at torproject.org arlo at torproject.org
Tue Dec 20 18:39:45 UTC 2016


commit e2955562542ca5f607dc624c81edc21ca5ec74dc
Author: Arlo Breault <arlolra at gmail.com>
Date:   Mon Dec 19 20:09:46 2016 -0800

    Standalone snowflake
    
     * Start of #30
---
 .gitignore             |  1 +
 proxy/Cakefile         |  3 ++-
 proxy/package.json     | 18 ++++++++++++++----
 proxy/proxypair.coffee | 12 ++++++------
 proxy/shims.coffee     | 23 ++++++++++++++++++++---
 proxy/snowflake.coffee | 16 ++++++++--------
 proxy/ui.coffee        |  2 +-
 7 files changed, 52 insertions(+), 23 deletions(-)

diff --git a/.gitignore b/.gitignore
index 22101d6..a4a541e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,3 +13,4 @@ proxy/build
 proxy/node_modules
 proxy/spec/support
 ignore/
+npm-debug.log
diff --git a/proxy/Cakefile b/proxy/Cakefile
index f272583..6e5a413 100644
--- a/proxy/Cakefile
+++ b/proxy/Cakefile
@@ -35,7 +35,8 @@ task 'test', 'snowflake unit tests', ->
   # Simply concat all the files because we're not using node exports.
   jasmineFiles = FILES_ALL
   outFile = 'test/bundle.spec.coffee'
-  exec 'cat ' + jasmineFiles.join(' ') +  ' | cat > ' + outFile
+  exec 'echo "TESTING = true" > ' + outFile
+  exec 'cat ' + jasmineFiles.join(' ') +  ' | cat >> ' + outFile
   execSync 'coffee -cb ' + outFile
   spawn 'jasmine', ['test/bundle.spec.js'], {
     stdio: 'inherit'
diff --git a/proxy/package.json b/proxy/package.json
index 7fbc1a0..bdf3bf7 100644
--- a/proxy/package.json
+++ b/proxy/package.json
@@ -11,14 +11,24 @@
     "lint": "cake lint",
     "build": "cake build",
     "clean": "cake clean",
-    "modern": "modernizr -c modernizr-config.json -d static/"
+    "modern": "modernizr -c modernizr-config.json -d static/",
+    "prepublish": "npm run build",
+    "start": "node build/snowflake.js"
+  },
+  "bin": {
+    "snowflake": "build/snowflake.js"
   },
   "author": "Serene Han",
   "license": "BSD-3-Clause",
   "devDependencies": {
-    "coffee-script": "^1.10.0",
-    "coffeelint": "^1.15.0",
-    "jasmine": "^2.4.1",
+    "coffeelint": "^1.16.0",
+    "jasmine": "^2.5.2",
     "modernizr": "^3.3.1"
+  },
+  "dependencies": {
+    "coffee-script": "^1.12.2",
+    "wrtc": "^0.0.61",
+    "ws": "^1.1.1",
+    "xmlhttprequest": "^1.8.0"
   }
 }
diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee
index 6782523..d8253da 100644
--- a/proxy/proxypair.coffee
+++ b/proxy/proxypair.coffee
@@ -69,14 +69,14 @@ class ProxyPair
     channel.onopen = =>
       log 'WebRTC DataChannel opened!'
       snowflake.state = MODE.WEBRTC_READY
-      snowflake.ui.setActive true
+      snowflake.ui?.setActive true
       # This is the point when the WebRTC datachannel is done, so the next step
       # is to establish websocket to the server.
       @connectRelay()
     channel.onclose = =>
       log 'WebRTC DataChannel closed.'
-      snowflake.ui.setStatus 'disconnected by webrtc.'
-      snowflake.ui.setActive false
+      snowflake.ui?.setStatus 'disconnected by webrtc.'
+      snowflake.ui?.setActive false
       snowflake.state = MODE.INIT
       @flush()
       @close()
@@ -95,11 +95,11 @@ class ProxyPair
         clearTimeout @timer
         @timer = 0
       log @relay.label + ' connected!'
-      snowflake.ui.setStatus 'connected'
+      snowflake.ui?.setStatus 'connected'
     @relay.onclose = =>
       log @relay.label + ' closed.'
-      snowflake.ui.setStatus 'disconnected.'
-      snowflake.ui.setActive false
+      snowflake.ui?.setStatus 'disconnected.'
+      snowflake.ui?.setActive false
       snowflake.state = MODE.INIT
       @flush()
       @close()
diff --git a/proxy/shims.coffee b/proxy/shims.coffee
index f2f8fb9..325184c 100644
--- a/proxy/shims.coffee
+++ b/proxy/shims.coffee
@@ -1,11 +1,27 @@
 ###
-WebrTC shims for multiple browsers.
+WebRTC shims for multiple browsers.
 ###
 
-if typeof module isnt 'undefined' and module.exports
+if module?.exports
   window = {}
+  location = ''
+
+  if not TESTING? or not TESTING
+    webrtc = require 'wrtc'
+
+    PeerConnection = webrtc.RTCPeerConnection
+    IceCandidate = webrtc.RTCIceCandidate
+    SessionDescription = webrtc.RTCSessionDescription
+
+    WebSocket = require 'ws'
+    { XMLHttpRequest } = require 'xmlhttprequest'
+
+    process.nextTick () -> init true
+
 else
   window = this
+  location = window.location.search.substr(1)
+
   prop = Modernizr.prefixed 'RTCPeerConnection', window, false
   if not prop
     console.log 'webrtc feature not detected. shutting down'
@@ -18,4 +34,5 @@ else
   SessionDescription = window.RTCSessionDescription ||
     window.mozRTCSessionDescription
 
-location = if window.location then window.location.search.substr(1) else ""
+  WebSocket = window.WebSocket
+  XMLHttpRequest = window.XMLHttpRequest
diff --git a/proxy/snowflake.coffee b/proxy/snowflake.coffee
index 92ef4ab..5bdf315 100644
--- a/proxy/snowflake.coffee
+++ b/proxy/snowflake.coffee
@@ -13,7 +13,6 @@ DEFAULT_RELAY =
   port: 9902
 COPY_PASTE_ENABLED = false
 
-DEBUG = false
 silenceNotifications = false
 query = Query.parse(location)
 DEBUG = Params.getBool(query, 'debug', false)
@@ -88,7 +87,8 @@ class Snowflake
   pollBroker: ->
     # Temporary countdown. TODO: Simplify
     countdown = (msg, sec) =>
-      @ui.setStatus msg + ' (Polling in ' + sec + ' seconds...)'
+      dbg msg
+      @ui?.setStatus msg + ' (Polling in ' + sec + ' seconds...)'
       sec--
       if sec >= 0
         setTimeout((-> countdown(msg, sec)), 1000)
@@ -103,7 +103,7 @@ class Snowflake
         return
       msg = 'polling for client... '
       msg += '[retries: ' + @retries + ']' if @retries > 0
-      @ui.setStatus msg
+      @ui?.setStatus msg
       recv = @broker.getClientOffer pair.id
       recv.then (desc) =>
         @receiveOffer pair, desc
@@ -199,12 +199,12 @@ Signalling =
 # Log to both console and UI if applicable.
 log = (msg) ->
   console.log 'Snowflake: ' + msg
-  snowflake.ui.log msg
+  snowflake.ui?.log msg
 
-dbg = (msg) -> log msg if true == snowflake.ui.debug
+dbg = (msg) -> log msg if DEBUG or snowflake.ui?.debug
 
-init = ->
-  ui = new UI()
+init = (isNode) ->
+  ui = if isNode then null else new UI()
   silenceNotifications = Params.getBool(query, 'silent', false)
   brokerUrl = Params.getString(query, 'broker', DEFAULT_BROKER)
   broker = new Broker brokerUrl
@@ -229,4 +229,4 @@ window.onunload = ->
   pair.close() for pair in snowflake.proxyPairs
   null
 
-window.onload = init
+window.onload = init.bind null, false
diff --git a/proxy/ui.coffee b/proxy/ui.coffee
index c2f898f..1603ec3 100644
--- a/proxy/ui.coffee
+++ b/proxy/ui.coffee
@@ -3,7 +3,7 @@ All of Snowflake's DOM manipulation and inputs.
 ###
 
 class UI
-  debug = false  # True when there's no badge
+  debug: false  # True when there's no badge
 
   # DOM elements references.
   $msglog: null



More information about the tor-commits mailing list