stack_wallet/lib/services/fusion_tor_service.dart
sneurlax 523639edd4 Revert "tor_ffi_plugin -> tor"
This reverts commit f4f03b2dd3.
2024-06-22 11:43:20 -05:00

71 lines
1.8 KiB
Dart

import 'dart:io';
import '../utilities/logger.dart';
import 'package:tor_ffi_plugin/tor_ffi_plugin.dart';
class FusionTorService {
Tor? _tor;
String? _torDataDirPath;
TorStatus get status => _tor!.status;
/// Singleton instance of the TorService.
///
/// Use this to access the TorService and its properties.
static final sharedInstance = FusionTorService._();
// private constructor for singleton
FusionTorService._();
/// Getter for the proxyInfo.
///
/// Throws if Tor is not connected.
({
InternetAddress host,
int port,
}) getProxyInfo() {
try {
return (
host: InternetAddress.loopbackIPv4,
port: _tor!.port,
);
} catch (_) {
throw Exception("Tor proxy info fetched while not connected!");
}
}
/// Initialize the tor ffi lib instance if it hasn't already been set. Nothing
/// changes if _tor is already been set.
void init({
required String torDataDirPath,
Tor? mockableOverride,
}) {
_tor ??= mockableOverride ?? Tor.instance;
_torDataDirPath ??= torDataDirPath;
}
/// Start the Tor service.
///
/// This will start the Tor service and establish a Tor circuit.
///
/// Throws an exception if the Tor library was not inited or if the Tor
/// service fails to start.
///
/// Returns a Future that completes when the Tor service has started.
Future<void> start() async {
if (_tor == null || _torDataDirPath == null) {
throw Exception("FusionTorService.init has not been called!");
}
// Start the Tor service.
try {
await _tor!.start(torDataDirPath: _torDataDirPath!);
} catch (e, s) {
Logging.instance.log(
"FusionTorService.start failed: $e\n$s",
level: LogLevel.Warning,
);
rethrow;
}
}
}