[tor-commits] [pluggable-transports/snowflake-webext] 04/06: Simplify WS.makeWebsocket to take just a client_ip.

gitolite role git at cupani.torproject.org
Fri Mar 31 15:20:55 UTC 2023


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 9e14a21cf48f936edaaf6403773f3963ae405602
Author: David Fifield <david at bamsoftware.com>
AuthorDate: Wed Mar 29 12:03:32 2023 -0600

    Simplify WS.makeWebsocket to take just a client_ip.
    
    Rather than a full array of key–value pairs.
---
 proxypair.js           |  8 ++------
 spec/websocket.spec.js | 24 ++++++++++++------------
 websocket.js           | 29 +++++++++++++++--------------
 3 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/proxypair.js b/proxypair.js
index b4dc3c9..bea1fc0 100644
--- a/proxypair.js
+++ b/proxypair.js
@@ -164,12 +164,8 @@ class ProxyPair {
     // are not marked experimental, were undefined when I tried them in Firefox
     // 52.2.0.
     // https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/remoteDescription
-    const peer_ip = Parse.ipFromSDP(remoteDescription.sdp);
-    const params = [];
-    if (peer_ip != null) {
-      params.push(["client_ip", peer_ip]);
-    }
-    const relay = this.relay = WS.makeWebsocket(this.relayURL, params);
+    const clientIP = Parse.ipFromSDP(remoteDescription.sdp);
+    const relay = this.relay = WS.makeWebsocket(this.relayURL, clientIP);
     relay.label = 'websocket-relay';
     relay.onopen = () => {
       clearTimeout(this.connectToRelayTimeoutId);
diff --git a/spec/websocket.spec.js b/spec/websocket.spec.js
index a3c19c1..81722da 100644
--- a/spec/websocket.spec.js
+++ b/spec/websocket.spec.js
@@ -1,25 +1,25 @@
 describe('makeWebsocketURL', function() {
-  it('attaches params', function() {
-    // With empty parameters, should leave input unchanged.
-    expect(WS.makeWebsocketURL('wss://example.com/path', []).toString())
+  it('attaches client_ip', function() {
+    // With no clientIP, should leave query parameters unchanged.
+    expect(WS.makeWebsocketURL('wss://example.com/path', null).toString())
       .toBe('wss://example.com/path');
-    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.99', []).toString())
+    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.99', null).toString())
       .toBe('wss://example.com/path?client_ip=192.0.2.99');
-    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.98&client_ip=192.0.2.99', []).toString())
+    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.98&client_ip=192.0.2.99', null).toString())
       .toBe('wss://example.com/path?client_ip=192.0.2.98&client_ip=192.0.2.99');
 
-    // Should add new parameters.
-    expect(WS.makeWebsocketURL('wss://example.com/path', [['client_ip', '192.0.2.1']]).toString())
+    // Should add a client_ip query parameter.
+    expect(WS.makeWebsocketURL('wss://example.com/path', '192.0.2.1').toString())
       .toBe('wss://example.com/path?client_ip=192.0.2.1');
 
-    // Should retain any other existing parameters.
-    expect(WS.makeWebsocketURL('wss://example.com/path?foo=bar', [['client_ip', '192.0.2.1']]).toString())
+    // Should retain any other existing query parameters.
+    expect(WS.makeWebsocketURL('wss://example.com/path?foo=bar', '192.0.2.1').toString())
       .toBe('wss://example.com/path?foo=bar&client_ip=192.0.2.1');
 
-    // Should overwrite any existing parameters of the same name.
-    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.99', [['client_ip', '192.0.2.1']]).toString())
+    // Should overwrite any existing client_ip query parameters.
+    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.99', '192.0.2.1').toString())
       .toBe('wss://example.com/path?client_ip=192.0.2.1');
-    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.98&client_ip=192.0.2.99', [['client_ip', '192.0.2.1']]).toString())
+    expect(WS.makeWebsocketURL('wss://example.com/path?client_ip=192.0.2.98&client_ip=192.0.2.99', '192.0.2.1').toString())
       .toBe('wss://example.com/path?client_ip=192.0.2.1');
   });
 });
diff --git a/websocket.js b/websocket.js
index f04a505..1fbc68b 100644
--- a/websocket.js
+++ b/websocket.js
@@ -5,28 +5,29 @@ Only websocket-specific stuff.
 // eslint-disable-next-line no-unused-vars
 class WS {
   /**
-   * Creates a websocket URL from a base URL and params to override
+   * Creates a websocket URL from a base URL and an optional client IP address
+   * string.
    * @param {URL|string} url
-   * @param {URLSearchParams|string[][]} params
+   * @param {?string} clientIP
    * @return {URL}
    */
-  static makeWebsocketURL(url, params) {
-    let parsedURL = new URL(url);
-    let urlpa = new URLSearchParams(params);
-    urlpa.forEach(function (value, key) {
-      parsedURL.searchParams.set(key, value);
-    });
-    return parsedURL;
+  static makeWebsocketURL(url, clientIP) {
+    url = new URL(url);
+    if (clientIP != null) {
+      url.searchParams.set('client_ip', clientIP);
+    }
+    return url;
   }
 
   /**
-   * Creates a websocket connection from a URL and params to override
+   * Creates a websocket connection from a URL and an optional client IP address
+   * string.
    * @param {URL|string} url
-   * @param {URLSearchParams|string[][]} params
+   * @param {?string} clientIP
    * @return {WebSocket}
    */
-  static makeWebsocket(url, params) {
-    let ws = new WebSocket(WS.makeWebsocketURL(url, params));
+  static makeWebsocket(url, clientIP) {
+    let ws = new WebSocket(WS.makeWebsocketURL(url, clientIP));
     /*
     'User agents can use this as a hint for how to handle incoming binary data:
     if the attribute is set to 'blob', it is safe to spool it to disk, and if it
@@ -42,7 +43,7 @@ class WS {
    */
   static probeWebsocket(addr) {
     return /** @type {Promise<void>} */(new Promise((resolve, reject) => {
-      const ws = WS.makeWebsocket(addr, []);
+      const ws = WS.makeWebsocket(addr, null);
       ws.onopen = () => {
         resolve();
         ws.close();

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


More information about the tor-commits mailing list