WIP: tor http connection

This commit is contained in:
ryleedavis 2023-09-11 14:20:40 -06:00
parent 4160196135
commit 5053e7d97a
5 changed files with 65 additions and 34 deletions

View file

@ -15,11 +15,13 @@ import 'package:flutter/material.dart';
import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS; import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS;
import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS; import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS;
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart';
import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS; import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS;
import 'package:package_info_plus/package_info_plus.dart'; 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/themes/stack_colors.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
@ -39,14 +41,17 @@ Future<bool> doesCommitExist(
String commit, String commit,
) async { ) async {
Logging.instance.log("doesCommitExist", level: LogLevel.Info); Logging.instance.log("doesCommitExist", level: LogLevel.Info);
final Client client = Client(); // final Client client = Client();
HTTP client = HTTP();
try { try {
final uri = Uri.parse( final uri = Uri.parse(
"$kGithubAPI$kGithubHead/$organization/$project/commits/$commit"); "$kGithubAPI$kGithubHead/$organization/$project/commits/$commit");
final commitQuery = await client.get( final commitQuery = await client.get(
uri, url: uri,
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
); );
final response = jsonDecode(commitQuery.body.toString()); final response = jsonDecode(commitQuery.body.toString());
@ -76,14 +81,16 @@ Future<bool> isHeadCommit(
String commit, String commit,
) async { ) async {
Logging.instance.log("doesCommitExist", level: LogLevel.Info); Logging.instance.log("doesCommitExist", level: LogLevel.Info);
final Client client = Client(); HTTP client = HTTP();
try { try {
final uri = Uri.parse( final uri = Uri.parse(
"$kGithubAPI$kGithubHead/$organization/$project/commits/$branch"); "$kGithubAPI$kGithubHead/$organization/$project/commits/$branch");
final commitQuery = await client.get( final commitQuery = await client.get(
uri, url: uri,
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
); );
final response = jsonDecode(commitQuery.body.toString()); final response = jsonDecode(commitQuery.body.toString());

View file

@ -11,7 +11,6 @@
import 'dart:convert'; import 'dart:convert';
import 'package:decimal/decimal.dart'; 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/exchange_exception.dart';
import 'package:stackwallet/exceptions/exchange/majestic_bank/mb_exception.dart'; import 'package:stackwallet/exceptions/exchange/majestic_bank/mb_exception.dart';
import 'package:stackwallet/exceptions/exchange/pair_unavailable_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_calculation.dart';
import 'package:stackwallet/models/exchange/majestic_bank/mb_order_status.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/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/exchange/exchange_response.dart';
import 'package:stackwallet/services/tor_service.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
class MajesticBankAPI { class MajesticBankAPI {
static const String scheme = "https"; static const String scheme = "https";
@ -35,22 +37,23 @@ class MajesticBankAPI {
static MajesticBankAPI get instance => _instance; static MajesticBankAPI get instance => _instance;
/// set this to override using standard http client. Useful for testing HTTP client = HTTP();
http.Client? client;
Uri _buildUri({required String endpoint, Map<String, String>? params}) { Uri _buildUri({required String endpoint, Map<String, String>? params}) {
return Uri.https(authority, "/api/$version/$endpoint", params); return Uri.https(authority, "/api/$version/$endpoint", params);
} }
Future<dynamic> _makeGetRequest(Uri uri) async { Future<dynamic> _makeGetRequest(Uri uri) async {
final client = this.client ?? http.Client(); // final client = this.client ?? http.Client();
int code = -1; int code = -1;
try { try {
final response = await client.get( 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); final parsed = jsonDecode(response.body);

View file

@ -12,7 +12,6 @@ import 'dart:convert';
import 'package:decimal/decimal.dart'; import 'package:decimal/decimal.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:stackwallet/exceptions/exchange/exchange_exception.dart'; import 'package:stackwallet/exceptions/exchange/exchange_exception.dart';
import 'package:stackwallet/external_api_keys.dart'; import 'package:stackwallet/external_api_keys.dart';
import 'package:stackwallet/models/exchange/response_objects/fixed_rate_market.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/response_objects/trade.dart';
import 'package:stackwallet/models/exchange/simpleswap/sp_currency.dart'; import 'package:stackwallet/models/exchange/simpleswap/sp_currency.dart';
import 'package:stackwallet/models/isar/exchange_cache/pair.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/exchange_response.dart';
import 'package:stackwallet/services/exchange/simpleswap/simpleswap_exchange.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/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import 'package:uuid/uuid.dart'; import 'package:uuid/uuid.dart';
@ -34,22 +36,22 @@ class SimpleSwapAPI {
static final SimpleSwapAPI _instance = SimpleSwapAPI._(); static final SimpleSwapAPI _instance = SimpleSwapAPI._();
static SimpleSwapAPI get instance => _instance; static SimpleSwapAPI get instance => _instance;
/// set this to override using standard http client. Useful for testing HTTP client = HTTP();
http.Client? client;
Uri _buildUri(String path, Map<String, String>? params) { Uri _buildUri(String path, Map<String, String>? params) {
return Uri.https(authority, path, params); return Uri.https(authority, path, params);
} }
Future<dynamic> _makeGetRequest(Uri uri) async { Future<dynamic> _makeGetRequest(Uri uri) async {
final client = this.client ?? http.Client();
int code = -1; int code = -1;
try { try {
final response = await client.get( 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); final parsed = jsonDecode(response.body);
@ -67,15 +69,16 @@ class SimpleSwapAPI {
Uri uri, Uri uri,
Map<String, dynamic> body, Map<String, dynamic> body,
) async { ) async {
final client = this.client ?? http.Client();
try { try {
final response = await client.post( final response = await client.post(
uri, url: uri,
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
body: jsonEncode(body), body: jsonEncode(body),
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
); );
if (response.statusCode == 200) { if (response.code == 200) {
final parsed = jsonDecode(response.body); final parsed = jsonDecode(response.body);
return parsed; return parsed;
} }

View file

@ -13,8 +13,9 @@ import 'dart:convert';
import 'package:decimal/decimal.dart'; import 'package:decimal/decimal.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:stackwallet/db/hive/db.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/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart'; import 'package:stackwallet/utilities/prefs.dart';
@ -32,7 +33,8 @@ class PriceAPI {
static const Duration refreshIntervalDuration = static const Duration refreshIntervalDuration =
Duration(seconds: refreshInterval); Duration(seconds: refreshInterval);
final Client client; // final Client client;
HTTP client = HTTP();
PriceAPI(this.client); PriceAPI(this.client);
@ -104,8 +106,10 @@ class PriceAPI {
"&order=market_cap_desc&per_page=50&page=1&sparkline=false"); "&order=market_cap_desc&per_page=50&page=1&sparkline=false");
final coinGeckoResponse = await client.get( final coinGeckoResponse = await client.get(
uri, url: uri,
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
); );
final coinGeckoData = jsonDecode(coinGeckoResponse.body) as List<dynamic>; final coinGeckoData = jsonDecode(coinGeckoResponse.body) as List<dynamic>;
@ -136,6 +140,8 @@ class PriceAPI {
static Future<List<String>?> availableBaseCurrencies() async { static Future<List<String>?> availableBaseCurrencies() async {
final externalCalls = Prefs.instance.externalCalls; final externalCalls = Prefs.instance.externalCalls;
HTTP client = HTTP();
if ((!Logger.isTestEnv && !externalCalls) || if ((!Logger.isTestEnv && !externalCalls) ||
!(await Prefs.instance.isExternalCallsSet())) { !(await Prefs.instance.isExternalCallsSet())) {
Logging.instance.log("User does not want to use external calls", 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"; "https://api.coingecko.com/api/v3/simple/supported_vs_currencies";
try { try {
final uri = Uri.parse(uriString); final uri = Uri.parse(uriString);
final response = await Client().get( final response = await client.get(
uri, url: uri,
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
); );
final json = jsonDecode(response.body) as List<dynamic>; final json = jsonDecode(response.body) as List<dynamic>;
@ -186,8 +194,10 @@ class PriceAPI {
"=$contractAddressesString&include_24hr_change=true"); "=$contractAddressesString&include_24hr_change=true");
final coinGeckoResponse = await client.get( final coinGeckoResponse = await client.get(
uri, url: uri,
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
proxyInfo:
Prefs.instance.useTor ? TorService.sharedInstance.proxyInfo : null,
); );
final coinGeckoData = jsonDecode(coinGeckoResponse.body) as Map; final coinGeckoData = jsonDecode(coinGeckoResponse.body) as Map;

View file

@ -10,22 +10,30 @@
import 'dart:convert'; 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/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/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
Future<bool> _testEpicBoxNodeConnection(Uri uri) async { Future<bool> _testEpicBoxNodeConnection(Uri uri) async {
HTTP client = HTTP();
try { try {
final client = http.Client(); // final client = http.Client();
final response = await client.get( final response = await client
uri, .get(
headers: {'Content-Type': 'application/json'}, url: uri,
).timeout(const Duration(milliseconds: 2000), headers: {'Content-Type': 'application/json'},
onTimeout: () async => http.Response('Error', 408)); 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); final json = jsonDecode(response.body);
if (response.statusCode == 200 && json["node_version"] != null) { if (response.code == 200 && json["node_version"] != null) {
return true; return true;
} else { } else {
return false; return false;