diff --git a/lib/main.dart b/lib/main.dart index fcb040c..3f11a20 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -18,11 +18,12 @@ import 'package:provider/provider.dart'; import 'package:haveno_flutter_app/services/haveno_service.dart'; import 'package:haveno_flutter_app/services/http_service.dart'; import 'package:haveno_flutter_app/services/monero_service.dart'; -//import 'package:haveno_flutter_app/services/tor_service.dart'; +import 'package:haveno_flutter_app/services/tor_service.dart'; import 'package:haveno_flutter_app/providers/get_version_provider.dart'; import 'package:haveno_flutter_app/providers/account_provider.dart'; import 'dart:async'; -import 'package:badges/badges.dart' as badges; // Import the badges package +import 'package:badges/badges.dart' as badges; +import 'package:tor/tor.dart'; // Import the badges package void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -31,10 +32,19 @@ void main() async { _setupLogging(); // Initialize services - //final torService = TorService(); - //await torService.initializeTor(); + Tor tor = Tor(); // Constructing the Tor instance + TorService torService = TorService(tor); + + // Strart service and listen + torService.startService(); + //torService.listenToTorServiceEvents(); + torService.checkControlPort(); + torService.checkSocks5Port(); + final httpService = HttpService(); + final myIP = await httpService.request('GET', 'https://api.ipify.org?format=json'); + debugPrint(myIP.toString()); final moneroService = MoneroService(); final havenoService = HavenoService('127.0.0.1', 3201, 'apitest'); diff --git a/lib/services/tor_service.dart b/lib/services/tor_service.dart index 47536a0..a0d8db1 100644 --- a/lib/services/tor_service.dart +++ b/lib/services/tor_service.dart @@ -1,53 +1,92 @@ import 'dart:async'; -import 'dart:convert'; import 'dart:io'; -import 'package:flutter/services.dart' show rootBundle; +import 'package:flutter/foundation.dart'; import 'package:tor/tor.dart'; class TorService { - final Tor _tor = Tor(); - StreamSubscription? _torStatusSubscription; - final StreamController _statusController = - StreamController.broadcast(); - final StreamController _controlPortStatusController = - StreamController.broadcast(); + StreamSubscription? _torStatusSubscription; + final StreamController _statusController = StreamController.broadcast(); + final StreamController _controlPortStatusController = StreamController.broadcast(); Stream get statusStream => _statusController.stream; - Stream get controlPortStatusStream => - _controlPortStatusController.stream; + Stream get controlPortStatusStream => _controlPortStatusController.stream; final String _controlAddress = '127.0.0.1'; final int _controlPort = 9051; - Future initializeTor() async { - _torStatusSubscription = Tor.torEvents.listen((String status) { - _statusController.add(status); - }); + final Tor _tor; + TorService(this._tor); + + Future startService() async { try { - await _tor.startTor(); - //_statusController.add("Tor started successfully!"); - //await _checkControlPort(); + await _tor.startService("ControlPort 9051\nCookieAuthentication 0"); + debugPrint("Tor service started"); + _statusController.add("Tor service started."); } catch (e) { + debugPrint("Tor service failed to start: $e"); _statusController.add("Failed to start Tor: $e"); } } - Future _checkControlPort() async { + Future stopService() async { + try { + await _tor.stopService(); + _statusController.add("Tor service stopped."); + } catch (e) { + _statusController.add("Failed to stop Tor: $e"); + } + } + + Future initializeDaemonHiddenService({required String hostname, required String privateKey}) async { + try { + final hiddenServiceInfo = await _tor.initializeHiddenService( + listenPort: 3201, + exposePort: 12134, + privateKey: privateKey, + ); + _statusController.add("Hidden service initialized: $hiddenServiceInfo"); + } catch (e) { + _statusController.add("Failed to initialize hidden service: $e"); + } + } + + Future checkControlPort() async { _controlPortStatusController.add("Checking control port..."); try { - final socket = await Socket.connect(_controlAddress, _controlPort, - timeout: Duration(seconds: 10)); + final socket = await Socket.connect(_controlAddress, _controlPort, timeout: Duration(seconds: 10)); socket.destroy(); + debugPrint("Tor control port is open"); _controlPortStatusController.add("Control port is open and listening."); } catch (e) { + debugPrint("Failed to connect to Tor control port"); _controlPortStatusController.add("Control port connection failed: $e"); } } - Future dispose() async { - await _torStatusSubscription?.cancel(); - await _statusController.close(); - await _controlPortStatusController.close(); + Future checkSocks5Port() async { + _controlPortStatusController.add("Checking control port..."); + try { + final socket = await Socket.connect('127.0.0.1', 9050, timeout: Duration(seconds: 10)); + socket.destroy(); + debugPrint("Tor control port is open"); + _controlPortStatusController.add("Control port is open and listening."); + } catch (e) { + debugPrint("Failed to connect to Tor control port"); + _controlPortStatusController.add("Control port connection failed: $e"); + } } -} + + void listenToTorServiceEvents() { + _torStatusSubscription = _tor.torServiceEvents.listen((event) { + _statusController.add("Tor event: $event"); + debugPrint(event); + }); + } + + void dispose() { + _torStatusSubscription?.cancel(); + _statusController.close(); + _controlPortStatusController.close(); + } +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 9d2a945..a15c46a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -549,6 +549,13 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.0" + tor: + dependency: "direct main" + description: + path: "../Flutter Plugins/tor" + relative: true + source: path + version: "0.0.1" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 11938d2..09215a5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -29,8 +29,8 @@ dependencies: font_awesome_flutter: ^10.7.0 timeline_tile: ^2.0.0 # Add your plugin dependency here - ##tor: - ## path: ../flutter_plugins/tor + tor: + path: "../Flutter Plugins/tor" protoc_plugin: ^21.1.2 fixnum: ^1.1.0 intl: ^0.17.0