2023-09-07 18:07:05 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-08-07 16:39:04 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-06-22 17:23:59 +00:00
|
|
|
import 'package:tor_ffi_plugin/tor_ffi_plugin.dart';
|
|
|
|
|
|
|
|
import '../utilities/logger.dart';
|
2024-05-23 00:37:06 +00:00
|
|
|
import 'event_bus/events/global/tor_connection_status_changed_event.dart';
|
|
|
|
import 'event_bus/global_event_bus.dart';
|
2023-08-07 16:39:04 +00:00
|
|
|
|
2023-08-07 16:46:34 +00:00
|
|
|
final pTorService = Provider((_) => TorService.sharedInstance);
|
2023-08-07 16:39:04 +00:00
|
|
|
|
|
|
|
class TorService {
|
2023-09-08 21:41:37 +00:00
|
|
|
Tor? _tor;
|
2023-09-15 19:51:20 +00:00
|
|
|
String? _torDataDirPath;
|
2023-09-07 22:56:48 +00:00
|
|
|
|
2023-09-15 21:01:31 +00:00
|
|
|
/// Current status. Same as that fired on the event bus.
|
2023-09-15 18:10:51 +00:00
|
|
|
TorConnectionStatus get status => _status;
|
|
|
|
TorConnectionStatus _status = TorConnectionStatus.disconnected;
|
|
|
|
|
2023-09-07 22:56:48 +00:00
|
|
|
/// Singleton instance of the TorService.
|
|
|
|
///
|
|
|
|
/// Use this to access the TorService and its properties.
|
2023-09-07 18:07:05 +00:00
|
|
|
static final sharedInstance = TorService._();
|
2023-08-07 16:39:04 +00:00
|
|
|
|
2023-09-15 19:51:20 +00:00
|
|
|
// private constructor for singleton
|
|
|
|
TorService._();
|
|
|
|
|
2023-09-07 22:56:48 +00:00
|
|
|
/// Getter for the proxyInfo.
|
2023-09-15 19:51:20 +00:00
|
|
|
///
|
2023-09-15 21:01:31 +00:00
|
|
|
/// Throws if Tor is not connected.
|
2023-09-07 18:07:05 +00:00
|
|
|
({
|
|
|
|
InternetAddress host,
|
|
|
|
int port,
|
2023-09-15 19:51:20 +00:00
|
|
|
}) getProxyInfo() {
|
|
|
|
if (status == TorConnectionStatus.connected) {
|
|
|
|
return (
|
2023-09-07 18:07:05 +00:00
|
|
|
host: InternetAddress.loopbackIPv4,
|
2023-09-15 19:51:20 +00:00
|
|
|
port: _tor!.port,
|
2023-09-07 18:07:05 +00:00
|
|
|
);
|
2023-09-15 19:51:20 +00:00
|
|
|
} else {
|
|
|
|
throw Exception("Tor proxy info fetched while not connected!");
|
|
|
|
}
|
|
|
|
}
|
2023-09-07 18:07:05 +00:00
|
|
|
|
2023-09-08 21:41:37 +00:00
|
|
|
/// Initialize the tor ffi lib instance if it hasn't already been set. Nothing
|
|
|
|
/// changes if _tor is already been set.
|
2023-09-15 19:51:20 +00:00
|
|
|
void init({
|
|
|
|
required String torDataDirPath,
|
|
|
|
Tor? mockableOverride,
|
|
|
|
}) {
|
2023-09-12 18:49:25 +00:00
|
|
|
_tor ??= mockableOverride ?? Tor.instance;
|
2023-09-15 19:51:20 +00:00
|
|
|
_torDataDirPath ??= torDataDirPath;
|
2023-09-08 21:41:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 22:56:48 +00:00
|
|
|
/// Start the Tor service.
|
|
|
|
///
|
|
|
|
/// This will start the Tor service and establish a Tor circuit.
|
|
|
|
///
|
2023-09-15 21:01:31 +00:00
|
|
|
/// Throws an exception if the Tor library was not inited or if the Tor
|
|
|
|
/// service fails to start.
|
2023-09-07 22:56:48 +00:00
|
|
|
///
|
|
|
|
/// Returns a Future that completes when the Tor service has started.
|
2023-08-07 16:39:04 +00:00
|
|
|
Future<void> start() async {
|
2023-09-15 19:51:20 +00:00
|
|
|
if (_tor == null || _torDataDirPath == null) {
|
2023-09-08 21:41:37 +00:00
|
|
|
throw Exception("TorService.init has not been called!");
|
|
|
|
}
|
|
|
|
|
2023-09-07 22:56:48 +00:00
|
|
|
// Start the Tor service.
|
2023-09-07 18:07:05 +00:00
|
|
|
try {
|
2023-09-15 18:10:51 +00:00
|
|
|
_updateStatusAndFireEvent(
|
|
|
|
status: TorConnectionStatus.connecting,
|
2023-09-15 19:51:20 +00:00
|
|
|
message: "TorService.start call in progress",
|
2023-09-07 21:27:00 +00:00
|
|
|
);
|
2023-09-15 18:10:51 +00:00
|
|
|
|
2023-09-15 19:51:20 +00:00
|
|
|
await _tor!.start(torDataDirPath: _torDataDirPath!);
|
|
|
|
|
2023-09-07 18:07:05 +00:00
|
|
|
// no exception or error so we can (probably?) assume tor
|
|
|
|
// has started successfully
|
2023-09-07 23:28:55 +00:00
|
|
|
// Fire a TorConnectionStatusChangedEvent on the event bus.
|
2023-09-15 18:10:51 +00:00
|
|
|
_updateStatusAndFireEvent(
|
|
|
|
status: TorConnectionStatus.connected,
|
2023-09-15 19:51:20 +00:00
|
|
|
message: "TorService.start call success",
|
2023-09-07 21:27:00 +00:00
|
|
|
);
|
2023-09-15 21:01:23 +00:00
|
|
|
|
|
|
|
// Complete the future.
|
|
|
|
return;
|
2023-09-07 18:07:05 +00:00
|
|
|
} catch (e, s) {
|
|
|
|
Logging.instance.log(
|
|
|
|
"TorService.start failed: $e\n$s",
|
|
|
|
level: LogLevel.Warning,
|
|
|
|
);
|
2023-09-07 21:27:00 +00:00
|
|
|
// _enabled should already be false
|
2023-09-07 23:28:55 +00:00
|
|
|
|
|
|
|
// Fire a TorConnectionStatusChangedEvent on the event bus.
|
2023-09-15 18:10:51 +00:00
|
|
|
_updateStatusAndFireEvent(
|
|
|
|
status: TorConnectionStatus.disconnected,
|
2023-09-15 19:51:20 +00:00
|
|
|
message: "TorService.start call failed",
|
2023-09-07 21:27:00 +00:00
|
|
|
);
|
2023-09-07 18:07:05 +00:00
|
|
|
rethrow;
|
|
|
|
}
|
2023-08-07 16:39:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 17:05:23 +00:00
|
|
|
/// Disable Tor.
|
2023-09-15 19:51:20 +00:00
|
|
|
Future<void> disable() async {
|
2023-09-08 21:41:37 +00:00
|
|
|
if (_tor == null) {
|
|
|
|
throw Exception("TorService.init has not been called!");
|
|
|
|
}
|
|
|
|
|
2024-06-24 17:05:23 +00:00
|
|
|
// No need to update status and fire event if status won't change.
|
2023-09-15 19:51:20 +00:00
|
|
|
if (_status == TorConnectionStatus.disconnected) {
|
2023-09-07 18:07:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-24 19:36:24 +00:00
|
|
|
_tor!.disable();
|
2024-06-24 17:05:23 +00:00
|
|
|
await _tor?.stop();
|
|
|
|
|
2023-09-15 19:51:20 +00:00
|
|
|
_updateStatusAndFireEvent(
|
|
|
|
status: TorConnectionStatus.disconnected,
|
|
|
|
message: "TorService.disable call success",
|
|
|
|
);
|
2023-09-15 21:01:23 +00:00
|
|
|
|
|
|
|
return;
|
2023-08-07 16:39:04 +00:00
|
|
|
}
|
2023-09-15 18:10:51 +00:00
|
|
|
|
|
|
|
void _updateStatusAndFireEvent({
|
|
|
|
required TorConnectionStatus status,
|
|
|
|
required String message,
|
|
|
|
}) {
|
|
|
|
_status = status;
|
|
|
|
GlobalEventBus.instance.fire(
|
|
|
|
TorConnectionStatusChangedEvent(
|
|
|
|
_status,
|
|
|
|
message,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-08-07 16:39:04 +00:00
|
|
|
}
|