Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: proxy (Tor/SOCKS5) support: add optional proxy parameter #103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/src/core/client/impl/tezart_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class TezartClient {
final RpcInterface rpcInterface;

/// Default constructor.
TezartClient(String url) : rpcInterface = RpcInterface(url);
TezartClient(String url, {String? proxy})
: rpcInterface = RpcInterface(url, proxy: proxy);

/// Returns an [OperationsList] containing a [TransactionOperation] that transfers [amount] from [source]
/// to [destination] and returns the operation group id.\
Expand Down
3 changes: 2 additions & 1 deletion lib/src/core/rpc/impl/rpc_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class RpcInterface {
final TezartHttpClient httpClient;
final log = Logger('RpcInterface');

RpcInterface(String url) : httpClient = TezartHttpClient(url);
RpcInterface(String url, {String? proxy})
: httpClient = TezartHttpClient(url, proxy: proxy);

/// Returns the block's hash of [chain] and [level]
Future<String> branch([chain = 'main', level = 'head']) async {
Expand Down
37 changes: 24 additions & 13 deletions lib/src/core/rpc/impl/tezart_http_client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:dio/adapter.dart';
import 'package:dio/dio.dart' as http_client;
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import 'package:logging/logging.dart';
Expand All @@ -13,26 +14,36 @@ class TezartHttpClient {
final String url;

// Add client as optional parameter for testing
TezartHttpClient(this.url, {http_client.Dio? client}) {
TezartHttpClient(this.url, {http_client.Dio? client, String? proxy}) {
// ensure that the url ends with '/' (double / is ok)
final baseUrl = '$url/';

if (client != null) {
this.client = client;
this.client.options.baseUrl = baseUrl;
return;
}
} else {
var options = http_client.BaseOptions(
baseUrl: baseUrl, contentType: 'application/json');

this.client = http_client.Dio(options);
this.client.interceptors.add(PrettyDioLogger(
logPrint: log.finest,
requestHeader: true,
requestBody: true,
responseBody: true,
responseHeader: false,
compact: false,
));

final options = http_client.BaseOptions(baseUrl: baseUrl, contentType: 'application/json');
this.client = http_client.Dio(options);
this.client.interceptors.add(PrettyDioLogger(
logPrint: log.finest,
requestHeader: true,
requestBody: true,
responseBody: true,
responseHeader: false,
compact: false,
));
if (proxy != null) {
this.client.httpClientAdapter = DefaultHttpClientAdapter()
..onHttpClientCreate = (HttpClient httpClient) {
httpClient.findProxy = (_) => 'PROXY $proxy';
// Ignore SSL errors if required for development with self-signed certificates
httpClient.badCertificateCallback = (cert, host, port) => true;
};
}
}
}

Future<http_client.Response> post(String path, {dynamic data}) {
Expand Down