mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-03-12 09:32:33 +00:00
proxy fixes
This commit is contained in:
parent
700dc7ad15
commit
bd3db7faa1
4 changed files with 38 additions and 26 deletions
|
@ -1,7 +1,9 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:cake_wallet/di.dart';
|
||||
import 'package:cake_wallet/store/settings_store.dart';
|
||||
import 'package:cake_wallet/utils/proxy_wrapper.dart';
|
||||
import 'package:cake_wallet/view_model/settings/tor_connection.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cake_wallet/entities/fiat_currency.dart';
|
||||
import 'dart:convert';
|
||||
|
@ -16,6 +18,7 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
|||
final crypto = args['crypto'] as String;
|
||||
final fiat = args['fiat'] as String;
|
||||
final mainThreadProxyPort = args['port'] as int;
|
||||
final torConnectionMode = args['torConnectionMode'] as TorConnectionMode;
|
||||
|
||||
final Map<String, String> queryParams = {
|
||||
'interval_count': '1',
|
||||
|
@ -30,7 +33,7 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
|||
final Uri onionUri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
|
||||
final Uri clearnetUri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
|
||||
|
||||
ProxyWrapper proxy = await getIt.get<ProxyWrapper>();
|
||||
ProxyWrapper proxy = ProxyWrapper();
|
||||
|
||||
late HttpClientResponse httpResponse;
|
||||
late String responseBody;
|
||||
|
@ -43,7 +46,9 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
|||
clearnetUri: clearnetUri,
|
||||
portOverride: mainThreadProxyPort,
|
||||
torOnly: false,
|
||||
torConnectionMode: torConnectionMode,
|
||||
);
|
||||
|
||||
responseBody = await utf8.decodeStream(httpResponse);
|
||||
statusCode = httpResponse.statusCode;
|
||||
} catch (e) {
|
||||
|
@ -68,12 +73,12 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
|||
}
|
||||
}
|
||||
|
||||
Future<double> _fetchPriceAsync(
|
||||
CryptoCurrency crypto, FiatCurrency fiat) async =>
|
||||
Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat) async =>
|
||||
compute(_fetchPrice, {
|
||||
'fiat': fiat.toString(),
|
||||
'crypto': crypto.toString(),
|
||||
'port': ProxyWrapper.port,
|
||||
'torConnectionMode': getIt.get<SettingsStore>().torConnectionMode,
|
||||
});
|
||||
|
||||
class FiatConversionService {
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
||||
import 'package:cake_wallet/view_model/settings/tor_connection.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:cake_wallet/core/fiat_conversion_service.dart';
|
||||
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
|
||||
|
|
|
@ -17,10 +17,10 @@ class NullOverrides extends HttpOverrides {
|
|||
|
||||
class ProxyWrapper {
|
||||
ProxyWrapper({
|
||||
required this.settingsStore,
|
||||
this.settingsStore,
|
||||
});
|
||||
|
||||
late SettingsStore settingsStore;
|
||||
SettingsStore? settingsStore;
|
||||
|
||||
HttpClient? _torClient;
|
||||
|
||||
|
@ -32,6 +32,8 @@ class ProxyWrapper {
|
|||
|
||||
// Method to get or create the Tor proxy instance
|
||||
Future<HttpClient> getProxyHttpClient({int? portOverride}) async {
|
||||
portOverride = (portOverride == -1 || portOverride == null) ? Tor.instance.port : portOverride;
|
||||
|
||||
if (!started) {
|
||||
started = true;
|
||||
_torClient = HttpClient();
|
||||
|
@ -40,7 +42,7 @@ class ProxyWrapper {
|
|||
SocksTCPClient.assignToHttpClient(_torClient!, [
|
||||
ProxySettings(
|
||||
InternetAddress.loopbackIPv4,
|
||||
portOverride ?? Tor.instance.port,
|
||||
portOverride,
|
||||
password: null,
|
||||
),
|
||||
]);
|
||||
|
@ -81,27 +83,30 @@ class ProxyWrapper {
|
|||
Map<String, String>? headers,
|
||||
int? portOverride,
|
||||
bool torOnly = false,
|
||||
TorConnectionMode? torConnectionMode,
|
||||
Uri? clearnetUri,
|
||||
Uri? onionUri,
|
||||
}) async {
|
||||
HttpClient? torClient;
|
||||
late bool torEnabled;
|
||||
if (settingsStore.torConnectionMode == TorConnectionMode.onionOnly ||
|
||||
settingsStore.torConnectionMode == TorConnectionMode.enabled) {
|
||||
torClient = await getProxyHttpClient(portOverride: portOverride);
|
||||
torConnectionMode ??= settingsStore?.torConnectionMode ?? TorConnectionMode.disabled;
|
||||
if (torConnectionMode == TorConnectionMode.onionOnly ||
|
||||
torConnectionMode == TorConnectionMode.enabled) {
|
||||
torEnabled = true;
|
||||
} else {
|
||||
torEnabled = false;
|
||||
}
|
||||
|
||||
if (settingsStore.torConnectionMode == TorConnectionMode.onionOnly) {
|
||||
if (onionUri == null) {
|
||||
throw Exception("Cannot connect to clearnet");
|
||||
}
|
||||
if (torConnectionMode == TorConnectionMode.onionOnly && onionUri == null) {
|
||||
throw Exception("Cannot connect to clearnet");
|
||||
}
|
||||
|
||||
// if tor is enabled, try to connect to the onion url first:
|
||||
if (torEnabled) {
|
||||
try {
|
||||
torClient = await getProxyHttpClient(portOverride: portOverride);
|
||||
} catch (_) {}
|
||||
|
||||
if (onionUri != null) {
|
||||
try {
|
||||
return makeGet(
|
||||
|
@ -112,7 +117,7 @@ class ProxyWrapper {
|
|||
} catch (_) {}
|
||||
}
|
||||
|
||||
if (clearnetUri != null && settingsStore.torConnectionMode != TorConnectionMode.onionOnly) {
|
||||
if (clearnetUri != null && torConnectionMode != TorConnectionMode.onionOnly) {
|
||||
try {
|
||||
return makeGet(
|
||||
client: torClient!,
|
||||
|
@ -123,7 +128,7 @@ class ProxyWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
if (!torOnly && clearnetUri != null) {
|
||||
if (clearnetUri != null && !torOnly && torConnectionMode != TorConnectionMode.onionOnly) {
|
||||
try {
|
||||
return HttpOverrides.runZoned(
|
||||
() {
|
||||
|
@ -148,28 +153,31 @@ class ProxyWrapper {
|
|||
Map<String, String>? headers,
|
||||
int? portOverride,
|
||||
bool torOnly = false,
|
||||
TorConnectionMode? torConnectionMode,
|
||||
Uri? clearnetUri,
|
||||
Uri? onionUri,
|
||||
}) async {
|
||||
HttpClient? torClient;
|
||||
late bool torEnabled;
|
||||
if (settingsStore.torConnectionMode == TorConnectionMode.onionOnly ||
|
||||
settingsStore.torConnectionMode == TorConnectionMode.enabled) {
|
||||
torClient = await getProxyHttpClient(portOverride: portOverride);
|
||||
torConnectionMode ??= settingsStore?.torConnectionMode ?? TorConnectionMode.disabled;
|
||||
if (torConnectionMode == TorConnectionMode.onionOnly ||
|
||||
torConnectionMode == TorConnectionMode.enabled) {
|
||||
torEnabled = true;
|
||||
} else {
|
||||
torEnabled = false;
|
||||
}
|
||||
|
||||
if (settingsStore.torConnectionMode == TorConnectionMode.onionOnly) {
|
||||
if (onionUri == null) {
|
||||
throw Exception("Cannot connect to clearnet");
|
||||
}
|
||||
if (torConnectionMode == TorConnectionMode.onionOnly && onionUri == null) {
|
||||
throw Exception("Cannot connect to clearnet");
|
||||
}
|
||||
|
||||
// if tor is enabled, try to connect to the onion url first:
|
||||
|
||||
if (torEnabled) {
|
||||
try {
|
||||
torClient = await getProxyHttpClient(portOverride: portOverride);
|
||||
} catch (_) {}
|
||||
|
||||
if (onionUri != null) {
|
||||
try {
|
||||
return makePost(
|
||||
|
@ -180,7 +188,7 @@ class ProxyWrapper {
|
|||
} catch (_) {}
|
||||
}
|
||||
|
||||
if (clearnetUri != null && settingsStore.torConnectionMode != TorConnectionMode.onionOnly) {
|
||||
if (clearnetUri != null && torConnectionMode != TorConnectionMode.onionOnly) {
|
||||
try {
|
||||
return makePost(
|
||||
client: torClient!,
|
||||
|
@ -191,7 +199,7 @@ class ProxyWrapper {
|
|||
}
|
||||
}
|
||||
|
||||
if (!torOnly && clearnetUri != null) {
|
||||
if (clearnetUri != null && !torOnly && torConnectionMode != TorConnectionMode.onionOnly) {
|
||||
try {
|
||||
return HttpOverrides.runZoned(
|
||||
() {
|
||||
|
|
|
@ -81,7 +81,7 @@ abstract class TorViewModelBase with Store {
|
|||
|
||||
@action
|
||||
Future<void> stopTor() async {
|
||||
Tor.instance.disable();
|
||||
// Tor.instance.disable();
|
||||
_settingsStore.shouldStartTorOnLaunch = false;
|
||||
torConnectionStatus = TorConnectionStatus.disconnected;
|
||||
SocksTCPClient.removeProxy();
|
||||
|
|
Loading…
Reference in a new issue