[tor-commits] [snowflake/master] Make a few object literals classes

arlo at torproject.org arlo at torproject.org
Wed May 8 15:41:17 UTC 2019


commit 898ba5707093f99ec8b5b12f16e0d3c29c697e8e
Author: Arlo Breault <arlolra at gmail.com>
Date:   Wed May 1 10:59:45 2019 -0400

    Make a few object literals classes
---
 proxy/init.coffee      | 14 +---------
 proxy/proxypair.coffee |  3 +-
 proxy/util.coffee      | 76 +++++++++++++++++++++++++++++---------------------
 3 files changed, 46 insertions(+), 47 deletions(-)

diff --git a/proxy/init.coffee b/proxy/init.coffee
index 22176e5..a75f67c 100644
--- a/proxy/init.coffee
+++ b/proxy/init.coffee
@@ -48,18 +48,6 @@ log = (msg) ->
 
 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
-  # Also do nothing if running in Tor Browser.
-  if mightBeTBB()
-    log 'Will not run within Tor Browser.'
-    return true
-  return false
-
 
 ###
 Entry point.
@@ -72,7 +60,7 @@ init = (isNode) ->
   snowflake = new Snowflake broker, ui
 
   log '== snowflake proxy =='
-  if snowflakeIsDisabled()
+  if Util.snowflakeIsDisabled()
     # Do not activate the proxy if any number of conditions are true.
     log 'Currently not active.'
     return
diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee
index 81ddd41..5a11f9a 100644
--- a/proxy/proxypair.coffee
+++ b/proxy/proxypair.coffee
@@ -8,7 +8,6 @@ Broker with an WebRTC answer.
 ###
 
 class ProxyPair
-
   MAX_BUFFER: 10 * 1024 * 1024
   pc:          null
   c2rSchedule: []
@@ -29,7 +28,7 @@ class ProxyPair
   ###
   constructor: (@relayAddr, @rateLimit) ->
     @active = false
-    @id = genSnowflakeID()
+    @id = Util.genSnowflakeID()
 
   # Prepare a WebRTC PeerConnection and await for an SDP offer.
   begin: ->
diff --git a/proxy/util.coffee b/proxy/util.coffee
index 8f4c75f..15b4152 100644
--- a/proxy/util.coffee
+++ b/proxy/util.coffee
@@ -4,26 +4,38 @@ A Coffeescript WebRTC snowflake proxy
 Contains helpers for parsing query strings and other utilities.
 ###
 
-
-# It would not be effective for Tor Browser users to run the proxy.
-# Do we seem to be running in Tor Browser? Check the user-agent string and for
-# no listing of supported MIME types.
-TBB_UAS = [
-    'Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0'
-    'Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0'
-    'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0'
-    'Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0'
-]
-mightBeTBB = ->
-  return TBB_UAS.indexOf(window.navigator.userAgent) > -1 and
-          (window.navigator.mimeTypes and
-           window.navigator.mimeTypes.length == 0)
-
-genSnowflakeID = ->
-  Math.random().toString(36).substring(2)
-
-
-Query =
+class Util
+  # It would not be effective for Tor Browser users to run the proxy.
+  # Do we seem to be running in Tor Browser? Check the user-agent string and for
+  # no listing of supported MIME types.
+  @TBB_UAS: [
+      'Mozilla/5.0 (Windows NT 6.1; rv:10.0) Gecko/20100101 Firefox/10.0'
+      'Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0'
+      'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0'
+      'Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0'
+  ]
+  @mightBeTBB: ->
+    return Util.TBB_UAS.indexOf(window.navigator.userAgent) > -1 and
+            (window.navigator.mimeTypes and
+             window.navigator.mimeTypes.length == 0)
+
+  @genSnowflakeID: ->
+    Math.random().toString(36).substring(2)
+
+  @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
+    # Also do nothing if running in Tor Browser.
+    if Util.mightBeTBB()
+      log 'Will not run within Tor Browser.'
+      return true
+    return false
+
+
+class Query
   ###
   Parse a URL query string or application/x-www-form-urlencoded body. The
   return type is an object mapping string keys to string values. By design,
