[tor-commits] [snowflake-webext/main] Close peer connection for NAT check

cohosh at torproject.org cohosh at torproject.org
Thu Jan 27 22:55:43 UTC 2022


commit bdc5bab098747af3e6e56ecd37fae2bc13b4f343
Author: Cecylia Bocovich <cohosh at torproject.org>
Date:   Mon Jan 17 18:56:47 2022 -0500

    Close peer connection for NAT check
    
    Make sure we close the data channel and peer connection created for our
    NAT check, regardless of the outcome.
---
 util.js | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/util.js b/util.js
index adaef30..710ad51 100644
--- a/util.js
+++ b/util.js
@@ -21,29 +21,31 @@ class Util {
     return navigator.cookieEnabled;
   }
 
-  // returns a promise that fullfills to "restricted" if we
+  // returns a promise that resolves to "restricted" if we
   // fail to make a test connection to a known restricted
-  // NAT, "unrestricted" if the test connection fails, and
+  // NAT, "unrestricted" if the test connection succeeds, and
   // "unknown" if we fail to reach the probe test server
   static checkNATType(timeout) {
-    return new Promise((fulfill, reject) => {
+    let pc = new RTCPeerConnection({iceServers: [
+      {urls: 'stun:stun1.l.google.com:19302'}
+    ]});
+    let channel = pc.createDataChannel("NAT test");
+    return (new Promise((fulfill, reject) => {
       let open = false;
-      let pc = new RTCPeerConnection({iceServers: [
-        {urls: 'stun:stun1.l.google.com:19302'}
-      ]});
-      let channel = pc.createDataChannel("NAT test");
       channel.onopen = function() {
         open = true;
         fulfill("unrestricted");
-        channel.close();
-        pc.close();
       };
       pc.onicecandidate = (evt) => {
         if (evt.candidate == null) {
           //ice gathering is finished
           Util.sendOffer(pc.localDescription)
           .then((answer) => {
-            setTimeout(() => {if(!open) fulfill("restricted");}, timeout);
+            setTimeout(() => {
+              if(!open) {
+                fulfill("restricted");
+              }
+            }, timeout);
             pc.setRemoteDescription(JSON.parse(answer));
           }).catch((e) => {
             console.log(e);
@@ -57,7 +59,10 @@ class Util {
         console.log(e);
         reject("Error creating offer for probetest");
       });
-    });
+    }).finally(() => {
+      channel.close();
+      pc.close();
+    }));
   }
 
   // Assumes getClientOffer happened, and a WebRTC SDP answer has been generated.



More information about the tor-commits mailing list