[tor-commits] [pluggable-transports/snowflake-webext] 01/02: allow multiple clients

gitolite role git at cupani.torproject.org
Thu Mar 24 17:03:54 UTC 2022


This is an automated email from the git hooks/post-receive script.

cohosh pushed a commit to branch main
in repository pluggable-transports/snowflake-webext.

commit b3636e5a6af43d2543917e1c1f299377b61fac52
Author: 5eba <5eba at untiy-mail.de>
AuthorDate: Sat Mar 19 11:08:32 2022 +0100

    allow multiple clients
---
 init-badge.js     |  8 +++-----
 init-testing.js   | 11 ++++++++---
 init-webext.js    |  7 +------
 proxypair.js      | 14 +++++++++++---
 spec/init.spec.js |  6 +++---
 spec/ui.spec.js   |  4 ++--
 ui.js             | 24 +++++++++++++++++++-----
 webext/embed.js   |  4 +---
 8 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/init-badge.js b/init-badge.js
index da50045..f4dd7ff 100644
--- a/init-badge.js
+++ b/init-badge.js
@@ -62,8 +62,7 @@ class BadgeUI extends UI {
 
   turnOn() {
     this.enabled = true;
-    const clients = this.active ? 1 : 0;
-    if (clients > 0) {
+    if (this.clients > 0) {
       this.setIcon('running');
     } else {
       this.setIcon('on');
@@ -71,7 +70,7 @@ class BadgeUI extends UI {
     const total = this.stats.reduce((function(t, c) {
       return t + c;
     }), 0);
-    this.popup.turnOn(clients, total);
+    this.popup.turnOn(this.clients, total);
   }
 
   turnOff() {
@@ -80,8 +79,7 @@ class BadgeUI extends UI {
     this.popup.turnOff();
   }
 
-  setActive(connected) {
-    super.setActive(connected);
+  postActive() {
     if(this.enabled) {
       this.turnOn();
     }
diff --git a/init-testing.js b/init-testing.js
index 01b6147..7724004 100644
--- a/init-testing.js
+++ b/init-testing.js
@@ -24,9 +24,14 @@ class DebugUI extends UI {
     return this.$status.appendChild(txt);
   }
 
-  setActive(connected) {
-    super.setActive(connected);
-    return this.$msglog.className = connected ? 'active' : '';
+  increaseClients() {
+    super.increaseClients();
+    return this.$msglog.className = this.active ? 'active' : '';
+  }
+
+  decreaseClients() {
+    super.decreaseClients();
+    return this.$msglog.className = this.active ? 'active' : '';
   }
 
   log(msg) {
diff --git a/init-webext.js b/init-webext.js
index d527bb8..6c13e7f 100644
--- a/init-webext.js
+++ b/init-webext.js
@@ -76,7 +76,7 @@ class WebExtUI extends UI {
     this.setIcon();
     if (!this.port) { return; }
     this.port.postMessage({
-      active: this.active,
+      clients: this.clients,
       total: this.stats.reduce((function(t, c) {
         return t + c;
       }), 0),
@@ -111,11 +111,6 @@ class WebExtUI extends UI {
     this.port = null;
   }
 
-  setActive(connected) {
-    super.setActive(connected);
-    this.postActive();
-  }
-
   setEnabled(enabled) {
     this.enabled = enabled;
     this.postActive();
diff --git a/proxypair.js b/proxypair.js
index 93efd5a..4066c02 100644
--- a/proxypair.js
+++ b/proxypair.js
@@ -31,6 +31,7 @@ class ProxyPair {
     this.id = Util.genSnowflakeID();
     this.c2rSchedule = [];
     this.r2cSchedule = [];
+    this.counted = false;
   }
 
   // Prepare a WebRTC PeerConnection and await for an SDP offer.
@@ -74,7 +75,8 @@ class ProxyPair {
   prepareDataChannel(channel) {
     channel.onopen = () => {
       log('WebRTC DataChannel opened!');
-      snowflake.ui.setActive(true);
+      snowflake.ui.increaseClients();
+      this.counted = true;
       // This is the point when the WebRTC datachannel is done, so the next step
       // is to establish websocket to the server.
       return this.connectRelay();
@@ -82,7 +84,10 @@ class ProxyPair {
     channel.onclose = () => {
       log('WebRTC DataChannel closed.');
       snowflake.ui.setStatus('disconnected by webrtc.');
-      snowflake.ui.setActive(false);
+      if(this.counted) {
+        snowflake.ui.decreaseClients();
+        this.counted = false;
+      }
       this.flush();
       return this.close();
     };
@@ -122,7 +127,10 @@ class ProxyPair {
     this.relay.onclose = () => {
       log(relay.label + ' closed.');
       snowflake.ui.setStatus('disconnected.');
-      snowflake.ui.setActive(false);
+      if(this.counted) {
+        snowflake.ui.decreaseClients();
+        this.counted = false;
+      }
       this.flush();
       return this.close();
     };
diff --git a/spec/init.spec.js b/spec/init.spec.js
index 593add9..77d4bad 100644
--- a/spec/init.spec.js
+++ b/spec/init.spec.js
@@ -13,11 +13,11 @@ describe('Init', function() {
 
   it('gives a dialog when closing, only while active', function() {
     silenceNotifications = false;
-    ui.setActive(true);
+    ui.increaseClients();
     var msg = window.onbeforeunload();
     expect(ui.active).toBe(true);
     expect(msg).toBe(Snowflake.MESSAGE.CONFIRMATION);
-    ui.setActive(false);
+    ui.decreaseClients();
     msg = window.onbeforeunload();
     expect(ui.active).toBe(false);
     expect(msg).toBe(null);
@@ -25,7 +25,7 @@ describe('Init', function() {
 
   it('does not give a dialog when silent flag is on', function() {
     silenceNotifications = true;
-    ui.setActive(true);
+    ui.increaseClients();
     var msg = window.onbeforeunload();
     expect(ui.active).toBe(true);
     expect(msg).toBe(null);
diff --git a/spec/ui.spec.js b/spec/ui.spec.js
index dc9aa35..e860a17 100644
--- a/spec/ui.spec.js
+++ b/spec/ui.spec.js
@@ -46,9 +46,9 @@ describe('UI', function() {
   it('sets message log css correctly for debug mode', function() {
     var u;
     u = new DebugUI();
-    u.setActive(true);
+    u.increaseClients();
     expect(u.$msglog.className).toEqual('active');
-    u.setActive(false);
+    u.decreaseClients();
     expect(u.$msglog.className).toEqual('');
   });
 
diff --git a/ui.js b/ui.js
index f1d846a..de8670d 100644
--- a/ui.js
+++ b/ui.js
@@ -19,16 +19,30 @@ class UI {
 
   setStatus() {}
 
-  setActive(connected) {
-    if (connected) {
-      this.stats[0] += 1;
+  get active() {
+    return this.clients > 0;
+  }
+
+  postActive() {}
+
+  increaseClients() {
+    this.clients += 1;
+    this.postActive();
+    return this.clients;
+  }
+
+  decreaseClients() {
+    this.clients -= 1;
+    if(this.clients < 0) {
+      this.clients = 0;
     }
-    return this.active = connected;
+    this.postActive();
+    return this.clients;
   }
 
   log() {}
 
 }
 
-UI.prototype.active = false;
+UI.prototype.clients = 0;
 UI.prototype.stats = null;
diff --git a/webext/embed.js b/webext/embed.js
index c17f602..c108f82 100644
--- a/webext/embed.js
+++ b/webext/embed.js
@@ -12,7 +12,7 @@ const port = chrome.runtime.connect({
 });
 
 port.onMessage.addListener((m) => {
-  const { active, enabled, total, missingFeature } = m;
+  const { clients, enabled, total, missingFeature } = m;
   const popup = new Popup(
     (...args) => chrome.i18n.getMessage(...args),
     (event) => port.postMessage({ enabled: event.target.checked }),
@@ -24,8 +24,6 @@ port.onMessage.addListener((m) => {
     return;
   }
 
-  const clients = active ? 1 : 0;
-
   if (enabled) {
     popup.turnOn(clients, total);
   } else {

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the tor-commits mailing list