Reimagine the badge

Trac 27385
This commit is contained in:
Arlo Breault 2019-07-14 09:41:51 +02:00 committed by Cecylia Bocovich
parent 0bded511b9
commit e60f22833a
29 changed files with 401 additions and 351 deletions

View file

@ -1,18 +1,71 @@
/* global TESTING, Util, Params, Config, DebugUI, BadgeUI, UI, Broker, Snowflake */
/*
UI
*/
class BadgeUI extends UI {
constructor() {
super();
this.popup = new Popup();
}
setStatus() {}
missingFeature(missing) {
this.popup.setImgSrc('off');
this.popup.setStatusText("Snowflake is off");
this.popup.setStatusDesc(missing, 'firebrick');
this.popup.hideButton();
}
turnOn() {
const clients = this.active ? 1 : 0;
this.popup.setChecked(true);
this.popup.setToggleText('Turn Off');
this.popup.setStatusText(`${clients} client${(clients !== 1) ? 's' : ''} connected.`);
// FIXME: Share stats from webext
const total = 0;
this.popup.setStatusDesc(`Your snowflake has helped ${total} user${(total !== 1) ? 's' : ''} circumvent censorship in the last 24 hours.`);
this.popup.setImgSrc(this.active ? "running" : "on");
}
turnOff() {
this.popup.setChecked(false);
this.popup.setToggleText('Turn On');
this.popup.setStatusText("Snowflake is off");
this.popup.setStatusDesc("");
this.popup.setImgSrc("off");
}
setActive(connected) {
super.setActive(connected);
turnOn();
}
}
BadgeUI.prototype.popup = null;
/*
Entry point.
*/
var snowflake, query, debug, silenceNotifications, log, dbg, init;
// 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};`;
}
var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotifications, query;
(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);
@ -35,32 +88,56 @@ var snowflake, query, debug, silenceNotifications, log, dbg, init;
}
};
update = function() {
const cookies = Parse.cookie(document.cookie);
if (cookies[COOKIE_NAME] === '1') {
ui.turnOn();
dbg('Contacting Broker at ' + broker.url);
log('Starting snowflake');
snowflake.setRelayAddr(config.relayAddr);
snowflake.beginWebRTC();
} else {
ui.turnOff();
snowflake.disable();
log('Currently not active.');
}
};
init = function() {
var broker, config, ui;
ui = new BadgeUI();
if (!Util.hasWebRTC()) {
ui.missingFeature("WebRTC feature is not detected.");
return;
}
if (!Util.hasCookies()) {
ui.missingFeature("Cookies are not enabled.");
return;
}
if (Util.mightBeTBB()) {
ui.missingFeature("Will not run within Tor Browser.");
return;
}
config = new Config;
if ('off' !== query.get('ratelimit')) {
config.rateLimitBytes = Params.getByteCount(query, 'ratelimit', config.rateLimitBytes);
}
ui = null;
if (document.getElementById('badge') !== null) {
ui = new BadgeUI();
} else 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();
update();
document.getElementById('enabled').addEventListener('change', (event) => {
if (event.target.checked) {
setSnowflakeCookie('1', COOKIE_LIFETIME);
} else {
setSnowflakeCookie('', COOKIE_EXPIRE);
}
update();
})
};
// Notification of closing tab with active proxy.