tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

prepare-tcp-connection.js (1683B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { dumpn } = require("resource://devtools/shared/DevToolsUtils.js");
      8 const {
      9  runCommand,
     10 } = require("resource://devtools/client/shared/remote-debugging/adb/commands/run-command.js");
     11 
     12 // sends adb forward deviceId, localPort and devicePort
     13 const forwardPort = function (deviceId, localPort, devicePort) {
     14  dumpn("forwardPort " + localPort + " -- " + devicePort);
     15  // Send "host-serial:<serial-number>:<request>",
     16  // with <request> set to "forward:<local>;<remote>"
     17  // See https://android.googlesource.com/platform/system/core/+/jb-dev/adb/SERVICES.TXT
     18  return runCommand(
     19    `host-serial:${deviceId}:forward:${localPort};${devicePort}`
     20  ).then(function onSuccess(data) {
     21    return data;
     22  });
     23 };
     24 
     25 const getFreeTCPPort = function () {
     26  const serv = Cc["@mozilla.org/network/server-socket;1"].createInstance(
     27    Ci.nsIServerSocket
     28  );
     29  serv.init(-1, true, -1);
     30  const port = serv.port;
     31  serv.close();
     32  return port;
     33 };
     34 
     35 // Prepare TCP connection for provided device id and socket path.
     36 // The returned value is a port number of localhost for the connection.
     37 const prepareTCPConnection = async function (deviceId, socketPath) {
     38  const port = getFreeTCPPort();
     39  const local = `tcp:${port}`;
     40  const remote = socketPath.startsWith("@")
     41    ? `localabstract:${socketPath.substring(1)}`
     42    : `localfilesystem:${socketPath}`;
     43  await forwardPort(deviceId, local, remote);
     44  return port;
     45 };
     46 exports.prepareTCPConnection = prepareTCPConnection;