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

fixup! Bug 40597: Implement TorSettings module

Various fixes to address !968.
parent 05516a28
Branches
Tags
1 merge request!968Draft: Bug 42479: Improve TorConnect error handling
......@@ -12,6 +12,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
MoatRPC: "resource://gre/modules/Moat.sys.mjs",
TorBootstrapRequest: "resource://gre/modules/TorBootstrapRequest.sys.mjs",
TorProviderBuilder: "resource://gre/modules/TorProviderBuilder.sys.mjs",
TorProviderTopics: "resource://gre/modules/TorProviderBuilder.sys.mjs",
TorLauncherUtil: "resource://gre/modules/TorLauncherUtil.sys.mjs",
TorSettings: "resource://gre/modules/TorSettings.sys.mjs",
});
......@@ -23,11 +24,6 @@ ChromeUtils.defineModuleGetter(
"resource:///modules/BrowserWindowTracker.jsm"
);
const TorTopics = Object.freeze({
LogHasWarnOrErr: "TorLogHasWarnOrErr",
ProcessExited: "TorProcessExited",
});
/* Relevant prefs used by tor-launcher */
const TorLauncherPrefs = Object.freeze({
prompt_at_startup: "extensions.torlauncher.prompt_at_startup",
......@@ -56,29 +52,21 @@ export const TorConnectState = Object.freeze({
Disabled: "Disabled",
});
/**
* Errors that can happen during the bootstrap.
*
* BootstrapError should include an exception with more details, if possible.
* ExternalError is used to wrap errors that do not include a code from this
* object, and should always include a cause.
*/
export const TorConnectErrors = Object.freeze({
Offline: "Offline",
BootstrapError: "BootstrapError",
CannotDetermineCountry: "CannotDetermineCountry",
NoSettingsForCountry: "NoSettingsForCountry",
AllSettingsFailed: "AllSettingsFailed",
ExternalError: "ExternalError",
});
export class TorConnectError extends Error {
static Offline = "Offline";
static BootstrapError = "BootstrapError";
static CannotDetermineCountry = "CannotDetermineCountry";
static NoSettingsForCountry = "NoSettingsForCountry";
static AllSettingsFailed = "AllSettingsFailed";
static ExternalError = "ExternalError";
class TorConnectError extends Error {
constructor(code, cause) {
super(cause?.message ?? `TorConnectError: ${code}`, cause ? { cause } : {});
this.name = "TorConnectError";
this.code = code;
}
}
Object.freeze(TorConnectError);
ChromeUtils.defineLazyGetter(
lazy,
......@@ -142,7 +130,7 @@ export const TorConnectTopics = Object.freeze({
StateChange: "torconnect:state-change",
BootstrapProgress: "torconnect:bootstrap-progress",
BootstrapComplete: "torconnect:bootstrap-complete",
BootstrapError: "torconnect:bootstrap-error",
Error: "torconnect:error",
});
// The StateCallback is the base class to implement the various states.
......@@ -310,7 +298,7 @@ class BootstrappingState extends StateCallback {
this.#bootstrap = new lazy.TorBootstrapRequest();
this.#bootstrap.onbootstrapstatus = (progress, status) => {
TorConnect._updateBootstrapStatus(progress, status);
TorConnect._updateBootstrapProgress(progress, status);
};
this.#bootstrap.onbootstrapcomplete = () => {
this.#internetTest.cancel();
......@@ -376,7 +364,7 @@ class BootstrappingState extends StateCallback {
if (this.#internetTest.status === InternetStatus.Offline) {
this.changeState(
TorConnectState.Error,
new TorConnectError(TorConnectErrors.Offline)
new TorConnectError(TorConnectError.Offline)
);
} else {
// Give priority to the bootstrap error, in case the Internet test fails
......@@ -384,7 +372,7 @@ class BootstrappingState extends StateCallback {
this.changeState(
TorConnectState.Error,
new TorConnectError(
TorConnectErrors.BootstrapError,
TorConnectError.BootstrapError,
this.#bootstrapError
)
);
......@@ -413,10 +401,11 @@ class BootstrappingState extends StateCallback {
codes[Math.floor(Math.random() * codes.length)];
}
const err = new Error("Censorship simulation");
err.details = { phase: "conn", reason: "noroute" };
err.phase = "conn";
err.reason = "noroute";
this.changeState(
TorConnectState.Error,
new TorConnectError(TorConnectErrors.BootstrapError, err)
new TorConnectError(TorConnectError.BootstrapError, err)
);
return true;
}
......@@ -481,7 +470,7 @@ class AutoBootstrappingState extends StateCallback {
if (!this.transitioning) {
this.changeState(
TorConnectState.Error,
new TorConnectError(TorConnectErrors.AllSettingsFailed)
new TorConnectError(TorConnectError.AllSettingsFailed)
);
}
return true;
......@@ -494,7 +483,7 @@ class AutoBootstrappingState extends StateCallback {
if (!this.transitioning) {
this.changeState(
TorConnectState.Error,
new TorConnectError(TorConnectErrors.CannotDetermineCountry)
new TorConnectError(TorConnectError.CannotDetermineCountry)
);
}
return true;
......@@ -556,10 +545,10 @@ class AutoBootstrappingState extends StateCallback {
if (!TorConnect._detectedLocation) {
// unable to determine country
throw new TorConnectError(TorConnectErrors.CannotDetermineCountry);
throw new TorConnectError(TorConnectError.CannotDetermineCountry);
} else {
// no settings available for country
throw new TorConnectError(TorConnectErrors.NoSettingsForCountry);
throw new TorConnectError(TorConnectError.NoSettingsForCountry);
}
}
}
......@@ -604,7 +593,7 @@ class AutoBootstrappingState extends StateCallback {
// Build out our bootstrap request.
const bootstrap = new lazy.TorBootstrapRequest();
bootstrap.onbootstrapstatus = (progress, status) => {
TorConnect._updateBootstrapStatus(progress, status);
TorConnect._updateBootstrapProgress(progress, status);
};
bootstrap.onbootstraperror = error => {
lazy.logger.error("Auto-Bootstrap error", error);
......@@ -650,7 +639,7 @@ class AutoBootstrappingState extends StateCallback {
// Only explicitly change state here if something else has not transitioned
// us.
if (!this.transitioning) {
throw new TorConnectError(TorConnectErrors.AllSettingsFailed);
throw new TorConnectError(TorConnectError.AllSettingsFailed);
}
}
......@@ -702,17 +691,14 @@ class ErrorState extends StateCallback {
}
run(error) {
if (!error.code || !(error.code in TorConnectErrors)) {
error = new TorConnectError(TorConnectErrors.ExternalError, error);
if (!(error instanceof TorConnectError)) {
error = new TorConnectError(TorConnectError.ExternalError, error);
}
TorConnect._errorCode = error.code;
TorConnect._errorDetails = error;
lazy.logger.error(`Entering error state (${error.code})`, error);
Services.obs.notifyObservers(
{ code: error.code, message: error.message, details: error },
TorConnectTopics.BootstrapError
);
Services.obs.notifyObservers(error, TorConnectTopics.Error);
this.changeState(TorConnectState.Configuring);
}
......@@ -839,7 +825,6 @@ class InternetTest {
export const TorConnect = {
_stateHandler: new InitialState(),
_bootstrapProgress: 0,
_bootstrapStatus: null,
_internetStatus: InternetStatus.Unknown,
// list of country codes Moat has settings for
_countryCodes: [],
......@@ -929,17 +914,15 @@ export const TorConnect = {
this._stateHandler.begin(...args);
},
_updateBootstrapStatus(progress, status) {
_updateBootstrapProgress(progress, status) {
this._bootstrapProgress = progress;
this._bootstrapStatus = status;
lazy.logger.info(
`Bootstrapping ${this._bootstrapProgress}% complete (${this._bootstrapStatus})`
`Bootstrapping ${this._bootstrapProgress}% complete (${status})`
);
Services.obs.notifyObservers(
{
progress: TorConnect._bootstrapProgress,
status: TorConnect._bootstrapStatus,
hasWarnings: TorConnect._logHasWarningOrError,
},
TorConnectTopics.BootstrapProgress
......@@ -973,8 +956,8 @@ export const TorConnect = {
);
// register the Tor topics we always care about
observeTopic(TorTopics.ProcessExited);
observeTopic(TorTopics.LogHasWarnOrErr);
observeTopic(lazy.TorProviderTopics.ProcessExited);
observeTopic(lazy.TorProviderTopics.HasWarnOrErr);
}
},
......@@ -982,11 +965,11 @@ export const TorConnect = {
lazy.logger.debug(`Observed ${topic}`);
switch (topic) {
case TorTopics.LogHasWarnOrErr: {
case lazy.TorProviderTopics.HasWarnOrErr: {
this._logHasWarningOrError = true;
break;
}
case TorTopics.ProcessExited: {
case lazy.TorProviderTopics.ProcessExited: {
// Treat a failure as a possibly broken configuration.
// So, prevent quickstart at the next start.
Services.prefs.setBoolPref(TorLauncherPrefs.prompt_at_startup, true);
......@@ -1109,10 +1092,6 @@ export const TorConnect = {
return this._bootstrapProgress;
},
get bootstrapStatus() {
return this._bootstrapStatus;
},
get internetStatus() {
return this._internetStatus;
},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment