add required proxyInfo param to JsonRPC class

This commit is contained in:
sneurlax 2023-08-09 12:13:26 -05:00
parent ec5ae60a61
commit 9e7567a26a
2 changed files with 76 additions and 12 deletions
lib/electrumx_rpc
test

View file

@ -14,7 +14,9 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:mutex/mutex.dart';
import 'package:stackwallet/networking/tor_service.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
// Json RPC class to handle connecting to electrumx servers
class JsonRPC {
@ -23,15 +25,17 @@ class JsonRPC {
required this.port,
this.useSSL = false,
this.connectionTimeout = const Duration(seconds: 60),
required ({String host, int port})? proxyInfo,
});
final bool useSSL;
final String host;
final int port;
final Duration connectionTimeout;
({String host, int port})? proxyInfo;
final _requestMutex = Mutex();
final _JsonRPCRequestQueue _requestQueue = _JsonRPCRequestQueue();
Socket? _socket;
Socket? _socket; // TODO make a SocksSocket extension/wrapper or similar
StreamSubscription<Uint8List>? _subscription;
void _dataHandler(List<int> data) {
@ -152,19 +156,76 @@ class JsonRPC {
);
}
if (useSSL) {
_socket = await SecureSocket.connect(
host,
port,
timeout: connectionTimeout,
onBadCertificate: (_) => true,
); // TODO do not automatically trust bad certificates
} else {
if (Prefs.instance.useTor) {
if (proxyInfo == null) {
// TODO await tor / make sure it's running
proxyInfo = (
host: InternetAddress.loopbackIPv4.address,
port: TorService.sharedInstance.port
);
Logging.instance.log(
"ElectrumX.connect(): no tor proxy info, read $proxyInfo",
level: LogLevel.Warning);
}
// TODO connect to proxy socket...
// https://github.com/LacticWhale/socks_dart/blob/master/lib/src/client/socks_client.dart#L50C46-L50C56
// TODO implement ssl over tor
// if (useSSL) {
// _socket = await SecureSocket.connect(
// host,
// port,
// timeout: connectionTimeout,
// onBadCertificate: (_) => true,
// ); // TODO do not automatically trust bad certificates
// final _client = SocksSocket.protected(_socket, type);
// } else {
_socket = await Socket.connect(
host,
port,
proxyInfo!.host,
proxyInfo!.port,
timeout: connectionTimeout,
);
// final _client = SocksSocket.protected(
// _socket!, SocksConnectionType.connect
// );
// final InternetAddress _host =
// await InternetAddress.lookup(host).then((value) => value.first);
// var _socket = await SocksSocket.initialize(
// [
// ProxySettings(
// InternetAddress.loopbackIPv4,
// proxyInfo!.port,
// )
// ],
// _host,
// port,
// SocksConnectionType.connect,
// );
if (_socket == null) {
Logging.instance.log(
"JsonRPC.connect(): failed to connect to $host over tor proxy at $proxyInfo",
level: LogLevel.Error);
throw Exception("JsonRPC.connect(): failed to connect to tor proxy");
} else {
Logging.instance.log(
"JsonRPC.connect(): connected to $host over tor proxy at $proxyInfo",
level: LogLevel.Info);
}
} else {
if (useSSL) {
_socket = await SecureSocket.connect(
host,
port,
timeout: connectionTimeout,
onBadCertificate: (_) => true,
); // TODO do not automatically trust bad certificates
} else {
_socket = await Socket.connect(
host,
port,
timeout: connectionTimeout,
);
}
}
_subscription = _socket!.listen(

View file

@ -11,6 +11,7 @@ void main() {
port: DefaultNodes.bitcoin.port,
useSSL: true,
connectionTimeout: const Duration(seconds: 40),
proxyInfo: null, // TODO test for proxyInfo
);
const jsonRequestString =
@ -27,7 +28,8 @@ void main() {
final jsonRPC = JsonRPC(
host: "some.bad.address.thingdsfsdfsdaf",
port: 3000,
connectionTimeout: Duration(seconds: 10),
connectionTimeout: const Duration(seconds: 10),
proxyInfo: null,
);
const jsonRequestString =
@ -47,6 +49,7 @@ void main() {
port: 3000,
useSSL: false,
connectionTimeout: const Duration(seconds: 1),
proxyInfo: null,
);
const jsonRequestString =