Skip to content
Snippets Groups Projects
Verified Commit 0ee02b12 authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame :jack_o_lantern:
Browse files

fixup! Bug 40933: Add tor-launcher functionality

Switched to lazy.
parent 1f26c168
Branches
Tags
1 merge request!694Bug 41796: Rebased on top of FIREFOX_ESR_115_BASE
......@@ -108,13 +108,10 @@ class TorFile {
// If XDG_RUNTIME_DIR is set, use it as the base directory for IPC
// objects (e.g., Unix domain sockets) -- assuming it is not too long.
const env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
if (!env.exists("XDG_RUNTIME_DIR")) {
if (!Services.env.exists("XDG_RUNTIME_DIR")) {
return;
}
const ipcDir = this.createUniqueIPCDir(env.get("XDG_RUNTIME_DIR"));
const ipcDir = this.createUniqueIPCDir(Services.env.get("XDG_RUNTIME_DIR"));
if (ipcDir) {
const f = ipcDir.clone();
f.append(this.ipcFileName);
......@@ -466,14 +463,11 @@ const TorLauncherUtil = Object.freeze({
try {
const kBrowserToolboxPort = "MOZ_BROWSER_TOOLBOX_PORT";
const kEnvSkipLaunch = "TOR_SKIP_LAUNCH";
const env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
if (env.exists(kBrowserToolboxPort)) {
if (Services.env.exists(kBrowserToolboxPort)) {
return false;
}
if (env.exists(kEnvSkipLaunch)) {
const value = parseInt(env.get(kEnvSkipLaunch));
if (Services.env.exists(kEnvSkipLaunch)) {
const value = parseInt(Services.env.get(kEnvSkipLaunch));
return isNaN(value) || !value;
}
} catch (e) {}
......@@ -483,11 +477,8 @@ const TorLauncherUtil = Object.freeze({
get shouldShowNetworkSettings() {
try {
const kEnvForceShowNetConfig = "TOR_FORCE_NET_CONFIG";
const env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
if (env.exists(kEnvForceShowNetConfig)) {
const value = parseInt(env.get(kEnvForceShowNetConfig));
if (Services.env.exists(kEnvForceShowNetConfig)) {
const value = parseInt(Services.env.get(kEnvForceShowNetConfig));
return !isNaN(value) && value;
}
} catch (e) {}
......@@ -498,11 +489,8 @@ const TorLauncherUtil = Object.freeze({
const kPrefOnlyConfigureTor = "extensions.torlauncher.only_configure_tor";
try {
const kEnvOnlyConfigureTor = "TOR_CONFIGURE_ONLY";
const env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
if (env.exists(kEnvOnlyConfigureTor)) {
const value = parseInt(env.get(kEnvOnlyConfigureTor));
if (Services.env.exists(kEnvOnlyConfigureTor)) {
const value = parseInt(Services.env.get(kEnvOnlyConfigureTor));
return !isNaN(value) && value;
}
} catch (e) {}
......
......
......@@ -8,9 +8,7 @@ const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { clearTimeout, setTimeout } = ChromeUtils.import(
"resource://gre/modules/Timer.jsm"
);
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
const { TorParsers, TorStatuses } = ChromeUtils.import(
"resource://gre/modules/TorParsers.jsm"
......@@ -22,24 +20,19 @@ const { TorProcess } = ChromeUtils.import(
const { TorLauncherUtil } = ChromeUtils.import(
"resource://gre/modules/TorLauncherUtil.jsm"
);
const lazy = {};
ChromeUtils.defineModuleGetter(
this,
lazy,
"controller",
"resource://torbutton/modules/tor-control-port.js"
);
// TODO: Write a helper to create these logs
XPCOMUtils.defineLazyGetter(this, "logger", () => {
const { ConsoleAPI } = ChromeUtils.import(
"resource://gre/modules/Console.jsm"
);
// TODO: Use a preference to set the log level.
const consoleOptions = {
const logger = new ConsoleAPI({
// maxLogLevel: "warn",
maxLogLevel: "all",
prefix: "TorMonitorService",
};
return new ConsoleAPI(consoleOptions);
});
const Preferences = Object.freeze({
......@@ -271,7 +264,7 @@ const TorMonitorService = {
let conn;
try {
const avoidCache = true;
conn = await controller(avoidCache);
conn = await lazy.controller(avoidCache);
} catch (e) {
logger.error("Cannot open a control port connection", e);
if (conn) {
......
......
......@@ -4,16 +4,15 @@ var EXPORTED_SYMBOLS = ["TorProcess"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
const { Subprocess } = ChromeUtils.import(
"resource://gre/modules/Subprocess.jsm"
);
const lazy = {};
ChromeUtils.defineModuleGetter(
this,
lazy,
"TorProtocolService",
"resource://gre/modules/TorProtocolService.jsm"
);
......@@ -32,17 +31,9 @@ const TorProcessStatus = Object.freeze({
Exited: 3,
});
// Logger adapted from CustomizableUI.jsm
XPCOMUtils.defineLazyGetter(this, "logger", () => {
const { ConsoleAPI } = ChromeUtils.import(
"resource://gre/modules/Console.jsm"
);
// TODO: Use a preference to set the log level.
const consoleOptions = {
const logger = new ConsoleAPI({
maxLogLevel: "info",
prefix: "TorProcess",
};
return new ConsoleAPI(consoleOptions);
});
class TorProcess {
......@@ -242,7 +233,7 @@ class TorProcess {
const torrcFile = TorLauncherUtil.getTorFile("torrc", true);
this._dataDir = TorLauncherUtil.getTorFile("tordatadir", true);
const onionAuthDir = TorLauncherUtil.getTorFile("toronionauthdir", true);
const hashedPassword = TorProtocolService.torGetPassword(true);
const hashedPassword = lazy.TorProtocolService.torGetPassword(true);
let detailsKey;
if (!this._exeFile) {
detailsKey = "tor_missing";
......@@ -302,8 +293,8 @@ class TorProcess {
// to any control ports that the user has defined in their torrc
// file and (2) it is never written to torrc.
let controlPortArg;
const controlIPCFile = TorProtocolService.torGetControlIPCFile();
const controlPort = TorProtocolService.torGetControlPort();
const controlIPCFile = lazy.TorProtocolService.torGetControlIPCFile();
const controlPort = lazy.TorProtocolService.torGetControlPort();
if (controlIPCFile) {
controlPortArg = this._ipcPortArg(controlIPCFile);
} else if (controlPort) {
......@@ -321,7 +312,7 @@ class TorProcess {
// include a "+__" prefix so that (1) this SOCKS port is added
// to any SOCKS ports that the user has defined in their torrc
// file and (2) it is never written to torrc.
const socksPortInfo = TorProtocolService.torGetSOCKSPortInfo();
const socksPortInfo = lazy.TorProtocolService.torGetSOCKSPortInfo();
if (socksPortInfo) {
let socksPortArg;
if (socksPortInfo.ipcFile) {
......@@ -379,9 +370,9 @@ class TorProcess {
return true;
}
const controlIPCFile = TorProtocolService.torGetControlIPCFile();
const controlPort = TorProtocolService.torGetControlPort();
const socksPortInfo = TorProtocolService.torGetSOCKSPortInfo();
const controlIPCFile = lazy.TorProtocolService.torGetControlIPCFile();
const controlPort = lazy.TorProtocolService.torGetControlPort();
const socksPortInfo = lazy.TorProtocolService.torGetSOCKSPortInfo();
const valueIsUnixDomainSocket = aValue => {
// Handle several cases:
......
......
......@@ -6,16 +6,7 @@ var EXPORTED_SYMBOLS = ["TorProtocolService"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
ChromeUtils.defineModuleGetter(
this,
"FileUtils",
"resource://gre/modules/FileUtils.jsm"
);
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
Cu.importGlobalProperties(["crypto"]);
const { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
const { TorParsers } = ChromeUtils.import(
"resource://gre/modules/TorParsers.jsm"
......@@ -24,18 +15,26 @@ const { TorLauncherUtil } = ChromeUtils.import(
"resource://gre/modules/TorLauncherUtil.jsm"
);
const lazy = {};
ChromeUtils.defineModuleGetter(
this,
lazy,
"FileUtils",
"resource://gre/modules/FileUtils.jsm"
);
ChromeUtils.defineModuleGetter(
lazy,
"TorMonitorService",
"resource://gre/modules/TorMonitorService.jsm"
);
ChromeUtils.defineModuleGetter(
this,
lazy,
"configureControlPortModule",
"resource://torbutton/modules/tor-control-port.js"
);
ChromeUtils.defineModuleGetter(
this,
lazy,
"controller",
"resource://torbutton/modules/tor-control-port.js"
);
......@@ -45,18 +44,10 @@ const TorTopics = Object.freeze({
ProcessRestarted: "TorProcessRestarted",
});
// Logger adapted from CustomizableUI.jsm
XPCOMUtils.defineLazyGetter(this, "logger", () => {
const { ConsoleAPI } = ChromeUtils.import(
"resource://gre/modules/Console.jsm"
);
// TODO: Use a preference to set the log level.
const consoleOptions = {
const logger = new ConsoleAPI({
// maxLogLevel: "warn",
maxLogLevel: "all",
prefix: "TorProtocolService",
};
return new ConsoleAPI(consoleOptions);
});
// Manage the connection to tor's control port, to update its settings and query
......@@ -175,8 +166,8 @@ const TorProtocolService = {
settings[kTorConfKeyDisableNetwork] = false;
await this.setConfWithReply(settings);
await this.sendCommand("SAVECONF");
TorMonitorService.clearBootstrapError();
TorMonitorService.retrieveBootstrapStatus();
lazy.TorMonitorService.clearBootstrapError();
lazy.TorMonitorService.retrieveBootstrapStatus();
},
async stopBootstrap() {
......@@ -191,7 +182,7 @@ const TorProtocolService = {
// We are not interested in waiting for this, nor in **catching its error**,
// so we do not await this. We just want to be notified when the bootstrap
// status is actually updated through observers.
TorMonitorService.retrieveBootstrapStatus();
lazy.TorMonitorService.retrieveBootstrapStatus();
},
// TODO: transform the following 4 functions in getters. At the moment they
......@@ -318,21 +309,21 @@ const TorProtocolService = {
async _setSockets() {
try {
const isWindows = TorLauncherUtil.isWindows;
const env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
// Determine how Tor Launcher will connect to the Tor control port.
// Environment variables get top priority followed by preferences.
if (!isWindows && env.exists("TOR_CONTROL_IPC_PATH")) {
const ipcPath = env.get("TOR_CONTROL_IPC_PATH");
this._controlIPCFile = new FileUtils.File(ipcPath);
if (!isWindows && Services.env.exists("TOR_CONTROL_IPC_PATH")) {
const ipcPath = Services.env.get("TOR_CONTROL_IPC_PATH");
this._controlIPCFile = new lazy.FileUtils.File(ipcPath);
} else {
// Check for TCP host and port environment variables.
if (env.exists("TOR_CONTROL_HOST")) {
this._controlHost = env.get("TOR_CONTROL_HOST");
if (Services.env.exists("TOR_CONTROL_HOST")) {
this._controlHost = Services.env.get("TOR_CONTROL_HOST");
}
if (env.exists("TOR_CONTROL_PORT")) {
this._controlPort = parseInt(env.get("TOR_CONTROL_PORT"), 10);
if (Services.env.exists("TOR_CONTROL_PORT")) {
this._controlPort = parseInt(
Services.env.get("TOR_CONTROL_PORT"),
10
);
}
const useIPC =
......@@ -363,11 +354,11 @@ const TorProtocolService = {
}
// Populate _controlPassword so it is available when starting tor.
if (env.exists("TOR_CONTROL_PASSWD")) {
this._controlPassword = env.get("TOR_CONTROL_PASSWD");
} else if (env.exists("TOR_CONTROL_COOKIE_AUTH_FILE")) {
if (Services.env.exists("TOR_CONTROL_PASSWD")) {
this._controlPassword = Services.env.get("TOR_CONTROL_PASSWD");
} else if (Services.env.exists("TOR_CONTROL_COOKIE_AUTH_FILE")) {
// TODO: test this code path (TOR_CONTROL_COOKIE_AUTH_FILE).
const cookiePath = env.get("TOR_CONTROL_COOKIE_AUTH_FILE");
const cookiePath = Services.env.get("TOR_CONTROL_COOKIE_AUTH_FILE");
if (cookiePath) {
this._controlPassword = await this._readAuthenticationCookie(
cookiePath
......@@ -424,18 +415,21 @@ const TorProtocolService = {
let useIPC;
this._SOCKSPortInfo = { ipcFile: undefined, host: undefined, port: 0 };
if (!isWindows && env.exists("TOR_SOCKS_IPC_PATH")) {
let ipcPath = env.get("TOR_SOCKS_IPC_PATH");
this._SOCKSPortInfo.ipcFile = new FileUtils.File(ipcPath);
if (!isWindows && Services.env.exists("TOR_SOCKS_IPC_PATH")) {
let ipcPath = Services.env.get("TOR_SOCKS_IPC_PATH");
this._SOCKSPortInfo.ipcFile = new lazy.FileUtils.File(ipcPath);
useIPC = true;
} else {
// Check for TCP host and port environment variables.
if (env.exists("TOR_SOCKS_HOST")) {
this._SOCKSPortInfo.host = env.get("TOR_SOCKS_HOST");
if (Services.env.exists("TOR_SOCKS_HOST")) {
this._SOCKSPortInfo.host = Services.env.get("TOR_SOCKS_HOST");
useIPC = false;
}
if (env.exists("TOR_SOCKS_PORT")) {
this._SOCKSPortInfo.port = parseInt(env.get("TOR_SOCKS_PORT"), 10);
if (Services.env.exists("TOR_SOCKS_PORT")) {
this._SOCKSPortInfo.port = parseInt(
Services.env.get("TOR_SOCKS_PORT"),
10
);
useIPC = false;
}
}
......@@ -488,7 +482,7 @@ const TorProtocolService = {
// Set the global control port info parameters.
// These values may be overwritten by torbutton when it initializes, but
// torbutton's values *should* be identical.
configureControlPortModule(
lazy.configureControlPortModule(
this._controlIPCFile,
this._controlHost,
this._controlPort,
......@@ -617,7 +611,7 @@ const TorProtocolService = {
async _getConnection() {
if (!this._controlConnection) {
const avoidCache = true;
this._controlConnection = await controller(avoidCache);
this._controlConnection = await lazy.controller(avoidCache);
}
if (this._controlConnection.inUse) {
await new Promise((resolve, reject) =>
......
......
......@@ -4,20 +4,22 @@ var EXPORTED_SYMBOLS = ["TorStartupService"];
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const lazy = {};
// We will use the modules only when the profile is loaded, so prefer lazy
// loading
ChromeUtils.defineModuleGetter(
this,
lazy,
"TorLauncherUtil",
"resource://gre/modules/TorLauncherUtil.jsm"
);
ChromeUtils.defineModuleGetter(
this,
lazy,
"TorMonitorService",
"resource://gre/modules/TorMonitorService.jsm"
);
ChromeUtils.defineModuleGetter(
this,
lazy,
"TorProtocolService",
"resource://gre/modules/TorProtocolService.jsm"
);
......@@ -50,8 +52,8 @@ class TorStartupService {
// Starts TorProtocolService first, because it configures the controller
// factory, too.
await TorProtocolService.init();
TorMonitorService.init();
await lazy.TorProtocolService.init();
lazy.TorMonitorService.init();
gInited = true;
}
......@@ -60,11 +62,11 @@ class TorStartupService {
Services.obs.removeObserver(this, BrowserTopics.QuitApplicationGranted);
// Close any helper connection first...
TorProtocolService.uninit();
lazy.TorProtocolService.uninit();
// ... and only then closes the event monitor connection, which will cause
// Tor to stop.
TorMonitorService.uninit();
lazy.TorMonitorService.uninit();
TorLauncherUtil.cleanupTempDirectories();
lazy.TorLauncherUtil.cleanupTempDirectories();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment