cake_wallet/lib/view_model/settings/tor_view_model.dart

90 lines
2.5 KiB
Dart
Raw Normal View History

2024-01-04 20:08:27 +00:00
import 'dart:io';
import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/store/app_store.dart';
2023-12-20 01:20:21 +00:00
import 'package:cake_wallet/store/settings_store.dart';
2024-01-04 18:43:11 +00:00
import 'package:cake_wallet/view_model/settings/tor_connection.dart';
2023-12-20 01:20:21 +00:00
import 'package:mobx/mobx.dart';
2024-01-12 19:53:19 +00:00
import 'package:socks5_proxy/socks_client.dart';
2024-01-04 18:43:11 +00:00
import 'package:tor/tor.dart';
2023-12-20 01:20:21 +00:00
part 'tor_view_model.g.dart';
class TorViewModel = TorViewModelBase with _$TorViewModel;
2024-01-04 18:43:11 +00:00
enum TorConnectionStatus { connecting, connected, disconnected }
2023-12-20 01:20:21 +00:00
2024-01-04 18:43:11 +00:00
abstract class TorViewModelBase with Store {
2024-01-04 20:08:27 +00:00
TorViewModelBase(this._settingsStore) {
reaction((_) => torConnectionMode, (TorConnectionMode mode) async {
if (mode == TorConnectionMode.enabled || mode == TorConnectionMode.onionOnly) {
startTor();
} else {
stopTor();
}
});
}
2023-12-20 01:20:21 +00:00
final SettingsStore _settingsStore;
@action
Future<void> updateStartOnLaunch(bool value) async {
_settingsStore.shouldStartTorOnLaunch = value;
}
2024-01-04 18:43:11 +00:00
@computed
TorConnectionMode get torConnectionMode => _settingsStore.torConnectionMode;
2024-01-04 20:08:27 +00:00
@observable
TorConnectionStatus torConnectionStatus = TorConnectionStatus.disconnected;
2024-01-04 18:43:11 +00:00
@action
void setTorConnectionMode(TorConnectionMode mode) => _settingsStore.torConnectionMode = mode;
2024-01-04 20:08:27 +00:00
@action
Future<void> startTor() async {
try {
torConnectionStatus = TorConnectionStatus.connecting;
2024-01-12 19:53:19 +00:00
2024-01-04 20:08:27 +00:00
await Tor.init();
2024-01-12 19:53:19 +00:00
// start only if not already running:
if (Tor.instance.port == -1) {
await Tor.instance.enable();
}
2024-01-04 18:43:11 +00:00
2024-01-04 20:08:27 +00:00
_settingsStore.shouldStartTorOnLaunch = true;
torConnectionStatus = TorConnectionStatus.connected;
2024-01-04 18:43:11 +00:00
2024-01-12 19:53:19 +00:00
SocksTCPClient.setProxy(proxies: [
ProxySettings(
InternetAddress.loopbackIPv4,
Tor.instance.port,
password: null,
),
]);
2024-01-04 20:08:27 +00:00
// connect to node through the proxy:
final appStore = getIt.get<AppStore>();
if (appStore.wallet != null) {
final node = _settingsStore.getCurrentNode(appStore.wallet!.type);
if (node.socksProxyAddress?.isEmpty ?? true) {
node.socksProxyAddress = "${InternetAddress.loopbackIPv4.address}:${Tor.instance.port}";
}
appStore.wallet!.connectToNode(node: node);
}
} catch (e) {
torConnectionStatus = TorConnectionStatus.disconnected;
2024-01-04 18:43:11 +00:00
}
2024-01-04 20:08:27 +00:00
}
2024-01-04 18:43:11 +00:00
2024-01-04 20:08:27 +00:00
@action
Future<void> stopTor() async {
Tor.instance.disable();
_settingsStore.shouldStartTorOnLaunch = false;
torConnectionStatus = TorConnectionStatus.disconnected;
2024-01-12 19:53:19 +00:00
SocksTCPClient.setProxy(proxies: null);
2024-01-04 18:43:11 +00:00
}
2023-12-20 01:20:21 +00:00
}