Verified Commit 82c7fa8e authored by henry's avatar henry Committed by ma1
Browse files

fixup! TB 40933: Add tor-launcher functionality

TB 42300: Store TorProvider log messages in TorProviderBuilder to be
used between instances and to be available whilst a provider is not
available.
parent 59205899
Loading
Loading
Loading
Loading
+0 −34
Changes for toolkit/components/tor-launcher/TorProvider.sys.mjs: 0 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -45,12 +45,6 @@ const logger = console.createInstance({
 * @property {string} [host] The host to connect for a TCP proxy
 * @property {number} [port] The port number to use for a TCP proxy
 */
/**
 * @typedef {object} LogEntry An object with a log message
 * @property {string} timestamp The local date-time stamp at which we received the message
 * @property {string} type The message level
 * @property {string} msg The message
 */
/**
 * Stores the data associated with a circuit node.
 *
@@ -69,7 +63,6 @@ const Preferences = Object.freeze({
  ControlUseIpc: "extensions.torlauncher.control_port_use_ipc",
  ControlHost: "extensions.torlauncher.control_host",
  ControlPort: "extensions.torlauncher.control_port",
  MaxLogEntries: "extensions.torlauncher.max_tor_log_entries",
});

/* Config Keys used to configure tor daemon */
@@ -141,15 +134,6 @@ export class TorProvider {
   */
  #socksSettings = null;

  /**
   * The logs we received over the control port.
   * We store a finite number of log entries which can be configured with
   * extensions.torlauncher.max_tor_log_entries.
   *
   * @type {LogEntry[]}
   */
  #logs = [];

  #isBootstrapDone = false;
  /**
   * Keep the last warning to avoid broadcasting an async warning if it is the
@@ -511,15 +495,6 @@ export class TorProvider {
    return this.#controller.onionAuthViewKeys();
  }

  /**
   * Returns captured log messages.
   *
   * @returns {LogEntry[]} The logs we collected from the tor daemon so far.
   */
  getLog() {
    return structuredClone(this.#logs);
  }

  /**
   * @returns {boolean} true if we launched and control tor, false if we are
   * using system tor.
@@ -1033,15 +1008,6 @@ export class TorProvider {
      TorProviderTopics.TorLog
    );

    const maxEntries = Services.prefs.getIntPref(
      Preferences.MaxLogEntries,
      1000
    );
    if (maxEntries > 0 && this.#logs.length >= maxEntries) {
      this.#logs.splice(0, 1);
    }

    this.#logs.push({ type, msg, timestamp });
    switch (type) {
      case "ERR":
        logger.error(`[Tor error] ${msg}`);
+59 −0
Changes for toolkit/components/tor-launcher/TorProviderBuilder.sys.mjs: 59 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -23,6 +23,13 @@ export const TorProviders = Object.freeze({
  tor: 1,
});

/**
 * @typedef {object} LogEntry An object with a log message
 * @property {string} timestamp The local date-time stamp at which we received the message
 * @property {string} type The message level
 * @property {string} msg The message
 */

/**
 * The factory to get a Tor provider.
 * Currently we support only TorProvider, i.e., the one that interacts with
@@ -36,6 +43,48 @@ export class TorProviderBuilder {
   */
  static #provider = null;

  /**
   * A record of the log messages from all TorProvider instances.
   *
   * @type {LogEntry[]}
   */
  static #log = [];

  /**
   * Get a record of historic log entries.
   *
   * @returns {LogEntry[]} - The record of entries.
   */
  static getLog() {
    return structuredClone(this.#log);
  }

  /**
   * The limit on the number of log entries we should store.
   *
   * @type {integer}
   */
  static #logLimit;

  /**
   * The observer that checks for new TorLog messages.
   *
   * @type {Function}
   */
  static #logObserver;

  /**
   * Add a new log message.
   *
   * @param {LogEntry} logEntry - The log entry to add.
   */
  static #addLogEntry(logEntry) {
    if (this.#logLimit > 0 && this.#log.length >= this.#logLimit) {
      this.#log.splice(0, 1);
    }
    this.#log.push(logEntry);
  }

  /**
   * The observer that checks when the tor process exits, and reinitializes the
   * provider.
@@ -56,6 +105,15 @@ export class TorProviderBuilder {
   * Initialize the provider of choice.
   */
  static init() {
    this.#logLimit = Services.prefs.getIntPref(
      "extensions.torlauncher.max_tor_log_entries",
      1000
    );
    this.#logObserver = subject => {
      this.#addLogEntry(subject.wrappedJSObject);
    };
    Services.obs.addObserver(this.#logObserver, TorProviderTopics.TorLog);

    switch (this.providerType) {
      case TorProviders.tor:
        // Even though initialization of the initial TorProvider is
@@ -136,6 +194,7 @@ export class TorProviderBuilder {
      );
      this.#exitObserver = null;
    }
    Services.obs.removeObserver(this.#logObserver, TorProviderTopics.TorLog);
  }

  /**