@@ -34,7 +46,7 @@ Query =
   Always decodes from UTF-8, not any other encoding.
   http://dev.w3.org/html5/spec/Overview.html#url-encoded-form-data
   ###
-  parse: (qs) ->
+  @parse: (qs) ->
     result = {}
     strings = []
     strings = qs.split '&' if qs
@@ -53,7 +65,7 @@ Query =
     result
 
   # params is a list of (key, value) 2-tuples.
-  buildString: (params) ->
+  @buildString: (params) ->
     parts = []
     for param in params
       parts.push encodeURIComponent(param[0]) + '=' +
@@ -61,11 +73,11 @@ Query =
     parts.join '&'
 
 
-Parse =
+class Parse
   # Parse a cookie data string (usually document.cookie). The return type is an
   # object mapping cookies names to values. Returns null on error.
   # http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038
-  cookie: (cookies) ->
+  @cookie: (cookies) ->
     result = {}
     strings = []
     strings = cookies.split ';' if cookies
@@ -79,7 +91,7 @@ Parse =
 
   # Parse an address in the form 'host:port'. Returns an Object with keys 'host'
   # (String) and 'port' (int). Returns null on error.
-  address: (spec) ->
+  @address: (spec) ->
     m = null
     # IPv6 syntax.
     m = spec.match(/^\[([\0-9a-fA-F:.]+)\]:([0-9]+)$/) if !m
@@ -96,7 +108,7 @@ Parse =
 
   # Parse a count of bytes. A suffix of 'k', 'm', or 'g' (or uppercase)
   # does what you would think. Returns null on error.
-  byteCount: (spec) ->
+  @byteCount: (spec) ->
     UNITS = {
       k: 1024, m: 1024 * 1024, g: 1024 * 1024 * 1024
       K: 1024, M: 1024 * 1024, G: 1024 * 1024 * 1024
@@ -115,7 +127,7 @@ Parse =
   # Parse a connection-address out of the "c=" Connection Data field of a session
   # description. Return undefined if none is found.
   # https://tools.ietf.org/html/rfc4566#section-5.7
-  ipFromSDP: (sdp) ->
+  @ipFromSDP: (sdp) ->
     for pattern in [
       /^c=IN IP4 ([\d.]+)(?:(?:\/\d+)?\/\d+)?(:? |$)/m,
       /^c=IN IP6 ([0-9A-Fa-f:.]+)(?:\/\d+)?(:? |$)/m,
@@ -124,8 +136,8 @@ Parse =
       return m[1] if m?
 
 
-Params =
-  getBool: (query, param, defaultValue) ->
+class Params
+  @getBool: (query, param, defaultValue) ->
     val = query[param]
     return defaultValue if undefined == val
     return true if 'true' == val || '1' == val || '' == val
@@ -135,21 +147,21 @@ Params =
   # Get an object value and parse it as a byte count. Example byte counts are
   # '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null
   # on a parsing error.
-  getByteCount: (query, param, defaultValue) ->
+  @getByteCount: (query, param, defaultValue) ->
     spec = query[param]
     return defaultValue if undefined == spec
     Parse.byteCount spec
 
   # Get an object value and parse it as an address spec. Returns |defaultValue|
   # if param is not a key. Returns null on a parsing error.
-  getAddress: (query, param, defaultValue) ->
+  @getAddress: (query, param, defaultValue) ->
     val = query[param]
     return defaultValue if undefined == val
     Parse.address val
 
   # Get an object value and return it as a string. Returns default_val if param
   # is not a key.
-  getString: (query, param, defaultValue) ->
+  @getString: (query, param, defaultValue) ->
     val = query[param]
     return defaultValue if undefined == val
     val





More information about the tor-commits mailing list