-
Notifications
You must be signed in to change notification settings - Fork 3
/
init-badge.js
239 lines (198 loc) · 5.13 KB
/
init-badge.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* global Util, Params, Config, UI, Broker, Snowflake, Popup, Parse, availableLangs, WS */
/*
UI
*/
class Messages {
constructor(json) {
this.json = json;
}
getMessage(m, ...rest) {
let message = this.json[m].message;
return message.replace(/\$(\d+)/g, (...args) => {
return rest[Number(args[1]) - 1];
});
}
}
let messages = null;
class BadgeUI extends UI {
constructor() {
super();
this.popup = new Popup(
(...args) => messages.getMessage(...args),
(event) => {
if (event.target.checked) {
setSnowflakeCookie('1', COOKIE_LIFETIME);
} else {
setSnowflakeCookie('', COOKIE_EXPIRE);
}
update();
},
() => {
tryProbe();
}
);
}
checkNAT() {
Util.checkNATType(config.datachannelTimeout).then((type) => {
console.log("Setting NAT type: " + type);
this.natType = type;
}).catch((e) => {
console.log(e);
});
}
initNATType() {
this.natType = "unknown";
this.checkNAT();
return setInterval(() => {this.checkNAT();}, config.natCheckInterval);
}
setStatus() {}
missingFeature(missing) {
this.setIcon('off');
this.popup.missingFeature(missing);
}
turnOn() {
this.enabled = true;
if (this.clients > 0) {
this.setIcon('running');
} else {
this.setIcon('on');
}
const total = this.stats.reduce((function(t, c) {
return t + c;
}), 0);
this.popup.turnOn(this.clients, total);
}
turnOff() {
this.enabled = false;
this.setIcon('off');
this.popup.turnOff();
}
postActive() {
if(this.enabled) {
this.turnOn();
}
}
setIcon(status) {
document.getElementById('icon').href = `assets/toolbar-${status}.ico`;
}
}
BadgeUI.prototype.popup = null;
/*
Entry point.
*/
// Defaults to opt-in.
var COOKIE_NAME = "snowflake-allow";
var COOKIE_LIFETIME = "Thu, 01 Jan 2038 00:00:00 GMT";
var COOKIE_EXPIRE = "Thu, 01 Jan 1970 00:00:01 GMT";
function setSnowflakeCookie(val, expires) {
document.cookie = `${COOKIE_NAME}=${val}; path=/; expires=${expires}; secure=true; samesite=none;`;
}
const defaultLang = 'en_US';
// Resolve as in,
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization#Localized_string_selection
function getLang() {
let lang = navigator.language || defaultLang;
lang = lang.replace(/-/g, '_');
if (availableLangs.has(lang)) {
return lang;
}
lang = lang.split('_')[0];
if (availableLangs.has(lang)) {
return lang;
}
return defaultLang;
}
var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotifications, query, tryProbe;
(function() {
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) { log(msg); }
};
tryProbe = function() {
WS.probeWebsocket(config.relayAddr)
.then(
() => {
ui.turnOn();
dbg('Contacting Broker at ' + broker.url);
log('Starting snowflake');
snowflake.setRelayAddr(config.relayAddr);
snowflake.beginWebRTC();
},
() => {
ui.missingFeature('popupBridgeUnreachable');
snowflake.disable();
log('Could not connect to bridge.');
}
);
};
update = function() {
const cookies = Parse.cookie(document.cookie);
if (cookies[COOKIE_NAME] !== '1') {
ui.turnOff();
snowflake.disable();
log('Currently not active.');
return;
}
if (!Util.hasWebRTC()) {
ui.missingFeature('popupWebRTCOff');
snowflake.disable();
return;
}
tryProbe();
};
init = function() {
ui = new BadgeUI();
if (!Util.hasCookies()) {
ui.missingFeature('badgeCookiesOff');
return;
}
config = new Config("badge");
if ('off' !== query.get('ratelimit')) {
config.rateLimitBytes = Params.getByteCount(query, 'ratelimit', config.rateLimitBytes);
}
broker = new Broker(config);
snowflake = new Snowflake(config, ui, broker);
log('== snowflake proxy ==');
update();
ui.initNATType();
};
// Notification of closing tab with active proxy.
window.onbeforeunload = function() {
if (
!silenceNotifications &&
snowflake !== null &&
ui.active
) {
return Snowflake.MESSAGE.CONFIRMATION;
}
return null;
};
window.onunload = function() {
if (snowflake !== null) { snowflake.disable(); }
return null;
};
window.onload = function() {
fetch(`./_locales/${getLang()}/messages.json`)
.then((res) => {
if (!res.ok) { return; }
return res.json();
})
.then((json) => {
messages = new Messages(json);
Popup.fill(document.body, (m) => {
return messages.getMessage(m);
});
init();
});
};
}());