mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Make a few object literals classes
This commit is contained in:
parent
7ce3c83a31
commit
898ba57070
3 changed files with 42 additions and 43 deletions
|
@ -48,18 +48,6 @@ 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
|
|
||||||
# Also do nothing if running in Tor Browser.
|
|
||||||
if mightBeTBB()
|
|
||||||
log 'Will not run within Tor Browser.'
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
|
|
||||||
|
|
||||||
###
|
###
|
||||||
Entry point.
|
Entry point.
|
||||||
|
@ -72,7 +60,7 @@ init = (isNode) ->
|
||||||
snowflake = new Snowflake broker, ui
|
snowflake = new Snowflake broker, ui
|
||||||
|
|
||||||
log '== snowflake proxy =='
|
log '== snowflake proxy =='
|
||||||
if snowflakeIsDisabled()
|
if Util.snowflakeIsDisabled()
|
||||||
# Do not activate the proxy if any number of conditions are true.
|
# Do not activate the proxy if any number of conditions are true.
|
||||||
log 'Currently not active.'
|
log 'Currently not active.'
|
||||||
return
|
return
|
||||||
|
|
|
@ -8,7 +8,6 @@ Broker with an WebRTC answer.
|
||||||
###
|
###
|
||||||
|
|
||||||
class ProxyPair
|
class ProxyPair
|
||||||
|
|
||||||
MAX_BUFFER: 10 * 1024 * 1024
|
MAX_BUFFER: 10 * 1024 * 1024
|
||||||
pc: null
|
pc: null
|
||||||
c2rSchedule: []
|
c2rSchedule: []
|
||||||
|
@ -29,7 +28,7 @@ class ProxyPair
|
||||||
###
|
###
|
||||||
constructor: (@relayAddr, @rateLimit) ->
|
constructor: (@relayAddr, @rateLimit) ->
|
||||||
@active = false
|
@active = false
|
||||||
@id = genSnowflakeID()
|
@id = Util.genSnowflakeID()
|
||||||
|
|
||||||
# Prepare a WebRTC PeerConnection and await for an SDP offer.
|
# Prepare a WebRTC PeerConnection and await for an SDP offer.
|
||||||
begin: ->
|
begin: ->
|
||||||
|
|
|
@ -4,26 +4,38 @@ A Coffeescript WebRTC snowflake proxy
|
||||||
Contains helpers for parsing query strings and other utilities.
|
Contains helpers for parsing query strings and other utilities.
|
||||||
###
|
###
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
# It would not be effective for Tor Browser users to run the proxy.
|
@genSnowflakeID: ->
|
||||||
# Do we seem to be running in Tor Browser? Check the user-agent string and for
|
Math.random().toString(36).substring(2)
|
||||||
# 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 = ->
|
@snowflakeIsDisabled = ->
|
||||||
Math.random().toString(36).substring(2)
|
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
|
||||||
|
|
||||||
|
|
||||||
Query =
|
class Query
|
||||||
###
|
###
|
||||||
Parse a URL query string or application/x-www-form-urlencoded body. The
|
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,
|
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.
|
Always decodes from UTF-8, not any other encoding.
|
||||||
http://dev.w3.org/html5/spec/Overview.html#url-encoded-form-data
|
http://dev.w3.org/html5/spec/Overview.html#url-encoded-form-data
|
||||||
###
|
###
|
||||||
parse: (qs) ->
|
@parse: (qs) ->
|
||||||
result = {}
|
result = {}
|
||||||
strings = []
|
strings = []
|
||||||
strings = qs.split '&' if qs
|
strings = qs.split '&' if qs
|
||||||
|
@ -53,7 +65,7 @@ Query =
|
||||||
result
|
result
|
||||||
|
|
||||||
# params is a list of (key, value) 2-tuples.
|
# params is a list of (key, value) 2-tuples.
|
||||||
buildString: (params) ->
|
@buildString: (params) ->
|
||||||
parts = []
|
parts = []
|
||||||
for param in params
|
for param in params
|
||||||
parts.push encodeURIComponent(param[0]) + '=' +
|
parts.push encodeURIComponent(param[0]) + '=' +
|
||||||
|
@ -61,11 +73,11 @@ Query =
|
||||||
parts.join '&'
|
parts.join '&'
|
||||||
|
|
||||||
|
|
||||||
Parse =
|
class Parse
|
||||||
# Parse a cookie data string (usually document.cookie). The return type is an
|
# Parse a cookie data string (usually document.cookie). The return type is an
|
||||||
# object mapping cookies names to values. Returns null on error.
|
# object mapping cookies names to values. Returns null on error.
|
||||||
# http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038
|
# http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-8747038
|
||||||
cookie: (cookies) ->
|
@cookie: (cookies) ->
|
||||||
result = {}
|
result = {}
|
||||||
strings = []
|
strings = []
|
||||||
strings = cookies.split ';' if cookies
|
strings = cookies.split ';' if cookies
|
||||||
|
@ -79,7 +91,7 @@ Parse =
|
||||||
|
|
||||||
# Parse an address in the form 'host:port'. Returns an Object with keys 'host'
|
# Parse an address in the form 'host:port'. Returns an Object with keys 'host'
|
||||||
# (String) and 'port' (int). Returns null on error.
|
# (String) and 'port' (int). Returns null on error.
|
||||||
address: (spec) ->
|
@address: (spec) ->
|
||||||
m = null
|
m = null
|
||||||
# IPv6 syntax.
|
# IPv6 syntax.
|
||||||
m = spec.match(/^\[([\0-9a-fA-F:.]+)\]:([0-9]+)$/) if !m
|
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)
|
# Parse a count of bytes. A suffix of 'k', 'm', or 'g' (or uppercase)
|
||||||
# does what you would think. Returns null on error.
|
# does what you would think. Returns null on error.
|
||||||
byteCount: (spec) ->
|
@byteCount: (spec) ->
|
||||||
UNITS = {
|
UNITS = {
|
||||||
k: 1024, m: 1024 * 1024, g: 1024 * 1024 * 1024
|
k: 1024, m: 1024 * 1024, g: 1024 * 1024 * 1024
|
||||||
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
|
# Parse a connection-address out of the "c=" Connection Data field of a session
|
||||||
# description. Return undefined if none is found.
|
# description. Return undefined if none is found.
|
||||||
# https://tools.ietf.org/html/rfc4566#section-5.7
|
# https://tools.ietf.org/html/rfc4566#section-5.7
|
||||||
ipFromSDP: (sdp) ->
|
@ipFromSDP: (sdp) ->
|
||||||
for pattern in [
|
for pattern in [
|
||||||
/^c=IN IP4 ([\d.]+)(?:(?:\/\d+)?\/\d+)?(:? |$)/m,
|
/^c=IN IP4 ([\d.]+)(?:(?:\/\d+)?\/\d+)?(:? |$)/m,
|
||||||
/^c=IN IP6 ([0-9A-Fa-f:.]+)(?:\/\d+)?(:? |$)/m,
|
/^c=IN IP6 ([0-9A-Fa-f:.]+)(?:\/\d+)?(:? |$)/m,
|
||||||
|
@ -124,8 +136,8 @@ Parse =
|
||||||
return m[1] if m?
|
return m[1] if m?
|
||||||
|
|
||||||
|
|
||||||
Params =
|
class Params
|
||||||
getBool: (query, param, defaultValue) ->
|
@getBool: (query, param, defaultValue) ->
|
||||||
val = query[param]
|
val = query[param]
|
||||||
return defaultValue if undefined == val
|
return defaultValue if undefined == val
|
||||||
return true if 'true' == val || '1' == val || '' == 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
|
# 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
|
# '100' and '1.3m'. Returns |defaultValue| if param is not a key. Return null
|
||||||
# on a parsing error.
|
# on a parsing error.
|
||||||
getByteCount: (query, param, defaultValue) ->
|
@getByteCount: (query, param, defaultValue) ->
|
||||||
spec = query[param]
|
spec = query[param]
|
||||||
return defaultValue if undefined == spec
|
return defaultValue if undefined == spec
|
||||||
Parse.byteCount spec
|
Parse.byteCount spec
|
||||||
|
|
||||||
# Get an object value and parse it as an address spec. Returns |defaultValue|
|
# 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.
|
# if param is not a key. Returns null on a parsing error.
|
||||||
getAddress: (query, param, defaultValue) ->
|
@getAddress: (query, param, defaultValue) ->
|
||||||
val = query[param]
|
val = query[param]
|
||||||
return defaultValue if undefined == val
|
return defaultValue if undefined == val
|
||||||
Parse.address val
|
Parse.address val
|
||||||
|
|
||||||
# Get an object value and return it as a string. Returns default_val if param
|
# Get an object value and return it as a string. Returns default_val if param
|
||||||
# is not a key.
|
# is not a key.
|
||||||
getString: (query, param, defaultValue) ->
|
@getString: (query, param, defaultValue) ->
|
||||||
val = query[param]
|
val = query[param]
|
||||||
return defaultValue if undefined == val
|
return defaultValue if undefined == val
|
||||||
val
|
val
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue