From 5053e7d97a6a767600d354adc2efcdd01190c17f Mon Sep 17 00:00:00 2001 From: ryleedavis Date: Mon, 11 Sep 2023 14:20:40 -0600 Subject: [PATCH] WIP: tor http connection --- .../global_settings_view/about_view.dart | 17 +++++++++---- .../majestic_bank/majestic_bank_api.dart | 15 +++++++----- .../exchange/simpleswap/simpleswap_api.dart | 21 +++++++++------- lib/services/price.dart | 22 ++++++++++++----- lib/utilities/test_epic_box_connection.dart | 24 ++++++++++++------- 5 files changed, 65 insertions(+), 34 deletions(-) diff --git a/lib/pages/settings_views/global_settings_view/about_view.dart b/lib/pages/settings_views/global_settings_view/about_view.dart index 5daa1e81a..0d3e8d811 100644 --- a/lib/pages/settings_views/global_settings_view/about_view.dart +++ b/lib/pages/settings_views/global_settings_view/about_view.dart @@ -15,11 +15,13 @@ import 'package:flutter/material.dart'; import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS; import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS; import 'package:flutter_riverpod/flutter_riverpod.dart'; -import 'package:http/http.dart'; import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS; import 'package:package_info_plus/package_info_plus.dart'; +import 'package:stackwallet/networking/http.dart'; +import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/prefs.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; @@ -39,14 +41,17 @@ Future doesCommitExist( String commit, ) async { Logging.instance.log("doesCommitExist", level: LogLevel.Info); - final Client client = Client(); + // final Client client = Client(); + HTTP client = HTTP(); try { final uri = Uri.parse( "$kGithubAPI$kGithubHead/$organization/$project/commits/$commit"); final commitQuery = await client.get( - uri, + url: uri, headers: {'Content-Type': 'application/json'}, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); final response = jsonDecode(commitQuery.body.toString()); @@ -76,14 +81,16 @@ Future isHeadCommit( String commit, ) async { Logging.instance.log("doesCommitExist", level: LogLevel.Info); - final Client client = Client(); + HTTP client = HTTP(); try { final uri = Uri.parse( "$kGithubAPI$kGithubHead/$organization/$project/commits/$branch"); final commitQuery = await client.get( - uri, + url: uri, headers: {'Content-Type': 'application/json'}, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); final response = jsonDecode(commitQuery.body.toString()); diff --git a/lib/services/exchange/majestic_bank/majestic_bank_api.dart b/lib/services/exchange/majestic_bank/majestic_bank_api.dart index 613899f9c..d32209ae5 100644 --- a/lib/services/exchange/majestic_bank/majestic_bank_api.dart +++ b/lib/services/exchange/majestic_bank/majestic_bank_api.dart @@ -11,7 +11,6 @@ import 'dart:convert'; import 'package:decimal/decimal.dart'; -import 'package:http/http.dart' as http; import 'package:stackwallet/exceptions/exchange/exchange_exception.dart'; import 'package:stackwallet/exceptions/exchange/majestic_bank/mb_exception.dart'; import 'package:stackwallet/exceptions/exchange/pair_unavailable_exception.dart'; @@ -20,8 +19,11 @@ import 'package:stackwallet/models/exchange/majestic_bank/mb_order.dart'; import 'package:stackwallet/models/exchange/majestic_bank/mb_order_calculation.dart'; import 'package:stackwallet/models/exchange/majestic_bank/mb_order_status.dart'; import 'package:stackwallet/models/exchange/majestic_bank/mb_rate.dart'; +import 'package:stackwallet/networking/http.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; +import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/prefs.dart'; class MajesticBankAPI { static const String scheme = "https"; @@ -35,22 +37,23 @@ class MajesticBankAPI { static MajesticBankAPI get instance => _instance; - /// set this to override using standard http client. Useful for testing - http.Client? client; + HTTP client = HTTP(); Uri _buildUri({required String endpoint, Map? params}) { return Uri.https(authority, "/api/$version/$endpoint", params); } Future _makeGetRequest(Uri uri) async { - final client = this.client ?? http.Client(); + // final client = this.client ?? http.Client(); int code = -1; try { final response = await client.get( - uri, + url: uri, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); - code = response.statusCode; + code = response.code; final parsed = jsonDecode(response.body); diff --git a/lib/services/exchange/simpleswap/simpleswap_api.dart b/lib/services/exchange/simpleswap/simpleswap_api.dart index adca1f567..d52f8aa21 100644 --- a/lib/services/exchange/simpleswap/simpleswap_api.dart +++ b/lib/services/exchange/simpleswap/simpleswap_api.dart @@ -12,7 +12,6 @@ import 'dart:convert'; import 'package:decimal/decimal.dart'; import 'package:flutter/foundation.dart'; -import 'package:http/http.dart' as http; import 'package:stackwallet/exceptions/exchange/exchange_exception.dart'; import 'package:stackwallet/external_api_keys.dart'; import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.dart'; @@ -20,9 +19,12 @@ import 'package:stackwallet/models/exchange/response_objects/range.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; import 'package:stackwallet/models/exchange/simpleswap/sp_currency.dart'; import 'package:stackwallet/models/isar/exchange_cache/pair.dart'; +import 'package:stackwallet/networking/http.dart'; import 'package:stackwallet/services/exchange/exchange_response.dart'; import 'package:stackwallet/services/exchange/simpleswap/simpleswap_exchange.dart'; +import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/prefs.dart'; import 'package:tuple/tuple.dart'; import 'package:uuid/uuid.dart'; @@ -34,22 +36,22 @@ class SimpleSwapAPI { static final SimpleSwapAPI _instance = SimpleSwapAPI._(); static SimpleSwapAPI get instance => _instance; - /// set this to override using standard http client. Useful for testing - http.Client? client; + HTTP client = HTTP(); Uri _buildUri(String path, Map? params) { return Uri.https(authority, path, params); } Future _makeGetRequest(Uri uri) async { - final client = this.client ?? http.Client(); int code = -1; try { final response = await client.get( - uri, + url: uri, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); - code = response.statusCode; + code = response.code; final parsed = jsonDecode(response.body); @@ -67,15 +69,16 @@ class SimpleSwapAPI { Uri uri, Map body, ) async { - final client = this.client ?? http.Client(); try { final response = await client.post( - uri, + url: uri, headers: {'Content-Type': 'application/json'}, body: jsonEncode(body), + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); - if (response.statusCode == 200) { + if (response.code == 200) { final parsed = jsonDecode(response.body); return parsed; } diff --git a/lib/services/price.dart b/lib/services/price.dart index c6f28673f..91e9b1db8 100644 --- a/lib/services/price.dart +++ b/lib/services/price.dart @@ -13,8 +13,9 @@ import 'dart:convert'; import 'package:decimal/decimal.dart'; import 'package:flutter/foundation.dart'; -import 'package:http/http.dart'; import 'package:stackwallet/db/hive/db.dart'; +import 'package:stackwallet/networking/http.dart'; +import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/prefs.dart'; @@ -32,7 +33,8 @@ class PriceAPI { static const Duration refreshIntervalDuration = Duration(seconds: refreshInterval); - final Client client; + // final Client client; + HTTP client = HTTP(); PriceAPI(this.client); @@ -104,8 +106,10 @@ class PriceAPI { "&order=market_cap_desc&per_page=50&page=1&sparkline=false"); final coinGeckoResponse = await client.get( - uri, + url: uri, headers: {'Content-Type': 'application/json'}, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); final coinGeckoData = jsonDecode(coinGeckoResponse.body) as List; @@ -136,6 +140,8 @@ class PriceAPI { static Future?> availableBaseCurrencies() async { final externalCalls = Prefs.instance.externalCalls; + HTTP client = HTTP(); + if ((!Logger.isTestEnv && !externalCalls) || !(await Prefs.instance.isExternalCallsSet())) { Logging.instance.log("User does not want to use external calls", @@ -146,9 +152,11 @@ class PriceAPI { "https://api.coingecko.com/api/v3/simple/supported_vs_currencies"; try { final uri = Uri.parse(uriString); - final response = await Client().get( - uri, + final response = await client.get( + url: uri, headers: {'Content-Type': 'application/json'}, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); final json = jsonDecode(response.body) as List; @@ -186,8 +194,10 @@ class PriceAPI { "=$contractAddressesString&include_24hr_change=true"); final coinGeckoResponse = await client.get( - uri, + url: uri, headers: {'Content-Type': 'application/json'}, + proxyInfo: + Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null, ); final coinGeckoData = jsonDecode(coinGeckoResponse.body) as Map; diff --git a/lib/utilities/test_epic_box_connection.dart b/lib/utilities/test_epic_box_connection.dart index c65f85ac7..87e3948e8 100644 --- a/lib/utilities/test_epic_box_connection.dart +++ b/lib/utilities/test_epic_box_connection.dart @@ -10,22 +10,30 @@ import 'dart:convert'; -import 'package:http/http.dart' as http; +import 'package:stackwallet/networking/http.dart'; import 'package:stackwallet/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart'; +import 'package:stackwallet/services/tor_service.dart'; import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/prefs.dart'; Future _testEpicBoxNodeConnection(Uri uri) async { + HTTP client = HTTP(); try { - final client = http.Client(); - final response = await client.get( - uri, - headers: {'Content-Type': 'application/json'}, - ).timeout(const Duration(milliseconds: 2000), - onTimeout: () async => http.Response('Error', 408)); + // final client = http.Client(); + final response = await client + .get( + url: uri, + headers: {'Content-Type': 'application/json'}, + proxyInfo: Prefs.instance.useTor + ? TorService.sharedInstance.proxyInfo + : null, + ) + .timeout(const Duration(milliseconds: 2000), + onTimeout: () async => Response(utf8.encode('Error'), 408)); final json = jsonDecode(response.body); - if (response.statusCode == 200 && json["node_version"] != null) { + if (response.code == 200 && json["node_version"] != null) { return true; } else { return false;