tor-browser

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

adb-socket.js (1922B)


      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 
      9 function createTCPSocket(location, port, options) {
     10  const { TCPSocket } = Cu.getGlobalForObject(
     11    ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs")
     12  );
     13 
     14  return new TCPSocket(location, port, options);
     15 }
     16 
     17 // Creates a socket connected to the adb instance.
     18 // This instantiation is sync, and returns before we know if opening the
     19 // connection succeeds. Callers must attach handlers to the s field.
     20 class AdbSocket {
     21  constructor() {
     22    this.s = createTCPSocket("127.0.0.1", 5037, { binaryType: "arraybuffer" });
     23  }
     24 
     25  /**
     26   * Dump the first few bytes of the given array to the console.
     27   *
     28   * @param {TypedArray} inputArray
     29   *        the array to dump
     30   */
     31  _hexdump(inputArray) {
     32    const decoder = new TextDecoder("windows-1252");
     33    const array = new Uint8Array(inputArray.buffer);
     34    const s = decoder.decode(array);
     35    const len = array.length;
     36    let dbg = "len=" + len + " ";
     37    const l = len > 20 ? 20 : len;
     38 
     39    for (let i = 0; i < l; i++) {
     40      let c = array[i].toString(16);
     41      if (c.length == 1) {
     42        c = "0" + c;
     43      }
     44      dbg += c;
     45    }
     46    dbg += " ";
     47    for (let i = 0; i < l; i++) {
     48      const c = array[i];
     49      if (c < 32 || c > 127) {
     50        dbg += ".";
     51      } else {
     52        dbg += s[i];
     53      }
     54    }
     55    dumpn(dbg);
     56  }
     57 
     58  // debugging version of tcpsocket.send()
     59  send(array) {
     60    this._hexdump(array);
     61 
     62    this.s.send(array.buffer, array.byteOffset, array.byteLength);
     63  }
     64 
     65  close() {
     66    if (this.s.readyState === "open" || this.s.readyState === "connecting") {
     67      this.s.close();
     68    }
     69  }
     70 }
     71 
     72 exports.AdbSocket = AdbSocket;