From 973e60c91c0f6b994b2850d2496b36e959389e70 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Mon, 22 Apr 2024 16:57:25 -0500 Subject: [PATCH] Revert "Tor support: add optional proxyInfo property and use it if not null." This reverts commit 2d7189d31f1bfd5d6779268c81a897f03f339f5d. --- packages/solana/lib/src/rpc/client.dart | 2 - .../solana/lib/src/rpc/json_rpc_client.dart | 90 +++++-------------- packages/solana/pubspec.yaml | 1 - 3 files changed, 21 insertions(+), 72 deletions(-) diff --git a/packages/solana/lib/src/rpc/client.dart b/packages/solana/lib/src/rpc/client.dart index fdc83dbef2..dda868e8f7 100644 --- a/packages/solana/lib/src/rpc/client.dart +++ b/packages/solana/lib/src/rpc/client.dart @@ -18,7 +18,6 @@ abstract class RpcClient { String url, { Duration timeout = const Duration(seconds: 30), Map customHeaders = const {}, - Map? proxyInfo, // {host: String, port: int} }) => _RpcClient( url, @@ -26,7 +25,6 @@ abstract class RpcClient { url, timeout: timeout, customHeaders: customHeaders, - proxyInfo: proxyInfo ?? {}, ), ); diff --git a/packages/solana/lib/src/rpc/json_rpc_client.dart b/packages/solana/lib/src/rpc/json_rpc_client.dart index 7f4d393cdc..d12abc6087 100644 --- a/packages/solana/lib/src/rpc/json_rpc_client.dart +++ b/packages/solana/lib/src/rpc/json_rpc_client.dart @@ -1,9 +1,7 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io'; -import 'dart:typed_data'; -import 'package:socks5_proxy/socks.dart'; +import 'package:http/http.dart'; import 'package:solana/src/exceptions/http_exception.dart'; import 'package:solana/src/exceptions/json_rpc_exception.dart'; import 'package:solana/src/exceptions/rpc_timeout_exception.dart'; @@ -14,15 +12,12 @@ class JsonRpcClient { this._url, { required Duration timeout, required Map customHeaders, - required Map proxyInfo, // {host: String, port: int} }) : _timeout = timeout, - _headers = {..._defaultHeaders, ...customHeaders}, - _proxyInfo = proxyInfo; + _headers = {..._defaultHeaders, ...customHeaders}; final String _url; final Duration _timeout; final Map _headers; - final Map _proxyInfo; int _lastId = 1; Future>> bulkRequest( @@ -73,51 +68,26 @@ class JsonRpcClient { Future<_JsonRpcResponse> _postRequest( JsonRpcRequest request, ) async { - final Uri uri = Uri.parse(_url); - final HttpClient httpClient = HttpClient(); - - try { - // If proxyInfo is provided, configure the proxy. - if (_proxyInfo.isNotEmpty) { - SocksTCPClient.assignToHttpClient(httpClient, [ - ProxySettings( - InternetAddress(_proxyInfo['host'] as String), - _proxyInfo['port'] as int, - ), - ]); - } - - final HttpClientRequest httpClientRequest = await httpClient.postUrl(uri); - _headers - .forEach((key, value) => httpClientRequest.headers.set(key, value)); - httpClientRequest.write(json.encode(request.toJson())); - - final HttpClientResponse response = - await httpClientRequest.close().timeout( - _timeout, - onTimeout: () { - throw RpcTimeoutException( - method: request.method, - body: json.encode(request.toJson()), - timeout: _timeout, - ); - }, - ); - - // Consolidate the bytes and parse the response. - final Uint8List bodyBytes = - await consolidateHttpClientResponseBytes(response); - final String responseBody = utf8.decode(bodyBytes); - final int statusCode = response.statusCode; - - if (statusCode == 200) { - return _JsonRpcResponse._parse(json.decode(responseBody)); - } - - throw HttpException(statusCode, responseBody); - } finally { - httpClient.close(); + final body = request.toJson(); + // Perform the POST request + final Response response = await post( + Uri.parse(_url), + headers: _headers, + body: json.encode(body), + ).timeout( + _timeout, + onTimeout: () => throw RpcTimeoutException( + method: request.method, + body: body, + timeout: _timeout, + ), + ); + // Handle the response + if (response.statusCode == 200) { + return _JsonRpcResponse._parse(json.decode(response.body)); } + + throw HttpException(response.statusCode, response.body); } } @@ -178,21 +148,3 @@ class _JsonRpcArrayResponse implements _JsonRpcResponse { const _defaultHeaders = { 'Content-Type': 'application/json', }; - -/// Helper function to consolidate HttpClientResponse bytes. -/// -/// Helps ensure HttpClientResponse is fully read before closing the connection. -/// Helper for proxied connections. -Future consolidateHttpClientResponseBytes( - HttpClientResponse response) { - final Completer completer = Completer(); - final List bytes = []; - response.listen( - bytes.addAll, - onDone: () => completer.complete(Uint8List.fromList(bytes)), - onError: completer.completeError, - cancelOnError: true, - ); - - return completer.future; -} diff --git a/packages/solana/pubspec.yaml b/packages/solana/pubspec.yaml index 08d155aa72..2ba95ebe40 100644 --- a/packages/solana/pubspec.yaml +++ b/packages/solana/pubspec.yaml @@ -19,7 +19,6 @@ dependencies: freezed_annotation: ^2.0.0 http: ^1.1.0 json_annotation: ^4.4.0 - socks5_proxy: ^1.0.4 typed_data: ^1.3.0 web_socket_channel: ^2.1.0