From b278f691cc64d52bf9d71fdb4f651aca8e5a7daa Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 8 Sep 2023 15:41:37 -0600 Subject: [PATCH] add init function to TorService so the compiled rust lib isn't loaded on instance creation but rather on init being called --- lib/main.dart | 1 + .../tor_settings/tor_settings.dart | 3 +++ lib/services/tor_service.dart | 22 +++++++++++++++---- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 53c0df498..c39bc766d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -174,6 +174,7 @@ void main() async { // Some refactoring will need to be done here to make sure we don't make any // network calls before starting up tor if (Prefs.instance.useTor) { + TorService.sharedInstance.init(); await TorService.sharedInstance.start(); } diff --git a/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart b/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart index 76a1fa3bc..95c798930 100644 --- a/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart +++ b/lib/pages_desktop_specific/settings/settings_menu/tor_settings/tor_settings.dart @@ -66,6 +66,9 @@ class _TorSettingsState extends ConsumerState { // Toggle the useTor preference. _prefs.useTor = true; + // Start the Tor service. + ref.read(pTorService).init(); + // Start the Tor service. ref.read(pTorService).start(); }, diff --git a/lib/services/tor_service.dart b/lib/services/tor_service.dart index dc95f89c8..3998dba5e 100644 --- a/lib/services/tor_service.dart +++ b/lib/services/tor_service.dart @@ -9,7 +9,7 @@ import 'package:tor/tor.dart'; final pTorService = Provider((_) => TorService.sharedInstance); class TorService { - final _tor = Tor(); + Tor? _tor; /// Flag to indicate that a Tor circuit is thought to have been established. bool _enabled = false; @@ -30,9 +30,15 @@ class TorService { int port, }) get proxyInfo => ( host: InternetAddress.loopbackIPv4, - port: _tor.port, + port: _tor!.port, ); + /// Initialize the tor ffi lib instance if it hasn't already been set. Nothing + /// changes if _tor is already been set. + void init({Tor? mockableOverride}) { + _tor ??= mockableOverride ?? Tor(); + } + /// Start the Tor service. /// /// This will start the Tor service and establish a Tor circuit. @@ -41,6 +47,10 @@ class TorService { /// /// Returns a Future that completes when the Tor service has started. Future start() async { + if (_tor == null) { + throw Exception("TorService.init has not been called!"); + } + if (_enabled) { // already started so just return // could throw an exception here or something so the caller @@ -58,7 +68,7 @@ class TorService { "Tor connection status changed: connecting", ), ); - await _tor.start(); + await _tor!.start(); // no exception or error so we can (probably?) assume tor // has started successfully _enabled = true; @@ -89,6 +99,10 @@ class TorService { } Future stop() async { + if (_tor == null) { + throw Exception("TorService.init has not been called!"); + } + if (!_enabled) { // already stopped so just return // could throw an exception here or something so the caller @@ -99,7 +113,7 @@ class TorService { // Stop the Tor service. try { - await _tor.disable(); + await _tor!.disable(); // no exception or error so we can (probably?) assume tor // has started successfully _enabled = false;