Skip to content

Commit

Permalink
wip: works !!!
Browse files Browse the repository at this point in the history
  • Loading branch information
karliatto committed Dec 1, 2024
1 parent 0f051a1 commit e58e29e
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 88 deletions.
42 changes: 23 additions & 19 deletions packages/request-manager/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ export class TorController extends EventEmitter {
}, BOOTSTRAP_SLOW_TRESHOLD);
}

private onMessageReceived(message: string) {
const bootstrap: BootstrapEvent[] = bootstrapParser(message);
bootstrap.forEach(event => {
if (event.type !== 'progress') return;
if (event.progress && !this.getIsBootstrapping()) {
// We consider that bootstrap has started when we receive any bootstrap event and
// Tor is not bootstrapping yet.
// If we do not receive any bootstrapping event, we can consider there is something going wrong and
// an error will be thrown when `MAX_TRIES_WAITING` is reached in `waitUntilAlive`.
this.startBootstrap();
}
if (event.progress === BOOTSTRAP_EVENT_PROGRESS.Done) {
this.successfullyBootstrapped();
}
this.emit('bootstrap/event', event);
});
}

public async getTorConfiguration(
processId: number,
snowflakeBinaryPath?: string,
Expand Down Expand Up @@ -174,34 +192,20 @@ export class TorController extends EventEmitter {
return config;
}

public onMessageReceived(message: string) {
const bootstrap: BootstrapEvent[] = bootstrapParser(message);
bootstrap.forEach(event => {
if (event.type !== 'progress') return;
if (event.progress && !this.getIsBootstrapping()) {
// We consider that bootstrap has started when we receive any bootstrap event and
// Tor is not bootstrapping yet.
// If we do not receive any bootstrapping event, we can consider there is something going wrong and
// an error will be thrown when `MAX_TRIES_WAITING` is reached in `waitUntilAlive`.
this.startBootstrap();
}
if (event.progress === BOOTSTRAP_EVENT_PROGRESS.Done) {
this.successfullyBootstrapped();
}
this.emit('bootstrap/event', event);
});
}

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();
console.log('isConnected', isConnected);

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

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
const isAlive = this.controlPort.ping();
console.log('isAlive', isAlive);

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

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
const isCircuitEstablished = this.getIsCircuitEstablished();
console.log('isCircuitEstablished', isCircuitEstablished);

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

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
// It is running so let's not wait anymore.
if (isConnected && isAlive && this.getIsCircuitEstablished()) {
if (isConnected && isAlive && isCircuitEstablished) {
return true;
} else {
return false;
Expand Down
14 changes: 14 additions & 0 deletions packages/request-manager/src/controllerExternal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EventEmitter } from 'events';

export class TorControllerExternal extends EventEmitter {
constructor() {

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

View workflow job for this annotation

GitHub Actions / Linting and formatting

Useless constructor
super();
}
getTorConfiguration() {
return '';
}
waitUntilAlive() {}
getStatus() {}
closeActiveCircuits() {}
stop() {}
}
1 change: 1 addition & 0 deletions packages/request-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { TorController } from './controller';
export { TorControllerExternal } from './controllerExternal';
export { createInterceptor } from './interceptor';
export type { InterceptedEvent, BootstrapEvent, TorControllerStatus } from './types';
export { TOR_CONTROLLER_STATUS } from './types';
Expand Down
1 change: 1 addition & 0 deletions packages/request-manager/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const waitUntil = (
}
try {
const completed = await checkToSuccess();
console.log('completed', completed);

Check failure on line 21 in packages/request-manager/src/utils.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
if (completed) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/suite-desktop-core/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ declare type TorSettings = {
running: boolean; // Tor should be enabled
host: string; // Hostname of the tor process through which traffic is routed
port: number; // Port of the Tor process through which traffic is routed
// controlPort: number; // Port of the Tor Control Port
// torDataDir: string; // Path of tor data directory
controlPort: number; // Port of the Tor Control Port
torDataDir: string; // Path of tor data directory
snowflakeBinaryPath: string; // Path in user system to the snowflake binary
// externalPort: number; // Port of the Tor process run by the user externally to suite
useExternalTor: boolean; // Tor should use external daemon instead of the one built-in suite.
Expand Down
2 changes: 2 additions & 0 deletions packages/suite-desktop-core/src/libs/processes/BaseProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export abstract class BaseProcess {
* @param params Command line parameters for the process
*/
async start(params: string[] = []) {
console.log('start in base process');

Check failure on line 77 in packages/suite-desktop-core/src/libs/processes/BaseProcess.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
if (this.startupThrottle) {
this.logger.warn(this.logTopic, 'Canceling process start (throttle)');

Expand All @@ -82,6 +83,7 @@ export abstract class BaseProcess {

const status = await this.status();

console.log('status in base process', status);

Check failure on line 86 in packages/suite-desktop-core/src/libs/processes/BaseProcess.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Unexpected console statement
// Service is running, nothing to do
if (status.service) {
this.logger.warn(this.logTopic, 'Canceling process start (service running)');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { TorConnectionOptions } from '@trezor/request-manager/src/types';

Check failure on line 1 in packages/suite-desktop-core/src/libs/processes/TorExternalProcess.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'TorConnectionOptions' is defined but never used. Allowed unused vars must match /^_/u
import { checkSocks5Proxy } from '@trezor/node-utils';

import { Status } from './BaseProcess';
import { BaseProcess, Status } from './BaseProcess';

Check warning on line 4 in packages/suite-desktop-core/src/libs/processes/TorExternalProcess.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

There should be at least one empty line between import groups

Check warning on line 4 in packages/suite-desktop-core/src/libs/processes/TorExternalProcess.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`./BaseProcess` import should occur after import of `@trezor/request-manager`

Check failure on line 4 in packages/suite-desktop-core/src/libs/processes/TorExternalProcess.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'BaseProcess' is defined but never used. Allowed unused vars must match /^_/u
import { waitUntil } from '@trezor/request-manager/src/utils';
import { TorControllerExternal } from '@trezor/request-manager';
// import { TorController } from '@trezor/request-manager';

export type TorProcessStatus = Status & { isBootstrapping?: boolean; isSocks5ProxyPort?: boolean };

Expand All @@ -12,30 +14,13 @@ const DEFAULT_TOR_EXTERNAL_HOST = '127.0.0.1';
const DEFAULT_TOR_EXTERNAL_PORT = 9050;

export class TorExternalProcess {
// torController: TorController;
// port: number;
// controlPort: number;
// torHost: string;
// torDataDir: string;
// snowflakeBinaryPath: string;
// useExternalTor: boolean;

stop = true;

constructor(_options: TorConnectionOptions & { useExternalTor: boolean }) {
// this.port = options.port;
// this.controlPort = options.controlPort;
// this.torHost = options.host;
// this.torDataDir = options.torDataDir;
// this.useExternalTor = options.useExternalTor;
// this.snowflakeBinaryPath = '';
// this.torController = new TorController({
// host: this.torHost,
// port: this.port,
// controlPort: this.controlPort,
// torDataDir: this.torDataDir,
// snowflakeBinaryPath: this.snowflakeBinaryPath,
// });
isStopped = true;
torController: TorControllerExternal;
port = DEFAULT_TOR_EXTERNAL_PORT;
host = DEFAULT_TOR_EXTERNAL_HOST;
constructor() {
// this.torController = new FakeTorController();
this.torController = new TorControllerExternal();
}

setTorConfig(_torConfig: { useExternalTor: boolean; snowflakeBinaryPath: string }) {
Expand Down Expand Up @@ -64,7 +49,7 @@ export class TorExternalProcess {
}

public getIsStopped() {
return this.stop;
return this.isStopped;
}

public async waitUntilAliveExternal(host: string, port: number): Promise<void> {
Expand All @@ -89,7 +74,12 @@ export class TorExternalProcess {

async start(): Promise<void> {
console.log('startExternal in TorProcess start');
this.isStopped = false;
await this.waitUntilAliveExternal(DEFAULT_TOR_EXTERNAL_HOST, DEFAULT_TOR_EXTERNAL_PORT);
this.stop = false;
}

async stop() {
// We should not stop External Tor Process but ignore it.
this.isStopped = true;
}
}
2 changes: 2 additions & 0 deletions packages/suite-desktop-core/src/libs/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ export class Store {
return this.store.get('torSettings', {
running: false,
port: 9050,
controlPort: 9051,
host: '127.0.0.1',
snowflakeBinaryPath: '',
useExternalTor: false,
torDataDir: '',
});
}

Expand Down
Loading

0 comments on commit e58e29e

Please sign in to comment.