-
Notifications
You must be signed in to change notification settings - Fork 30
/
init-testing.js
125 lines (101 loc) · 3.06 KB
/
init-testing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* global TESTING, Util, Params, Config, UI, Broker, Snowflake */
/*
UI
*/
class DebugUI extends UI {
constructor() {
super();
// Setup other DOM handlers if it's debug mode.
this.$status = document.getElementById('status');
this.$msglog = document.getElementById('msglog');
this.$msglog.value = '';
}
// Status bar
setStatus(msg) {
var txt;
txt = document.createTextNode('Status: ' + msg);
while (this.$status.firstChild) {
this.$status.removeChild(this.$status.firstChild);
}
return this.$status.appendChild(txt);
}
setActive(connected) {
super.setActive(connected);
return this.$msglog.className = connected ? 'active' : '';
}
log(msg) {
// Scroll to latest
this.$msglog.value += msg + '\n';
return this.$msglog.scrollTop = this.$msglog.scrollHeight;
}
}
// DOM elements references.
DebugUI.prototype.$msglog = null;
DebugUI.prototype.$status = null;
/*
Entry point.
*/
var snowflake, query, debug, silenceNotifications, log, dbg, init;
(function() {
if (((typeof TESTING === "undefined" || TESTING === null) || !TESTING) && !Util.featureDetect()) {
console.log('webrtc feature not detected. shutting down');
return;
}
snowflake = null;
query = new URLSearchParams(location.search);
debug = Params.getBool(query, 'debug', false);
silenceNotifications = Params.getBool(query, 'silent', false);
// 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 = function(msg) {
console.log('Snowflake: ' + msg);
return snowflake != null ? snowflake.ui.log(msg) : void 0;
};
dbg = function(msg) {
if (debug || ((snowflake != null ? snowflake.ui : void 0) instanceof DebugUI)) {
return log(msg);
}
};
init = function() {
var broker, config, ui;
config = new Config;
if ('off' !== query['ratelimit']) {
config.rateLimitBytes = Params.getByteCount(query, 'ratelimit', config.rateLimitBytes);
}
ui = null;
if (document.getElementById('status') !== null) {
ui = new DebugUI();
} else {
ui = new UI();
}
broker = new Broker(config.brokerUrl);
snowflake = new Snowflake(config, ui, broker);
log('== snowflake proxy ==');
if (Util.snowflakeIsDisabled(config.cookieName)) {
// Do not activate the proxy if any number of conditions are true.
log('Currently not active.');
return;
}
// Otherwise, begin setting up WebRTC and acting as a proxy.
dbg('Contacting Broker at ' + broker.url);
snowflake.setRelayAddr(config.relayAddr);
return snowflake.beginWebRTC();
};
// Notification of closing tab with active proxy.
window.onbeforeunload = function() {
if (
!silenceNotifications &&
snowflake !== null &&
Snowflake.MODE.WEBRTC_READY === snowflake.state
) {
return Snowflake.MESSAGE.CONFIRMATION;
}
return null;
};
window.onunload = function() {
if (snowflake !== null) { snowflake.disable(); }
return null;
};
window.onload = init;
}());