Skip to content

Commit

Permalink
fixup! feat(request-manager): support external Tor
Browse files Browse the repository at this point in the history
  • Loading branch information
karliatto committed Dec 9, 2024
1 parent 0628e51 commit b60c7e9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 76 deletions.
46 changes: 30 additions & 16 deletions packages/request-manager/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './types';
import { bootstrapParser, BOOTSTRAP_EVENT_PROGRESS } from './events/bootstrap';
import { waitUntil } from './utils';

Check warning on line 14 in packages/request-manager/src/controller.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be at least one empty line between import groups

Check failure on line 14 in packages/request-manager/src/controller.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'waitUntil' is defined but never used. Allowed unused vars must match /^_/u
import { ScheduleActionParams, ScheduledAction, scheduleAction } from '@trezor/utils';

Check warning on line 15 in packages/request-manager/src/controller.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`@trezor/utils` import should occur before import of `./torControlPort`

const WAITING_TIME = 1000;
const MAX_TRIES_WAITING = 200;
Expand Down Expand Up @@ -193,24 +194,37 @@ export class TorController extends EventEmitter {

public async waitUntilAlive(): Promise<void> {
this.status = TOR_CONTROLLER_STATUS.Bootstrapping;
await waitUntil(
MAX_TRIES_WAITING,
WAITING_TIME,
async () => {
const isConnected = await this.controlPort.connect();
const isAlive = this.controlPort.ping();
const isCircuitEstablished = this.getIsCircuitEstablished();
// It is running so let's not wait anymore.
if (isConnected && isAlive && isCircuitEstablished) {
return true;
} else {
return false;

const abortController = new AbortController();
const checkConnection: ScheduledAction<boolean> = async signal => {
if (signal?.aborted) {
throw new Error('Tor controller check alive aborted');
}
const isConnected = await this.controlPort.connect();
const isAlive = this.controlPort.ping();
const isCircuitEstablished = this.getIsCircuitEstablished();
// It is running so let's not wait anymore.
if (isConnected && isAlive && isCircuitEstablished) {
return true;
}
throw new Error('Tor not alive');
};
const params: ScheduleActionParams = {
attempts: MAX_TRIES_WAITING,
timeout: WAITING_TIME,
gap: WAITING_TIME,
signal: abortController.signal,
attemptFailureHandler: () => {
if (this.getIsStopped()) {
abortController.abort();

return new Error('Operation stopped.');
}

return undefined;
},
() => {
return this.getIsStopped();
},
);
};
await scheduleAction(checkConnection, params);
}

public getStatus(): Promise<TorControllerStatus> {
Expand Down
52 changes: 31 additions & 21 deletions packages/request-manager/src/controllerExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { EventEmitter } from 'events';

import { checkSocks5Proxy } from '@trezor/node-utils';

import { waitUntil } from './utils';
import { TOR_CONTROLLER_STATUS, TorControllerStatus, TorExternalConnectionOptions } from './types';

Check warning on line 5 in packages/request-manager/src/controllerExternal.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be at least one empty line between import groups
import { ScheduleActionParams, ScheduledAction, scheduleAction } from '@trezor/utils';

Check warning on line 6 in packages/request-manager/src/controllerExternal.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`@trezor/utils` import should occur before import of `./types`

const WAITING_TIME = 1_000;
const MAX_TRIES_WAITING = 200;
Expand Down Expand Up @@ -46,33 +46,43 @@ export class TorControllerExternal extends EventEmitter {

public async waitUntilAlive() {
this.startBootstrap();
await waitUntil(
MAX_TRIES_WAITING,
WAITING_TIME,
async () => {
const isRunning = await this.getIsExternalTorRunning();
if (isRunning) {
this.successfullyBootstrapped();
}

return isRunning;
},
() => {
return this.getIsStopped();
const abortController = new AbortController();
const checkConnection: ScheduledAction<boolean> = async signal => {
if (signal?.aborted) {
throw new Error('Operation aborted');
}
const isRunning = await this.getIsExternalTorRunning();
if (isRunning) {
this.successfullyBootstrapped();
return true;

Check failure on line 58 in packages/request-manager/src/controllerExternal.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Expected blank line before this statement
}
throw new Error('Tor external not alive');
};

const params: ScheduleActionParams = {
attempts: MAX_TRIES_WAITING,
timeout: WAITING_TIME,
gap: WAITING_TIME,
signal: abortController.signal,
attemptFailureHandler: () => {
if (this.getIsStopped()) {
abortController.abort();
return new Error('Operation stopped.');

Check failure on line 71 in packages/request-manager/src/controllerExternal.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Expected blank line before this statement
}
return undefined;

Check failure on line 73 in packages/request-manager/src/controllerExternal.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Expected blank line before this statement
},
);
};
await scheduleAction(checkConnection, params);
}

public async getStatus() {
const isExternalTorRunning = await this.getIsExternalTorRunning();
if (isExternalTorRunning) {
return TOR_CONTROLLER_STATUS.ExternalTorRunning;
}

return new Promise(resolve => {
if (isExternalTorRunning) {
return resolve(TOR_CONTROLLER_STATUS.ExternalTorRunning);
}

return resolve(TOR_CONTROLLER_STATUS.Stopped);
});
return TOR_CONTROLLER_STATUS.Stopped;
}

public closeActiveCircuits() {
Expand Down
39 changes: 0 additions & 39 deletions packages/request-manager/src/utils.ts

This file was deleted.

0 comments on commit b60c7e9

Please sign in to comment.