remove _socksSocket

This commit is contained in:
sneurlax 2023-08-09 12:19:11 -05:00
parent 0522d2a0ee
commit d8f41cccaf

View file

@ -79,11 +79,7 @@ class JsonRPC {
_requestQueue.nextIncompleteReq.then((req) { _requestQueue.nextIncompleteReq.then((req) {
if (req != null) { if (req != null) {
// \r\n required by electrumx server // \r\n required by electrumx server
if (_socksSocket != null) {
_socksSocket!.write('${req.jsonRequest}\r\n');
} else {
_socket!.write('${req.jsonRequest}\r\n'); _socket!.write('${req.jsonRequest}\r\n');
}
// TODO different timeout length? // TODO different timeout length?
req.initiateTimeout( req.initiateTimeout(
@ -100,7 +96,7 @@ class JsonRPC {
Duration requestTimeout, Duration requestTimeout,
) async { ) async {
await _requestMutex.protect(() async { await _requestMutex.protect(() async {
if (_socket == null && _socksSocket == null) { if (_socket == null) {
Logging.instance.log( Logging.instance.log(
"JsonRPC request: opening socket $host:$port", "JsonRPC request: opening socket $host:$port",
level: LogLevel.Info, level: LogLevel.Info,
@ -154,50 +150,12 @@ class JsonRPC {
} }
Future<void> connect() async { Future<void> connect() async {
if (Prefs.instance.useTor) { if (_socket != null) {
if (_socksSocket != null) {
Logging.instance.log(
"JsonRPC.connect(): JsonRPC attempted to connect to an already existing tor socket!",
level: LogLevel.Error);
throw Exception( throw Exception(
"JsonRPC attempted to connect to an already existing tor socket!", "JsonRPC attempted to connect to an already existing socket!",
); );
} }
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);
}
final InternetAddress _host =
await InternetAddress.lookup(host).then((value) => value.first);
_socksSocket = await SocksTCPClient.connect(
[
ProxySettings(
InternetAddress.loopbackIPv4,
proxyInfo!.port,
)
],
_host,
port,
);
if (_socksSocket == 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);
}
if (Prefs.instance.useTor) { if (Prefs.instance.useTor) {
if (proxyInfo == null) { if (proxyInfo == null) {
// TODO await tor / make sure it's running // TODO await tor / make sure it's running
@ -269,6 +227,13 @@ class JsonRPC {
); );
} }
} }
_subscription = _socket!.listen(
_dataHandler,
onError: _errorHandler,
onDone: _doneHandler,
cancelOnError: true,
);
} }
} }