Verified Commit b2cd0ee8 authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

fixup! Bug 40933: Add tor-launcher functionality

Small improvements on event registration.
parent 2cde9fc3
Loading
Loading
Loading
Loading
+54 −57
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ const ControlConnTimings = Object.freeze({
 */
export const TorMonitorService = {
  _connection: null,
  _eventsToMonitor: Object.freeze(["STATUS_CLIENT", "NOTICE", "WARN", "ERR"]),
  _eventHandlers: {},
  _torLog: [], // Array of objects with date, type, and msg properties.
  _startTimeout: null,

@@ -72,6 +72,17 @@ export const TorMonitorService = {
      return;
    }
    this._inited = true;

    this._eventHandlers = new Map([
      [
        "STATUS_CLIENT",
        (_eventType, lines) => this._processBootstrapStatus(lines[0], false),
      ],
      ["NOTICE", this._processLog.bind(this)],
      ["WARN", this._processLog.bind(this)],
      ["ERR", this._processLog.bind(this)],
    ]);

    if (this.ownsTorDaemon) {
      this._controlTor();
    } else {
@@ -272,7 +283,7 @@ export const TorMonitorService = {

    // TODO: optionally monitor INFO and DEBUG log messages.
    let reply = await conn.sendCommand(
      "SETEVENTS " + this._eventsToMonitor.join(" ")
      "SETEVENTS " + Array.from(this._eventHandlers.keys()).join(" ")
    );
    reply = TorParsers.parseCommandResponse(reply);
    if (!TorParsers.commandSucceeded(reply)) {
@@ -297,7 +308,11 @@ export const TorMonitorService = {
    }

    this._connection = conn;
    this._waitForEventData();

    for (const [type, callback] of this._eventHandlers.entries()) {
      this._monitorEvent(type, callback);
    }

    return true;
  },

@@ -318,15 +333,11 @@ export const TorMonitorService = {
    }
  },

  _waitForEventData() {
    if (!this._connection) {
      return;
    }
    logger.debug("Start watching events:", this._eventsToMonitor);
  _monitorEvent(type, callback) {
    logger.debug(`Watching events of type ${type}.`);
    let replyObj = {};
    for (const torEvent of this._eventsToMonitor) {
    this._connection.watchEvent(
        torEvent,
      type,
      null,
      line => {
        if (!line) {
@@ -334,49 +345,33 @@ export const TorMonitorService = {
        }
        logger.debug("Event response: ", line);
        const isComplete = TorParsers.parseReplyLine(line, replyObj);
          if (isComplete) {
            this._processEventReply(replyObj);
            replyObj = {};
          }
        },
        true
      );
    }
  },

  _processEventReply(aReply) {
    if (aReply._parseError || !aReply.lineArray.length) {
        if (!isComplete || replyObj._parseError || !replyObj.lineArray.length) {
          return;
        }

    if (aReply.statusCode !== TorStatuses.EventNotification) {
      logger.warn("Unexpected event status code:", aReply.statusCode);
      return;
    }

    // TODO: do we need to handle multiple lines?
    const s = aReply.lineArray[0];
    const idx = s.indexOf(" ");
    if (idx === -1) {
        const reply = replyObj;
        replyObj = {};
        if (reply.statusCode !== TorStatuses.EventNotification) {
          logger.error("Unexpected event status code:", reply.statusCode);
          return;
        }
    const eventType = s.substring(0, idx);
    const msg = s.substring(idx + 1).trim();

    if (eventType === "STATUS_CLIENT") {
      this._processBootstrapStatus(msg, false);
      return;
    } else if (!this._eventsToMonitor.includes(eventType)) {
      logger.debug(`Dropping unlistened event ${eventType}`);
        if (!reply.lineArray[0].startsWith(`${type} `)) {
          logger.error("Wrong format for the first line:", reply.lineArray[0]);
          return;
        }
        reply.lineArray[0] = reply.lineArray[0].substring(type.length + 1);
        callback(type, reply.lineArray);
      },
      true
    );
  },

    if (eventType === "WARN" || eventType === "ERR") {
  _processLog(type, lines) {
    if (type === "WARN" || type === "ERR") {
      // Notify so that Copy Log can be enabled.
      Services.obs.notifyObservers(null, TorTopics.HasWarnOrErr);
    }

    const now = new Date();
    const date = new Date();
    const maxEntries = Services.prefs.getIntPref(
      "extensions.torlauncher.max_tor_log_entries",
      1000
@@ -384,8 +379,10 @@ export const TorMonitorService = {
    if (maxEntries > 0 && this._torLog.length >= maxEntries) {
      this._torLog.splice(0, 1);
    }
    this._torLog.push({ date: now, type: eventType, msg });
    const logString = `Tor ${eventType}: ${msg}`;

    const msg = lines.join("\n");
    this._torLog.push({ date, type, msg });
    const logString = `Tor ${type}: ${msg}`;
    logger.info(logString);
  },

+6 −6
Original line number Diff line number Diff line
@@ -181,12 +181,12 @@ export const TorParsers = Object.freeze({
      return aStr;
    }
    const escaped = aStr
      .replace("\\", "\\\\")
      .replace('"', '\\"')
      .replace("\n", "\\n")
      .replace("\r", "\\r")
      .replace("\t", "\\t")
      .replace(/[^\x20-\x7e]+/g, text => {
      .replaceAll("\\", "\\\\")
      .replaceAll('"', '\\"')
      .replaceAll("\n", "\\n")
      .replaceAll("\r", "\\r")
      .replaceAll("\t", "\\t")
      .replaceAll(/[^\x20-\x7e]+/g, text => {
        const encoder = new TextEncoder();
        return Array.from(
          encoder.encode(text),