From fd3bac27eb674004504d8c91396f0fd993358ad5 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Sat, 22 Jun 2024 19:30:24 -0500 Subject: [PATCH] WIP monero test onion node works sometimes but fails most of the time, ffs --- .../test_monero_node_connection.dart | 94 ++++++++++++++----- 1 file changed, 70 insertions(+), 24 deletions(-) diff --git a/lib/utilities/test_monero_node_connection.dart b/lib/utilities/test_monero_node_connection.dart index f7bbfeaf2..7af89f6d8 100644 --- a/lib/utilities/test_monero_node_connection.dart +++ b/lib/utilities/test_monero_node_connection.dart @@ -13,6 +13,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:socks5_proxy/socks.dart'; +import 'package:tor_ffi_plugin/socks_socket.dart'; import '../widgets/desktop/primary_button.dart'; import '../widgets/desktop/secondary_button.dart'; @@ -64,34 +65,79 @@ Future testMoneroNodeConnection( return false; }; - final request = await httpClient.postUrl(uri); + if (!uri.host.endsWith('.onion')) { + final request = await httpClient.postUrl(uri); - final body = utf8.encode( - jsonEncode({ - "jsonrpc": "2.0", - "id": "0", - "method": "get_info", - }), - ); + final body = utf8.encode( + jsonEncode({ + "jsonrpc": "2.0", + "id": "0", + "method": "get_info", + }), + ); - request.headers.add( - 'Content-Length', - body.length.toString(), - preserveHeaderCase: true, - ); - request.headers.set( - 'Content-Type', - 'application/json', - preserveHeaderCase: true, - ); + request.headers.add( + 'Content-Length', + body.length.toString(), + preserveHeaderCase: true, + ); + request.headers.set( + 'Content-Type', + 'application/json', + preserveHeaderCase: true, + ); - request.add(body); + request.add(body); - final response = await request.close(); - final result = await response.transform(utf8.decoder).join(); - // TODO: json decoded without error so assume connection exists? - // or we can check for certain values in the response to decide - return MoneroNodeConnectionResponse(null, null, null, true); + final response = await request.close(); + final result = await response.transform(utf8.decoder).join(); + // TODO: json decoded without error so assume connection exists? + // or we can check for certain values in the response to decide + return MoneroNodeConnectionResponse(null, null, null, true); + } else { + // If the URL ends in .onion, we can't use an httpClient to connect to it. + // + // The SOCKSSocket class from the tor_ffi_plugin package can be used to + // connect to .onion addresses. We'll do the same things as above but + // with SOCKSSocket instead of httpClient. + final socket = await SOCKSSocket.create( + proxyHost: proxyInfo!.host.address, + proxyPort: proxyInfo.port, + sslEnabled: false, + ); + await socket.connect(); + await socket.connectTo(uri.host, uri.port); + + final body = utf8.encode( + jsonEncode({ + "jsonrpc": "2.0", + "id": "0", + "method": "get_info", + }), + ); + + // Write the request body to the socket. + socket.write(body); + + // Read the response. + final response = await socket.inputStream.first; + final result = utf8.decode(response); + + // Close the socket. + await socket.close(); + return MoneroNodeConnectionResponse(null, null, null, true); + + // Parse the response. + // + // This is commented because any issues should throw. + // final Map jsonResponse = jsonDecode(result); + // print(jsonResponse); + // if (jsonResponse.containsKey('result')) { + // return MoneroNodeConnectionResponse(null, null, null, true); + // } else { + // return MoneroNodeConnectionResponse(null, null, null, false); + // } + } } catch (e, s) { if (badCertResponse != null) { return badCertResponse!;