[tor-commits] [snowflake-webext/master] Advertise suspected NAT type to broker

cohosh at torproject.org cohosh at torproject.org
Mon Jul 6 17:27:50 UTC 2020


commit 60beedf8ddc2e322e5d668bfeafcdfa4ccd0a9d5
Author: Cecylia Bocovich <cohosh at torproject.org>
Date:   Wed Jun 17 10:18:24 2020 -0400

    Advertise suspected NAT type to broker
    
    This compliments the poll the rate adjustment on proxy failures to
    address proxy matching issues. Since we cannot determine our NAT type
    directly for browser-based proxies, we assume a restricted NAT type if
    the datachannel fails to open.
---
 broker.js              |  8 +++++++-
 init-webext.js         |  9 +++++++++
 package.json           |  2 +-
 snowflake.js           |  4 ++++
 spec/snowflake.spec.js | 12 +++++-------
 5 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/broker.js b/broker.js
index 84f0929..e1aab0f 100644
--- a/broker.js
+++ b/broker.js
@@ -17,10 +17,12 @@ class Broker {
   constructor(config) {
     this.getClientOffer = this.getClientOffer.bind(this);
     this._postRequest = this._postRequest.bind(this);
+    this.setNATType = this.setNATType.bind(this);
 
     this.config = config;
     this.url = config.brokerUrl;
     this.clients = 0;
+    this.natType = "unknown";
     if (0 === this.url.indexOf('localhost', 0)) {
       // Ensure url has the right protocol + trailing slash.
       this.url = 'http://' + this.url;
@@ -64,7 +66,7 @@ class Broker {
         }
       };
       this._xhr = xhr; // Used by spec to fake async Broker interaction
-      var data = {"Version": "1.1", "Sid": id, "Type": this.config.proxyType};
+      var data = {"Version": "1.1", "Sid": id, "Type": this.config.proxyType, "NAT": this.natType};
       return this._postRequest(xhr, 'proxy', JSON.stringify(data));
     });
   }
@@ -93,6 +95,10 @@ class Broker {
     return this._postRequest(xhr, 'answer', JSON.stringify(data));
   }
 
+  setNATType(natType) {
+    this.natType = natType;
+  }
+
   // urlSuffix for the broker is different depending on what action
   // is desired.
   _postRequest(xhr, urlSuffix, payload) {
diff --git a/init-webext.js b/init-webext.js
index 4b09988..3d7b1ea 100644
--- a/init-webext.js
+++ b/init-webext.js
@@ -13,6 +13,7 @@ class WebExtUI extends UI {
     this.onMessage = this.onMessage.bind(this);
     this.onDisconnect = this.onDisconnect.bind(this);
     this.initStats();
+    this.initNATType();
     chrome.runtime.onConnect.addListener(this.onConnect);
   }
 
@@ -25,6 +26,14 @@ class WebExtUI extends UI {
     }), 60 * 60 * 1000);
   }
 
+  initNATType() {
+    this.natType = "unknown";
+    // reset NAT type every 24 hours in case proxy location changed
+    setInterval((() => {
+      this.natType = "unknown";
+    }), 24 * 60 * 60 * 1000);
+  }
+
   tryProbe() {
     WS.probeWebsocket(config.relayAddr)
     .then(
diff --git a/package.json b/package.json
index d42a138..215e777 100644
--- a/package.json
+++ b/package.json
@@ -32,4 +32,4 @@
     "ws": "^3.3.1",
     "xmlhttprequest": "^1.8.0"
   }
-}
\ No newline at end of file
+}
diff --git a/snowflake.js b/snowflake.js
index 1021c78..91a8d87 100644
--- a/snowflake.js
+++ b/snowflake.js
@@ -21,6 +21,7 @@ class Snowflake {
     this.config = config;
     this.ui = ui;
     this.broker = broker;
+    this.broker.setNATType(ui.natType);
     this.proxyPairs = [];
     this.pollInterval = this.config.defaultBrokerPollInterval;
     if (void 0 === this.config.rateLimitBytes) {
@@ -80,6 +81,9 @@ class Snowflake {
           this.pollInterval =
                 Math.min(this.pollInterval + this.config.pollAdjustment,
                   this.config.slowestBrokerPollInterval);
+          // assume restricted NAT
+          this.ui.natType = "restricted";
+          this.broker.setNATType(this.ui.natType);
         } else {
           // decrease poll interval
           this.pollInterval =
diff --git a/spec/snowflake.spec.js b/spec/snowflake.spec.js
index 970947b..d9b97e8 100644
--- a/spec/snowflake.spec.js
+++ b/spec/snowflake.spec.js
@@ -36,26 +36,24 @@ class FakeBroker {
       return {};
     });
   }
+  setNATType(natType) {
+  }
 }
 
 describe('Snowflake', function() {
 
   it('constructs correctly', function() {
     var s;
-    s = new Snowflake(config, ui, {
-      fake: 'broker'
-    });
+    s = new Snowflake(config, ui, new FakeBroker());
     expect(s.rateLimit).not.toBeNull();
-    expect(s.broker).toEqual({
-      fake: 'broker'
-    });
+    expect(s.broker).toEqual(new FakeBroker());
     expect(s.ui).not.toBeNull();
     expect(s.retries).toBe(0);
   });
 
   it('sets relay address correctly', function() {
     var s;
-    s = new Snowflake(config, ui, null);
+    s = new Snowflake(config, ui, new FakeBroker());
     s.setRelayAddr('foo');
     expect(s.relayAddr).toEqual('foo');
   });





More information about the tor-commits mailing list