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_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<bool> 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<bool> 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());

View file

@ -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<String, String>? params}) {
return Uri.https(authority, "/api/$version/$endpoint", params);
}
Future<dynamic> _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);

View file

@ -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<String, String>? params) {
return Uri.https(authority, path, params);
}
Future<dynamic> _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<String, dynamic> 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;
}

View file

@ -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<dynamic>;
@ -136,6 +140,8 @@ class PriceAPI {
static Future<List<String>?> 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<dynamic>;
@ -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;

View file

@ -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<bool> _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;