From 48a69f1d26d105bb09588b9162662e29f473f183 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 20 Oct 2025 19:39:33 -0400 Subject: [PATCH] Watches the correct OKHttp for relays --- .../com/vitorpamplona/amethyst/AppModules.kt | 26 +++++++------------ .../relayClient/RelayProxyClientConnector.kt | 20 +++++++------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt index 51d780ae9..bbfcdbaf7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt @@ -133,16 +133,6 @@ class AppModules( scope = applicationIOScope, ) - // manages all relay connections - val okHttpClientForRelaysForDms = - DualHttpClientManager( - userAgent = appAgent, - proxyPortProvider = torManager.activePortOrNull, - isMobileDataProvider = connManager.isMobileOrNull, - keyCache = keyCache, - scope = applicationIOScope, - ) - // Offers easy methods to know when connections are happening through Tor or not val roleBasedHttpClientBuilder = RoleBasedHttpClientBuilder(okHttpClients, torPrefs.value) @@ -170,11 +160,7 @@ class AppModules( val websocketBuilder = OkHttpWebSocket.Builder { url -> val useTor = torEvaluatorFlow.flow.value.useTor(url) - if (url in torEvaluatorFlow.flow.value.dmRelayList) { - okHttpClientForRelaysForDms.getHttpClient(useTor) - } else { - okHttpClientForRelays.getHttpClient(useTor) - } + okHttpClientForRelays.getHttpClient(useTor) } // Caches all events in Memory @@ -184,7 +170,15 @@ class AppModules( val client: INostrClient = NostrClient(websocketBuilder, applicationDefaultScope) // Watches for changes on Tor and Relay List Settings - val relayProxyClientConnector = RelayProxyClientConnector(torEvaluatorFlow.flow, okHttpClients, connManager, client, torManager, applicationDefaultScope) + val relayProxyClientConnector = + RelayProxyClientConnector( + torEvaluatorFlow.flow, + okHttpClientForRelays, + connManager, + torManager, + client, + applicationDefaultScope, + ) // Verifies and inserts in the cache from all relays, all subscriptions val cacheClientConnector = CacheClientConnector(client, cache) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayProxyClientConnector.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayProxyClientConnector.kt index 1cb1148e1..107e321c3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayProxyClientConnector.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelayProxyClientConnector.kt @@ -47,8 +47,8 @@ class RelayProxyClientConnector( val torEvaluator: StateFlow, val okHttpClients: DualHttpClientManager, val connManager: ConnectivityManager, - val client: INostrClient, val torManager: TorManager, + val client: INostrClient, val scope: CoroutineScope, ) { data class RelayServiceInfra( @@ -56,6 +56,7 @@ class RelayProxyClientConnector( val torConnection: OkHttpClient, val clearConnection: OkHttpClient, val connectivity: ConnectivityStatus, + val torStatus: TorServiceStatus, ) @OptIn(FlowPreview::class) @@ -65,8 +66,9 @@ class RelayProxyClientConnector( okHttpClients.defaultHttpClient, okHttpClients.defaultHttpClientWithoutProxy, connManager.status, - ) { torSettings, torConnection, clearConnection, connectivity -> - RelayServiceInfra(torSettings, torConnection, clearConnection, connectivity) + torManager.status, + ) { torSettings, torConnection, clearConnection, connectivity, torStatus -> + RelayServiceInfra(torSettings, torConnection, clearConnection, connectivity, torStatus) }.debounce(100) .onEach { if (it.connectivity is ConnectivityStatus.StartingService) { @@ -76,18 +78,16 @@ class RelayProxyClientConnector( if (client.isActive()) { client.disconnect() } - val torStatus = torManager.status.value - if (torStatus is TorServiceStatus.Active) { - torStatus.torControlConnection?.signal(TorControlCommands.SIGNAL_DORMANT) + if (it.torStatus is TorServiceStatus.Active) { + it.torStatus.torControlConnection?.signal(TorControlCommands.SIGNAL_DORMANT) Log.d("ManageRelayServices", "Pausing Tor Activity") } } else if (it.connectivity is ConnectivityStatus.Active && !client.isActive()) { Log.d("ManageRelayServices", "Connectivity On: Resuming Relay Services") - val torStatus = torManager.status.value - if (torStatus is TorServiceStatus.Active) { - torStatus.torControlConnection?.signal(TorControlCommands.SIGNAL_ACTIVE) - torStatus.torControlConnection?.signal(TorControlCommands.SIGNAL_NEWNYM) + if (it.torStatus is TorServiceStatus.Active) { + it.torStatus.torControlConnection?.signal(TorControlCommands.SIGNAL_ACTIVE) + it.torStatus.torControlConnection?.signal(TorControlCommands.SIGNAL_NEWNYM) Log.d("ManageRelayServices", "Resuming Tor Activity with new nym") }