mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-10 04:44:43 +00:00
handle all fallback cases
This commit is contained in:
parent
12b79e126a
commit
26dbea91d0
2 changed files with 32 additions and 11 deletions
|
@ -7,6 +7,7 @@ import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:http/http.dart';
|
||||||
|
|
||||||
const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
|
const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
|
||||||
const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
|
const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
|
||||||
|
@ -34,22 +35,38 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
||||||
HttpClient client = await ProxyWrapper.instance.getProxyInstance(
|
HttpClient client = await ProxyWrapper.instance.getProxyInstance(
|
||||||
portOverride: mainThreadProxyPort,
|
portOverride: mainThreadProxyPort,
|
||||||
);
|
);
|
||||||
late HttpClientResponse response;
|
|
||||||
late String responseBody;
|
|
||||||
|
|
||||||
|
late HttpClientResponse httpResponse;
|
||||||
|
late String responseBody;
|
||||||
|
late int statusCode;
|
||||||
|
|
||||||
|
// we might have tor enabled (no way of knowing), so we try to use it first
|
||||||
try {
|
try {
|
||||||
final request = await client.getUrl(onionUri);
|
try {
|
||||||
response = await request.close();
|
final request = await client.getUrl(onionUri);
|
||||||
responseBody = await utf8.decodeStream(response);
|
httpResponse = await request.close();
|
||||||
} catch (e) {
|
responseBody = await utf8.decodeStream(httpResponse);
|
||||||
if (!torOnly) {
|
} catch (e) {
|
||||||
final request = await client.getUrl(clearnetUri);
|
// if the onion url fails, and not set to tor only, try the clearnet url, (still using tor!):
|
||||||
response = await request.close();
|
if (!torOnly) {
|
||||||
responseBody = await utf8.decodeStream(response);
|
final request = await client.getUrl(clearnetUri);
|
||||||
|
httpResponse = await request.close();
|
||||||
|
responseBody = await utf8.decodeStream(httpResponse);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
statusCode = httpResponse.statusCode;
|
||||||
|
} catch (e) {
|
||||||
|
// connections all failed / tor is not enabled, so we use the clearnet url directly as normal:
|
||||||
|
if (torOnly) {
|
||||||
|
// we failed to connect through tor
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
final response = await get(clearnetUri);
|
||||||
|
responseBody = response.body;
|
||||||
|
statusCode = response.statusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +90,7 @@ Future<double> _fetchPriceAsync(
|
||||||
'crypto': crypto.toString(),
|
'crypto': crypto.toString(),
|
||||||
'apiMode': apiMode.toString(),
|
'apiMode': apiMode.toString(),
|
||||||
'port': ProxyWrapper.port,
|
'port': ProxyWrapper.port,
|
||||||
|
'torEnabled': ProxyWrapper.enabled,
|
||||||
});
|
});
|
||||||
|
|
||||||
class FiatConversionService {
|
class FiatConversionService {
|
||||||
|
|
|
@ -19,6 +19,9 @@ class ProxyWrapper {
|
||||||
|
|
||||||
static int get port => Tor.instance.port;
|
static int get port => Tor.instance.port;
|
||||||
|
|
||||||
|
static bool get enabled => Tor.instance.enabled;
|
||||||
|
|
||||||
|
|
||||||
// Method to get or create the Tor instance
|
// Method to get or create the Tor instance
|
||||||
Future<HttpClient> getProxyInstance({int? portOverride}) async {
|
Future<HttpClient> getProxyInstance({int? portOverride}) async {
|
||||||
if (!started) {
|
if (!started) {
|
||||||
|
|
Loading…
Reference in a new issue