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

fixup! Bug 40933: Add tor-launcher functionality

This partially reverts commit 34f7e833.

It removes the errorCode logic, and throws non-localized error
messages.

Also, it removes the no-longer used getFormattedLocalizedString.
parent 13ca87b4
Loading
Loading
Loading
Loading
+1 −25
Original line number Diff line number Diff line
@@ -376,12 +376,9 @@ export const TorLauncherUtil = Object.freeze({
   * @param {boolean} initError If we could connect to the control port at
   * least once and we are showing this prompt because the tor process exited
   * suddenly, we will display a different message
   * @param {Error?=} error The error that caused Tor not to launch. If it has a
   * code, we will try to translate it, otherwise we will show the message
   * (if not empty).
   * @returns {boolean} true if the user asked to restart tor
   */
  showRestartPrompt(initError, error = null) {
  showRestartPrompt(initError) {
    let s;
    if (initError) {
      const key = "tor_exited_during_startup";
@@ -393,15 +390,6 @@ export const TorLauncherUtil = Object.freeze({
        "\n\n" +
        this.getLocalizedString("tor_exited2");
    }

    if (error) {
      if (error.code && this.getLocalizedString(error.code) !== error.code) {
        s += "\n\n" + this.getLocalizedString(error.code);
      } else if (error.message) {
        s += `\n\n${error.message}`;
      }
    }

    const defaultBtnLabel = this.getLocalizedString("restart_tor");
    let cancelBtnLabel = "OK";
    try {
@@ -429,18 +417,6 @@ export const TorLauncherUtil = Object.freeze({
    return aStringName;
  },

  // "torlauncher." is prepended to aStringName.
  getFormattedLocalizedString(aStringName, aArray, aLen) {
    if (!aStringName || !aArray) {
      return aStringName;
    }
    try {
      const key = kPropNamePrefix + aStringName;
      return this._stringBundle.formatStringFromName(key, aArray, aLen);
    } catch (e) {}
    return aStringName;
  },

  /**
   * Determine what kind of SOCKS port has been requested for this session or
   * the browser has been configured for.
+19 −24
Original line number Diff line number Diff line
@@ -193,39 +193,32 @@ export class TorProcess {

  #makeArgs() {
    this.#exeFile = lazy.TorLauncherUtil.getTorFile("tor", false);
    if (!this.#exeFile) {
      throw new Error("Could not find the tor binary.");
    }
    const torrcFile = lazy.TorLauncherUtil.getTorFile("torrc", true);
    if (!torrcFile) {
      // FIXME: Is this still a fatal error?
      throw new Error("Could not find the torrc.");
    }
    // Get the Tor data directory first so it is created before we try to
    // construct paths to files that will be inside it.
    this.#dataDir = lazy.TorLauncherUtil.getTorFile("tordatadir", true);
    if (!this.#dataDir) {
      throw new Error("Could not find the tor data directory.");
    }
    const onionAuthDir = lazy.TorLauncherUtil.getTorFile(
      "toronionauthdir",
      true
    );
    if (!onionAuthDir) {
      throw new Error("Could not find the tor onion authentication directory.");
    }

    let errorCode;
    if (!this.#exeFile) {
      errorCode = "tor_missing";
    } else if (!torrcFile) {
      errorCode = "torrc_missing";
    } else if (!this.#dataDir) {
      errorCode = "datadir_missing";
    } else if (!onionAuthDir) {
      errorCode = "onionauthdir_missing";
    }
    if (errorCode) {
      const error = new Error(`An essential file is missing (${errorCode})`);
      error.code = errorCode;
      throw error;
    }

    this.#args = [
      "-f",
      torrcFile.path,
      "DataDirectory",
      this.#dataDir.path,
      "ClientOnionAuthDir",
      onionAuthDir.path,
    ];
    this.#args = [];
    this.#args.push("-f", torrcFile.path);
    this.#args.push("DataDirectory", this.#dataDir.path);
    this.#args.push("ClientOnionAuthDir", onionAuthDir.path);

    // TODO: Create this starting from pt_config.json (tor-browser#42357).
    const torrcDefaultsFile = lazy.TorLauncherUtil.getTorFile(
@@ -235,6 +228,8 @@ export class TorProcess {
    if (torrcDefaultsFile) {
      this.#args.push("--defaults-torrc", torrcDefaultsFile.path);
      // The geoip and geoip6 files are in the same directory as torrc-defaults.
      // TODO: Change TorFile to return the generic path to these files to make
      // them independent from the torrc-defaults.
      const geoipFile = torrcDefaultsFile.clone();
      geoipFile.leafName = "geoip";
      this.#args.push("GeoIPFile", geoipFile.path);
@@ -243,7 +238,7 @@ export class TorProcess {
      this.#args.push("GeoIPv6File", geoip6File.path);
    } else {
      logger.warn(
        "torrc-defaults not found, some functionalities will be disabled."
        "torrc-defaults was not found, some functionalities will be disabled."
      );
    }
  }
+5 −12
Original line number Diff line number Diff line
@@ -148,7 +148,6 @@ export class TorProviderBuilder {
      return;
    }
    let running = false;
    let error;
    try {
      const provider = await this.#provider;
      // The initialization might have succeeded, but so far we have ignored any
@@ -156,17 +155,14 @@ export class TorProviderBuilder {
      // provider has been initialized successfully, but the UI was not ready
      // yet.
      running = provider.isRunning;
    } catch (e) {
    } catch {
      // Not even initialized, running is already false.
      error = e;
    }
    while (!running && lazy.TorLauncherUtil.showRestartPrompt(true, error)) {
    while (!running && lazy.TorLauncherUtil.showRestartPrompt(true)) {
      try {
        await this.#initTorProvider();
        running = true;
      } catch (e) {
        error = e;
      }
      } catch {}
    }
    // The user might have canceled the restart, but at this point the UI is
    // ready in any case.
@@ -180,14 +176,11 @@ export class TorProviderBuilder {
      );
      return;
    }
    let error = null;
    while (lazy.TorLauncherUtil.showRestartPrompt(false, error)) {
    while (lazy.TorLauncherUtil.showRestartPrompt(false)) {
      try {
        await this.#initTorProvider();
        break;
      } catch (e) {
        error = e;
      }
      } catch {}
    }
  }