init-testing.js 3.0 KB

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