add init function to TorService so the compiled rust lib isn't loaded on instance creation but rather on init being called

This commit is contained in:
julian 2023-09-08 15:41:37 -06:00
parent 95790faf52
commit b278f691cc
3 changed files with 22 additions and 4 deletions

View file

@ -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();
}

View file

@ -66,6 +66,9 @@ class _TorSettingsState extends ConsumerState<TorSettings> {
// Toggle the useTor preference.
_prefs.useTor = true;
// Start the Tor service.
ref.read(pTorService).init();
// Start the Tor service.
ref.read(pTorService).start();
},

View file

@ -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<void> 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<void> 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;