Do not use tor proxy when localhost, fix proxy not being used inside ImageDownloader.kt
This commit is contained in:
@@ -39,6 +39,7 @@ object HttpClientManager {
|
|||||||
var proxyChangeListeners = ArrayList<() -> Unit>()
|
var proxyChangeListeners = ArrayList<() -> Unit>()
|
||||||
private var defaultTimeout = DEFAULT_TIMEOUT_ON_WIFI
|
private var defaultTimeout = DEFAULT_TIMEOUT_ON_WIFI
|
||||||
private var defaultHttpClient: OkHttpClient? = null
|
private var defaultHttpClient: OkHttpClient? = null
|
||||||
|
private var defaultHttpClientWithoutProxy: OkHttpClient? = null
|
||||||
|
|
||||||
// fires off every time value of the property changes
|
// fires off every time value of the property changes
|
||||||
private var internalProxy: Proxy? by
|
private var internalProxy: Proxy? by
|
||||||
@@ -58,6 +59,10 @@ object HttpClientManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getDefaultProxy(): Proxy? {
|
||||||
|
return this.internalProxy
|
||||||
|
}
|
||||||
|
|
||||||
fun setDefaultTimeout(timeout: Duration) {
|
fun setDefaultTimeout(timeout: Duration) {
|
||||||
Log.d("HttpClient", "Changing timeout to: $timeout")
|
Log.d("HttpClient", "Changing timeout to: $timeout")
|
||||||
if (this.defaultTimeout.seconds != timeout.seconds) {
|
if (this.defaultTimeout.seconds != timeout.seconds) {
|
||||||
@@ -98,11 +103,18 @@ object HttpClientManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getHttpClient(): OkHttpClient {
|
fun getHttpClient(useProxy: Boolean = true): OkHttpClient {
|
||||||
if (this.defaultHttpClient == null) {
|
return if (useProxy) {
|
||||||
this.defaultHttpClient = buildHttpClient(internalProxy, defaultTimeout)
|
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(
|
fun initProxy(
|
||||||
|
|||||||
@@ -121,8 +121,9 @@ class Nip11Retriever {
|
|||||||
try {
|
try {
|
||||||
val request: Request =
|
val request: Request =
|
||||||
Request.Builder().header("Accept", "application/nostr+json").url(url).build()
|
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)
|
.newCall(request)
|
||||||
.enqueue(
|
.enqueue(
|
||||||
object : Callback {
|
object : Callback {
|
||||||
|
|||||||
@@ -63,7 +63,12 @@ class Relay(
|
|||||||
const val RECONNECTING_IN_SECONDS = 60 * 3
|
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<Listener>()
|
private var listeners = setOf<Listener>()
|
||||||
private var socket: WebSocket? = null
|
private var socket: WebSocket? = null
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.ui.actions
|
package com.vitorpamplona.amethyst.ui.actions
|
||||||
|
|
||||||
|
import com.vitorpamplona.amethyst.service.HttpClientManager
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
@@ -36,7 +37,7 @@ class ImageDownloader {
|
|||||||
try {
|
try {
|
||||||
HttpURLConnection.setFollowRedirects(true)
|
HttpURLConnection.setFollowRedirects(true)
|
||||||
var url = URL(imageUrl)
|
var url = URL(imageUrl)
|
||||||
var huc = url.openConnection() as HttpURLConnection
|
var huc = url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection
|
||||||
huc.instanceFollowRedirects = true
|
huc.instanceFollowRedirects = true
|
||||||
var responseCode = huc.responseCode
|
var responseCode = huc.responseCode
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ class ImageDownloader {
|
|||||||
|
|
||||||
// open the new connnection again
|
// open the new connnection again
|
||||||
url = URL(newUrl)
|
url = URL(newUrl)
|
||||||
huc = url.openConnection() as HttpURLConnection
|
huc = url.openConnection(HttpClientManager.getDefaultProxy()) as HttpURLConnection
|
||||||
responseCode = huc.responseCode
|
responseCode = huc.responseCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user