[tor-commits] [snowflake-webext/master] Fix a bug in NAT update process

cohosh at torproject.org cohosh at torproject.org
Wed Jan 27 17:13:28 UTC 2021


commit 87cdf4546c126151bcc6610386f6ea9dcc885607
Author: Cecylia Bocovich <cohosh at torproject.org>
Date:   Thu Jan 21 17:12:44 2021 -0500

    Fix a bug in NAT update process
    
    This fixes two bugs in the NAT update process:
    - one bug wasn't updating the NAT type we advertize to the broker
    - one bug passed an already evaluated function to setTimeout
---
 init-badge.js  | 33 +++++++++++++++++----------------
 init-webext.js | 21 +++++++++++----------
 snowflake.js   |  3 +++
 3 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/init-badge.js b/init-badge.js
index 6233b10..3de6159 100644
--- a/init-badge.js
+++ b/init-badge.js
@@ -38,6 +38,21 @@ class BadgeUI extends UI {
     );
   }
 
+  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) {
@@ -105,7 +120,7 @@ function getLang() {
   return defaultLang;
 }
 
-var debug, snowflake, config, broker, ui, log, dbg, init, update, initNATType, silenceNotifications, query, tryProbe;
+var debug, snowflake, config, broker, ui, log, dbg, init, update, silenceNotifications, query, tryProbe;
 
 (function() {
 
@@ -147,20 +162,6 @@ var debug, snowflake, config, broker, ui, log, dbg, init, update, initNATType, s
     );
   };
 
-  initNATType = function() {
-    this.natType = "unknown";
-    (function loop(_this) {
-      Util.checkNATType(config.datachannelTimeout).then((type) => {
-        console.log("Setting NAT type: " + type);
-        _this.natType = type;
-      }).catch((e) => {
-        console.log(e);
-      });
-      // reset NAT type every 24 hours in case proxy location changed
-      setTimeout(_this.initNATType, config.natCheckInterval);
-    })(this);
-  };
-
   update = function() {
     const cookies = Parse.cookie(document.cookie);
     if (cookies[COOKIE_NAME] !== '1') {
@@ -196,7 +197,7 @@ var debug, snowflake, config, broker, ui, log, dbg, init, update, initNATType, s
     log('== snowflake proxy ==');
     update();
 
-    initNATType();
+    ui.initNATType();
   };
 
   // Notification of closing tab with active proxy.
diff --git a/init-webext.js b/init-webext.js
index d9a37d0..3b7decf 100644
--- a/init-webext.js
+++ b/init-webext.js
@@ -25,18 +25,19 @@ class WebExtUI extends UI {
     }), 60 * 60 * 1000);
   }
 
+  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";
-    (function loop(_this) {
-      Util.checkNATType(config.datachannelTimeout).then((type) => {
-        console.log("Setting NAT type: " + type);
-        _this.natType = type;
-      }).catch((e) => {
-        console.log(e);
-      });
-      // reset NAT type every 2 days in case proxy location changed
-      setTimeout(_this.initNATType, config.natCheckInterval);
-    })(this);
+    this.checkNAT();
+    return setInterval(() => {this.checkNAT();}, config.natCheckInterval);
   }
 
   tryProbe() {
diff --git a/snowflake.js b/snowflake.js
index 091e046..9ac5fad 100644
--- a/snowflake.js
+++ b/snowflake.js
@@ -68,6 +68,9 @@ class Snowflake {
       msg += '[retries: ' + this.retries + ']';
     }
     this.ui.setStatus(msg);
+    //update NAT type
+    console.log("NAT type: "+ this.ui.natType);
+    this.broker.setNATType(this.ui.natType);
     recv = this.broker.getClientOffer(pair.id);
     recv.then((resp) => {
       var clientNAT = resp.NAT;





More information about the tor-commits mailing list