mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-03-20 22:28:48 +00:00
julian's patch
love it
This commit is contained in:
parent
031bfca492
commit
c2d6b84129
1 changed files with 57 additions and 82 deletions
|
@ -38,34 +38,62 @@ Future<MoneroNodeConnectionResponse> testMoneroNodeConnection(
|
||||||
int port,
|
int port,
|
||||||
})? proxyInfo,
|
})? proxyInfo,
|
||||||
}) async {
|
}) async {
|
||||||
final httpClient = HttpClient();
|
if (uri.host.endsWith(".onion")) {
|
||||||
MoneroNodeConnectionResponse? badCertResponse;
|
if (proxyInfo == null) {
|
||||||
|
// If the host ends in .onion, we can't access it without Tor.
|
||||||
try {
|
return MoneroNodeConnectionResponse(null, null, null, false);
|
||||||
if (proxyInfo != null) {
|
|
||||||
SocksTCPClient.assignToHttpClient(httpClient, [
|
|
||||||
ProxySettings(
|
|
||||||
proxyInfo.host,
|
|
||||||
proxyInfo.port,
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
httpClient.badCertificateCallback = (cert, url, port) {
|
SOCKSSocket? socket;
|
||||||
if (allowBadX509Certificate) {
|
try {
|
||||||
return true;
|
// An HttpClient cannot be used for onion nodes.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
socket = await SOCKSSocket.create(
|
||||||
|
proxyHost: proxyInfo.host.address,
|
||||||
|
proxyPort: proxyInfo.port,
|
||||||
|
sslEnabled: false,
|
||||||
|
);
|
||||||
|
await socket.connect();
|
||||||
|
await socket.connectTo(uri.host, uri.port);
|
||||||
|
// assume success as connect did not fail
|
||||||
|
return MoneroNodeConnectionResponse(null, null, null, true);
|
||||||
|
} catch (e, s) {
|
||||||
|
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
|
||||||
|
return MoneroNodeConnectionResponse(null, null, null, false);
|
||||||
|
} finally {
|
||||||
|
await socket?.close();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
final httpClient = HttpClient();
|
||||||
|
MoneroNodeConnectionResponse? badCertResponse;
|
||||||
|
try {
|
||||||
|
if (proxyInfo != null) {
|
||||||
|
SocksTCPClient.assignToHttpClient(httpClient, [
|
||||||
|
ProxySettings(
|
||||||
|
proxyInfo.host,
|
||||||
|
proxyInfo.port,
|
||||||
|
),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (badCertResponse == null) {
|
httpClient.badCertificateCallback = (cert, url, port) {
|
||||||
badCertResponse = MoneroNodeConnectionResponse(cert, url, port, false);
|
if (allowBadX509Certificate) {
|
||||||
} else {
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (badCertResponse == null) {
|
||||||
|
badCertResponse =
|
||||||
|
MoneroNodeConnectionResponse(cert, url, port, false);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
};
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!uri.host.endsWith(".onion")) {
|
|
||||||
final request = await httpClient.postUrl(uri);
|
final request = await httpClient.postUrl(uri);
|
||||||
|
|
||||||
final body = utf8.encode(
|
final body = utf8.encode(
|
||||||
|
@ -94,69 +122,16 @@ Future<MoneroNodeConnectionResponse> testMoneroNodeConnection(
|
||||||
// TODO: json decoded without error so assume connection exists?
|
// TODO: json decoded without error so assume connection exists?
|
||||||
// or we can check for certain values in the response to decide
|
// or we can check for certain values in the response to decide
|
||||||
return MoneroNodeConnectionResponse(null, null, null, true);
|
return MoneroNodeConnectionResponse(null, null, null, true);
|
||||||
} else {
|
} catch (e, s) {
|
||||||
if (proxyInfo == null) {
|
if (badCertResponse != null) {
|
||||||
// If the host ends in .onion, we can't access it without Tor.
|
return badCertResponse!;
|
||||||
|
} else {
|
||||||
|
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
|
||||||
return MoneroNodeConnectionResponse(null, null, null, false);
|
return MoneroNodeConnectionResponse(null, null, null, false);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
// An HttpClient cannot be used for onion nodes.
|
httpClient.close(force: true);
|
||||||
//
|
|
||||||
// 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);
|
|
||||||
|
|
||||||
// If this is an onion address and there are no errors yet, assume success.
|
|
||||||
if (uri.host.endsWith('.onion')) {
|
|
||||||
return MoneroNodeConnectionResponse(null, null, null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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<String, dynamic> 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!;
|
|
||||||
} else {
|
|
||||||
Logging.instance.log("$e\n$s", level: LogLevel.Warning);
|
|
||||||
return MoneroNodeConnectionResponse(null, null, null, false);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
httpClient.close(force: true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue