Skip to content

Commit

Permalink
Tor support: add optional HttpClient parameter for Tor/SOCKS5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
sneurlax committed Apr 24, 2024
1 parent 973e60c commit 4b727c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
3 changes: 3 additions & 0 deletions packages/solana/lib/src/rpc/client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';

import 'package:json_annotation/json_annotation.dart';
import 'package:solana/encoder.dart';
Expand All @@ -18,13 +19,15 @@ abstract class RpcClient {
String url, {
Duration timeout = const Duration(seconds: 30),
Map<String, String> customHeaders = const {},
HttpClient? httpClient,
}) =>
_RpcClient(
url,
JsonRpcClient(
url,
timeout: timeout,
customHeaders: customHeaders,
httpClient: httpClient,
),
);

Expand Down
36 changes: 17 additions & 19 deletions packages/solana/lib/src/rpc/json_rpc_client.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

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';
import 'package:solana/src/rpc/json_rpc_request.dart';

class JsonRpcClient {
JsonRpcClient(
this._url, {
required Duration timeout,
required Map<String, String> customHeaders,
HttpClient? httpClient, // Optional client for proxy support.
}) : _timeout = timeout,
_headers = {..._defaultHeaders, ...customHeaders};
_headers = {..._defaultHeaders, ...customHeaders},
_httpClient = httpClient ?? HttpClient();

final String _url;
final Duration _timeout;
final Map<String, String> _headers;
final HttpClient _httpClient;
int _lastId = 1;

static get http => null;

Future<List<Map<String, dynamic>>> bulkRequest(
String method,
List<List<dynamic>> params,
Expand Down Expand Up @@ -65,29 +69,23 @@ class JsonRpcClient {
throw const FormatException('unexpected jsonrpc response type');
}

/// Sends a POST request to the server.
Future<_JsonRpcResponse> _postRequest(
JsonRpcRequest request,
) async {
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,
),
);
final HttpClientRequest httpRequest =
await _httpClient.postUrl(Uri.parse(_url));
_headers.forEach((key, value) => httpRequest.headers.add(key, value));
httpRequest.write(json.encode(body));
// Handle the response
if (response.statusCode == 200) {
return _JsonRpcResponse._parse(json.decode(response.body));
final HttpClientResponse httpResponse = await httpRequest.close();
final responseBody = await httpResponse.transform(utf8.decoder).join();
if (httpResponse.statusCode == 200) {
return _JsonRpcResponse._parse(json.decode(responseBody));
}

throw HttpException(response.statusCode, response.body);
throw HttpException(httpResponse.statusCode, responseBody);
}
}

Expand Down

0 comments on commit 4b727c2

Please sign in to comment.