created initProxy

This commit is contained in:
greenart7c3
2023-04-26 11:54:04 -03:00
parent 5d9d7ba16e
commit 3217d5095b
4 changed files with 12 additions and 10 deletions
@@ -8,6 +8,7 @@ import com.google.gson.reflect.TypeToken
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.RelaySetupInfo
import com.vitorpamplona.amethyst.model.toByteArray
import com.vitorpamplona.amethyst.service.HttpClient
import com.vitorpamplona.amethyst.service.model.ContactListEvent
import com.vitorpamplona.amethyst.service.model.Event
import com.vitorpamplona.amethyst.service.model.Event.Companion.getRefinedEvent
@@ -17,8 +18,6 @@ import nostr.postr.Persona
import nostr.postr.toHex
import nostr.postr.toNpub
import java.io.File
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.Locale
// Release mode (!BuildConfig.DEBUG) always uses encrypted preferences
@@ -256,7 +255,7 @@ object LocalPreferences {
val hideDeleteRequestDialog = getBoolean(PrefKeys.HIDE_DELETE_REQUEST_DIALOG, false)
val hideBlockAlertDialog = getBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, false)
val useProxy = getBoolean(PrefKeys.USE_PROXY, false)
var proxy = if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", 9050)
val a = Account(
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()),
@@ -2,6 +2,7 @@ package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.model.Account
import okhttp3.OkHttpClient
import java.net.InetSocketAddress
import java.net.Proxy
object HttpClient {
@@ -18,4 +19,8 @@ object HttpClient {
fun getProxy(): Proxy? {
return proxy
}
fun initProxy(useProxy: Boolean, hostname: String, port: Int): Proxy? {
return if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress(hostname, port)) else null
}
}
@@ -49,13 +49,12 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.HttpClient
import com.vitorpamplona.amethyst.ui.components.ResizeImage
import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountBackupDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import kotlinx.coroutines.launch
import java.net.InetSocketAddress
import java.net.Proxy
@OptIn(ExperimentalMaterialApi::class)
@Composable
@@ -283,7 +282,7 @@ fun ListContent(
onClick = {
checked = !checked
println("changed tor to $checked")
account.proxy = if (checked) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
account.proxy = HttpClient.initProxy(checked, "127.0.0.1", 9050)
ServiceManager.pause()
ServiceManager.start()
}
@@ -5,6 +5,7 @@ import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.ServiceManager
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.toByteArray
import com.vitorpamplona.amethyst.service.HttpClient
import com.vitorpamplona.amethyst.service.nip19.Nip19
import fr.acinq.secp256k1.Hex
import kotlinx.coroutines.CoroutineScope
@@ -18,8 +19,6 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import nostr.postr.Persona
import nostr.postr.bechToBytes
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.regex.Pattern
class AccountStateViewModel() : ViewModel() {
@@ -45,7 +44,7 @@ class AccountStateViewModel() : ViewModel() {
val pattern = Pattern.compile(".+@.+\\.[a-z]+")
val parsed = Nip19.uriToRoute(key)
val pubKeyParsed = parsed?.hex?.toByteArray()
var proxy = if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", 9050)
val account =
if (key.startsWith("nsec")) {
@@ -70,7 +69,7 @@ class AccountStateViewModel() : ViewModel() {
}
fun newKey(useProxy: Boolean) {
var proxy = if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
var proxy = HttpClient.initProxy(useProxy, "127.0.0.1", 9050)
val account = Account(Persona(), proxy = proxy)
// saves to local preferences
LocalPreferences.updatePrefsForLogin(account)