Skip to content

Commit

Permalink
Change library for the Oracle Tor verifier
Browse files Browse the repository at this point in the history
  • Loading branch information
simonesestito committed Jan 17, 2024
1 parent f02581c commit c708a58
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 52 deletions.
8 changes: 5 additions & 3 deletions oracle/bin/oracle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -29,7 +31,7 @@ void main() async {
},
);
await ethClient.sendVerificationResult(event, result).onError(
(error, stackTrace) {
(error, stackTrace) {
print('=== Error: $error ===');
print(stackTrace);
print('');
Expand Down
97 changes: 55 additions & 42 deletions oracle/lib/tor.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,47 +13,60 @@ class TorVerifier implements IVerifier {
}
final torUri = Uri.parse('http://$torAddress');

// Resolve DNS of the Tor address
final List<InternetAddress> 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<bool>();
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;
}
}
13 changes: 7 additions & 6 deletions oracle/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion oracle/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c708a58

Please sign in to comment.