add a checkbox to use tor in the login screen, set the proxy in the account model construction

This commit is contained in:
greenart7c3
2023-04-20 15:30:04 -03:00
parent 2ae54b544f
commit cf8747b19f
8 changed files with 44 additions and 24 deletions
@@ -17,6 +17,8 @@ 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
@@ -196,7 +198,8 @@ object LocalPreferences {
putString(PrefKeys.LATEST_CONTACT_LIST, Event.gson.toJson(account.backupContactList))
putBoolean(PrefKeys.HIDE_DELETE_REQUEST_DIALOG, account.hideDeleteRequestDialog)
putBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, account.hideBlockAlertDialog)
putBoolean(PrefKeys.USE_PROXY, account.useProxy)
println(account.proxy != null)
putBoolean(PrefKeys.USE_PROXY, account.proxy != null)
}.apply()
}
@@ -253,6 +256,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 a = Account(
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()),
@@ -267,7 +271,7 @@ object LocalPreferences {
hideDeleteRequestDialog,
hideBlockAlertDialog,
latestContactList,
useProxy
proxy
)
return a
@@ -16,6 +16,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import nostr.postr.Persona
import java.net.Proxy
import java.util.Locale
val DefaultChannels = setOf(
@@ -46,7 +47,7 @@ class Account(
var hideDeleteRequestDialog: Boolean = false,
var hideBlockAlertDialog: Boolean = false,
var backupContactList: ContactListEvent? = null,
var useProxy: Boolean = false
var proxy: Proxy?
) {
var transientHiddenUsers: Set<String> = setOf()
@@ -693,7 +694,7 @@ class Account(
fun activeRelays(): Array<Relay>? {
var usersRelayList = userProfile().latestContactList?.relays()?.map {
val localFeedTypes = localRelays.firstOrNull() { localRelay -> localRelay.url == it.key }?.feedTypes ?: FeedType.values().toSet()
Relay(it.key, it.value.read, it.value.write, localFeedTypes, useProxy)
Relay(it.key, it.value.read, it.value.write, localFeedTypes, proxy)
} ?: return null
// Ugly, but forces nostr.band as the only search-supporting relay today.
@@ -704,7 +705,7 @@ class Account(
Constants.forcedRelayForSearch.read,
Constants.forcedRelayForSearch.write,
Constants.forcedRelayForSearch.feedTypes,
useProxy
proxy
)
}
@@ -713,7 +714,7 @@ class Account(
fun convertLocalRelays(): Array<Relay> {
return localRelays.map {
Relay(it.url, it.read, it.write, it.feedTypes, useProxy)
Relay(it.url, it.read, it.write, it.feedTypes, proxy)
}.toTypedArray()
}
@@ -76,7 +76,8 @@ object Client : RelayPool.Listener {
}
} else {
/** temporary connection */
Relay(relay, false, true, emptySet(), false).requestAndWatch() {
/** TODO: set the proxy for this temporary connection */
Relay(relay, false, true, emptySet(), null).requestAndWatch() {
it.send(signedEvent)
it.disconnect()
}
@@ -10,7 +10,8 @@ object Constants {
fun convertDefaultRelays(): Array<Relay> {
return defaultRelays.map {
Relay(it.url, it.read, it.write, it.feedTypes, false)
/** TODO: set the proxy */
Relay(it.url, it.read, it.write, it.feedTypes, null)
}.toTypedArray()
}
@@ -10,7 +10,6 @@ import okhttp3.Request
import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.Date
@@ -23,10 +22,8 @@ class Relay(
var read: Boolean = true,
var write: Boolean = true,
var activeTypes: Set<FeedType> = FeedType.values().toSet(),
useProxy: Boolean
proxy: Proxy?
) {
private var proxy = if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
private val httpClient = OkHttpClient.Builder()
.proxy(proxy)
.followRedirects(true)
@@ -54,6 +54,8 @@ 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
@@ -233,7 +235,7 @@ fun ListContent(
) {
val coroutineScope = rememberCoroutineScope()
var backupDialogOpen by remember { mutableStateOf(false) }
var checked by remember { mutableStateOf(account.useProxy) }
var checked by remember { mutableStateOf(account.proxy != null) }
Column(modifier = modifier.fillMaxHeight()) {
if (accountUser != null) {
@@ -279,7 +281,7 @@ fun ListContent(
onClick = {
checked = !checked
println("changed tor to $checked")
account.useProxy = checked
account.proxy = if (checked) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
ServiceManager.pause()
ServiceManager.start()
}
@@ -18,6 +18,8 @@ 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() {
@@ -39,21 +41,22 @@ class AccountStateViewModel() : ViewModel() {
}
}
fun startUI(key: String) {
fun startUI(key: String, useProxy: Boolean) {
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 account =
if (key.startsWith("nsec")) {
Account(Persona(privKey = key.bechToBytes()))
Account(Persona(privKey = key.bechToBytes()), proxy = proxy)
} else if (pubKeyParsed != null) {
Account(Persona(pubKey = pubKeyParsed))
Account(Persona(pubKey = pubKeyParsed), proxy = proxy)
} else if (pattern.matcher(key).matches()) {
// Evaluate NIP-5
Account(Persona())
Account(Persona(), proxy = proxy)
} else {
Account(Persona(Hex.decode(key)))
Account(Persona(Hex.decode(key)), proxy = proxy)
}
LocalPreferences.updatePrefsForLogin(account)
@@ -66,8 +69,9 @@ class AccountStateViewModel() : ViewModel() {
tryLoginExistingAccount()
}
fun newKey() {
val account = Account(Persona())
fun newKey(useProxy: Boolean) {
var proxy = if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
val account = Account(Persona(), proxy = proxy)
// saves to local preferences
LocalPreferences.updatePrefsForLogin(account)
startUI(account)
@@ -55,6 +55,7 @@ fun LoginPage(
var dialogOpen by remember {
mutableStateOf(false)
}
val useProxy = remember { mutableStateOf(false) }
Column(
modifier = Modifier
@@ -158,7 +159,7 @@ fun LoginPage(
keyboardActions = KeyboardActions(
onGo = {
try {
accountViewModel.startUI(key.value.text)
accountViewModel.startUI(key.value.text, useProxy.value)
} catch (e: Exception) {
errorMessage = context.getString(R.string.invalid_key)
}
@@ -221,6 +222,15 @@ fun LoginPage(
}
}
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = useProxy.value,
onCheckedChange = { useProxy.value = it }
)
Text("Enable Tor")
}
Spacer(modifier = Modifier.height(20.dp))
Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.dp)) {
@@ -237,7 +247,7 @@ fun LoginPage(
if (acceptedTerms.value && key.value.text.isNotBlank()) {
try {
accountViewModel.startUI(key.value.text)
accountViewModel.startUI(key.value.text, useProxy.value)
} catch (e: Exception) {
errorMessage = context.getString(R.string.invalid_key)
}
@@ -265,7 +275,7 @@ fun LoginPage(
.fillMaxWidth(),
onClick = {
if (acceptedTerms.value) {
accountViewModel.newKey()
accountViewModel.newKey(useProxy.value)
} else {
termsAcceptanceIsRequired =
context.getString(R.string.acceptance_of_terms_is_required)