diff --git a/oracle/bin/oracle.dart b/oracle/bin/oracle.dart index f1a0be1..b2e5f64 100644 --- a/oracle/bin/oracle.dart +++ b/oracle/bin/oracle.dart @@ -14,9 +14,11 @@ void main() async { //! TEST final verifier = IVerifier.forType(DomainType.tor); - print(await verifier.verify('onion3yj2gmj7tv3f.onion')); + // print(await verifier.verify( + // 'duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion')); + // print(''); print(await verifier.verify( - 'duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion')); + 'duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzcaaa.onion')); ethClient.subscribeToDomainVerificationEvents().listen((event) async { final verifier = IVerifier.forType(event.type); @@ -29,7 +31,7 @@ void main() async { }, ); await ethClient.sendVerificationResult(event, result).onError( - (error, stackTrace) { + (error, stackTrace) { print('=== Error: $error ==='); print(stackTrace); print(''); diff --git a/oracle/lib/tor.dart b/oracle/lib/tor.dart index c6d8481..b78dcb6 100644 --- a/oracle/lib/tor.dart +++ b/oracle/lib/tor.dart @@ -1,7 +1,7 @@ -import 'dart:io'; -import 'dart:typed_data'; +import 'dart:async'; +import 'dart:convert'; -import 'package:socks5_proxy/socks_client.dart'; +import 'package:socks5_io/socks5_io.dart'; import 'verifier.dart'; @@ -13,47 +13,60 @@ class TorVerifier implements IVerifier { } final torUri = Uri.parse('http://$torAddress'); - // Resolve DNS of the Tor address - final List dns; + // Connect via TCP to port 80 of the Tor address + // using the SOCKS5 Tor proxy on localhost:9150 + final proxy = Socks5ClientDialer("127.0.0.1", 9150, authMethods: [ + AuthMethod.notRequired, + ]); try { - dns = await InternetAddress.lookup(torUri.host); - } on SocketException catch (err) { - print('=== Error: $err ==='); - print(''); + final socket = await proxy.connect(torUri.host, torUri.port); + + final result = Completer(); + complete(bool success) async { + if (result.isCompleted) return; + result.complete(success); + await socket.close(); + } + + // Timeout + Future.delayed( + const Duration(seconds: 30), + () => complete(false), + ); + + // Read 1 byte or handle timeout + connection close + socket.getReader().listen( + (event) { + print('=== Event: ${event.length} ==='); + if (event.isNotEmpty) { + complete(true); + } + }, + onError: (err) { + print('onError'); + print(err); + complete(false); + }, + onDone: () => complete(false), + ); + + // Send a HTTP HEAD request + // and consider the check successful + // iff I receive 1 byte in 5 seconds and the connection stays open + socket.getWriter().add(ascii.encode([ + "GET / HTTP/1.1", + "Host: $torAddress", + "User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0", + "Accept: */*", + "", + "", // An HTTP request must end with 2 CRLF + ].join('\r\n'))); + + return result.future; + } catch (err, stackTrace) { + print(err); + print(stackTrace); return false; } - - // Connect via TCP to port 80 of the Tor address - // using the SOCKS5 Tor proxy on localhost:9150 - final proxySettings = ProxySettings(InternetAddress.loopbackIPv4, 9150); - final client = - SocksTCPClient.connect([proxySettings], dns.first, torUri.port); - final socket = await SocketConnectionTask(client).socket; - - // Send a HTTP HEAD request - // and consider the check successful - // iff I receive 1 byte in 5 seconds and the connection stays open - final httpHeadRequest = """ -GET / HTTP/1.1 -Host: $torAddress -User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0 -Accept: */* -""" - .split('\n') - .map((row) => row.trim()) - .where((row) => row.isNotEmpty) - .join('\r\n'); - - socket.write(httpHeadRequest); - socket.write('\r\n\r\n'); - socket.flush(); - - // Read 1 byte or handle timeout + connection close - final result = await socket.where((data) => data.isNotEmpty).first.timeout( - const Duration(seconds: 5), - onTimeout: () => Uint8List(0), - ); - - return result.isNotEmpty; } } diff --git a/oracle/pubspec.lock b/oracle/pubspec.lock index fd359c5..37a5eba 100644 --- a/oracle/pubspec.lock +++ b/oracle/pubspec.lock @@ -184,14 +184,15 @@ packages: url: "https://pub.dev" source: hosted version: "0.2.0" - socks5_proxy: + socks5_io: dependency: "direct main" description: - name: socks5_proxy - sha256: "1d21b5606169654bbf4cfb904e8e6ed897e9f763358709f87310c757096d909a" - url: "https://pub.dev" - source: hosted - version: "1.0.4" + path: "." + ref: "736109deb205ebe6216efa1709eb40a4d423b369" + resolved-ref: "736109deb205ebe6216efa1709eb40a4d423b369" + url: "https://github.com/simonesestito/dart_socks5_io.git" + source: git + version: "0.2.0" source_span: dependency: transitive description: diff --git a/oracle/pubspec.yaml b/oracle/pubspec.yaml index 1d0a5d2..e2326a5 100644 --- a/oracle/pubspec.yaml +++ b/oracle/pubspec.yaml @@ -12,7 +12,10 @@ dependencies: web3dart: ^2.7.2 data_encoder: path: ../data_encoder - socks5_proxy: ^1.0.4 + socks5_io: + git: + url: https://github.com/simonesestito/dart_socks5_io.git + ref: 736109deb205ebe6216efa1709eb40a4d423b369 dev_dependencies: lints: ^2.1.0