From 31516964c86e51f2840864314a2a9255680ea376 Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Wed, 10 Apr 2024 13:51:48 -0300 Subject: [PATCH 1/2] Do not use tor proxy when localhost, fix proxy not being used inside ImageDownloader.kt --- .../amethyst/service/HttpClientManager.kt | 20 +++++++++++++++---- .../service/Nip11RelayInfoRetriever.kt | 3 ++- .../amethyst/service/relays/Relay.kt | 7 ++++++- .../amethyst/ui/actions/ImageDownloader.kt | 5 +++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClientManager.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClientManager.kt index ea504dae2..1b93c1478 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClientManager.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/HttpClientManager.kt @@ -39,6 +39,7 @@ object HttpClientManager { var proxyChangeListeners = ArrayList<() -> Unit>() private var defaultTimeout = DEFAULT_TIMEOUT_ON_WIFI private var defaultHttpClient: OkHttpClient? = null + private var defaultHttpClientWithoutProxy: OkHttpClient? = null // fires off every time value of the property changes private var internalProxy: Proxy? by @@ -58,6 +59,10 @@ object HttpClientManager { } } + fun getDefaultProxy(): Proxy? { + return this.internalProxy + } + fun setDefaultTimeout(timeout: Duration) { Log.d("HttpClient", "Changing timeout to: $timeout") if (this.defaultTimeout.seconds != timeout.seconds) { @@ -98,11 +103,18 @@ object HttpClientManager { } } - fun getHttpClient(): OkHttpClient { - if (this.defaultHttpClient == null) { - this.defaultHttpClient = buildHttpClient(internalProxy, defaultTimeout) + fun getHttpClient(useProxy: Boolean = true): OkHttpClient { + return if (useProxy) { + if (this.defaultHttpClient == null) { + this.defaultHttpClient = buildHttpClient(internalProxy, defaultTimeout) + } + defaultHttpClient!! + } else { + if (this.defaultHttpClientWithoutProxy == null) { + this.defaultHttpClientWithoutProxy = buildHttpClient(null, defaultTimeout) + } + defaultHttpClientWithoutProxy!! } - return defaultHttpClient!! } fun initProxy( diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/Nip11RelayInfoRetriever.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/Nip11RelayInfoRetriever.kt index fc5c5a1ff..ac4480c9b 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/Nip11RelayInfoRetriever.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/Nip11RelayInfoRetriever.kt @@ -121,8 +121,9 @@ class Nip11Retriever { try { val request: Request = Request.Builder().header("Accept", "application/nostr+json").url(url).build() + val isLocalHost = dirtyUrl.startsWith("ws://127.0.0.1") || dirtyUrl.startsWith("ws://localhost") - HttpClientManager.getHttpClient() + HttpClientManager.getHttpClient(useProxy = !isLocalHost) .newCall(request) .enqueue( object : Callback { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt index 002016ec4..a5f7b7bc5 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/relays/Relay.kt @@ -63,7 +63,12 @@ class Relay( const val RECONNECTING_IN_SECONDS = 60 * 3 } - private val httpClient = HttpClientManager.getHttpClient() + private val httpClient = + if (url.startsWith("ws://127.0.0.1") || url.startsWith("ws://localhost")) { + HttpClientManager.getHttpClient(false) + } else { + HttpClientManager.getHttpClient() + } private var listeners = setOf() private var socket: WebSocket? = null diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt index 6fa169566..5b04d65eb 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt @@ -20,6 +20,7 @@ */ package com.vitorpamplona.amethyst.ui.actions +import com.vitorpamplona.amethyst.service.HttpClientManager import kotlinx.coroutines.CancellationException import kotlinx.coroutines.delay import java.net.HttpURLConnection @@ -36,7 +37,7 @@ class ImageDownloader { try { HttpURLConnection.setFollowRedirects(true) var url = URL(imageUrl) - var huc = url.openConnection() as HttpURLConnection + var huc = url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection huc.instanceFollowRedirects = true var responseCode = huc.responseCode @@ -45,7 +46,7 @@ class ImageDownloader { // open the new connnection again url = URL(newUrl) - huc = url.openConnection() as HttpURLConnection + huc = url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection responseCode = huc.responseCode } From c4250ccd352d0f059b70e879fb125803142abd2c Mon Sep 17 00:00:00 2001 From: greenart7c3 Date: Wed, 10 Apr 2024 14:11:11 -0300 Subject: [PATCH 2/2] fix error when not using proxy --- .../amethyst/ui/actions/ImageDownloader.kt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt index 5b04d65eb..e61c88ccd 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/ImageDownloader.kt @@ -37,7 +37,12 @@ class ImageDownloader { try { HttpURLConnection.setFollowRedirects(true) var url = URL(imageUrl) - var huc = url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection + var huc = + if (HttpClientManager.getDefaultProxy() != null) { + url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection + } else { + url.openConnection() as HttpURLConnection + } huc.instanceFollowRedirects = true var responseCode = huc.responseCode @@ -46,7 +51,12 @@ class ImageDownloader { // open the new connnection again url = URL(newUrl) - huc = url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection + huc = + if (HttpClientManager.getDefaultProxy() != null) { + url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection + } else { + url.openConnection() as HttpURLConnection + } responseCode = huc.responseCode }