add a checkbox to use tor in the login screen, set the proxy in the account model construction
This commit is contained in:
@@ -17,6 +17,8 @@ import nostr.postr.Persona
|
|||||||
import nostr.postr.toHex
|
import nostr.postr.toHex
|
||||||
import nostr.postr.toNpub
|
import nostr.postr.toNpub
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.Proxy
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
// Release mode (!BuildConfig.DEBUG) always uses encrypted preferences
|
// Release mode (!BuildConfig.DEBUG) always uses encrypted preferences
|
||||||
@@ -196,7 +198,8 @@ object LocalPreferences {
|
|||||||
putString(PrefKeys.LATEST_CONTACT_LIST, Event.gson.toJson(account.backupContactList))
|
putString(PrefKeys.LATEST_CONTACT_LIST, Event.gson.toJson(account.backupContactList))
|
||||||
putBoolean(PrefKeys.HIDE_DELETE_REQUEST_DIALOG, account.hideDeleteRequestDialog)
|
putBoolean(PrefKeys.HIDE_DELETE_REQUEST_DIALOG, account.hideDeleteRequestDialog)
|
||||||
putBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, account.hideBlockAlertDialog)
|
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()
|
}.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,6 +256,7 @@ object LocalPreferences {
|
|||||||
val hideDeleteRequestDialog = getBoolean(PrefKeys.HIDE_DELETE_REQUEST_DIALOG, false)
|
val hideDeleteRequestDialog = getBoolean(PrefKeys.HIDE_DELETE_REQUEST_DIALOG, false)
|
||||||
val hideBlockAlertDialog = getBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, false)
|
val hideBlockAlertDialog = getBoolean(PrefKeys.HIDE_BLOCK_ALERT_DIALOG, false)
|
||||||
val useProxy = getBoolean(PrefKeys.USE_PROXY, 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(
|
val a = Account(
|
||||||
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()),
|
Persona(privKey = privKey?.toByteArray(), pubKey = pubKey.toByteArray()),
|
||||||
@@ -267,7 +271,7 @@ object LocalPreferences {
|
|||||||
hideDeleteRequestDialog,
|
hideDeleteRequestDialog,
|
||||||
hideBlockAlertDialog,
|
hideBlockAlertDialog,
|
||||||
latestContactList,
|
latestContactList,
|
||||||
useProxy
|
proxy
|
||||||
)
|
)
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import nostr.postr.Persona
|
import nostr.postr.Persona
|
||||||
|
import java.net.Proxy
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
val DefaultChannels = setOf(
|
val DefaultChannels = setOf(
|
||||||
@@ -46,7 +47,7 @@ class Account(
|
|||||||
var hideDeleteRequestDialog: Boolean = false,
|
var hideDeleteRequestDialog: Boolean = false,
|
||||||
var hideBlockAlertDialog: Boolean = false,
|
var hideBlockAlertDialog: Boolean = false,
|
||||||
var backupContactList: ContactListEvent? = null,
|
var backupContactList: ContactListEvent? = null,
|
||||||
var useProxy: Boolean = false
|
var proxy: Proxy?
|
||||||
) {
|
) {
|
||||||
var transientHiddenUsers: Set<String> = setOf()
|
var transientHiddenUsers: Set<String> = setOf()
|
||||||
|
|
||||||
@@ -693,7 +694,7 @@ class Account(
|
|||||||
fun activeRelays(): Array<Relay>? {
|
fun activeRelays(): Array<Relay>? {
|
||||||
var usersRelayList = userProfile().latestContactList?.relays()?.map {
|
var usersRelayList = userProfile().latestContactList?.relays()?.map {
|
||||||
val localFeedTypes = localRelays.firstOrNull() { localRelay -> localRelay.url == it.key }?.feedTypes ?: FeedType.values().toSet()
|
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
|
} ?: return null
|
||||||
|
|
||||||
// Ugly, but forces nostr.band as the only search-supporting relay today.
|
// Ugly, but forces nostr.band as the only search-supporting relay today.
|
||||||
@@ -704,7 +705,7 @@ class Account(
|
|||||||
Constants.forcedRelayForSearch.read,
|
Constants.forcedRelayForSearch.read,
|
||||||
Constants.forcedRelayForSearch.write,
|
Constants.forcedRelayForSearch.write,
|
||||||
Constants.forcedRelayForSearch.feedTypes,
|
Constants.forcedRelayForSearch.feedTypes,
|
||||||
useProxy
|
proxy
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -713,7 +714,7 @@ class Account(
|
|||||||
|
|
||||||
fun convertLocalRelays(): Array<Relay> {
|
fun convertLocalRelays(): Array<Relay> {
|
||||||
return localRelays.map {
|
return localRelays.map {
|
||||||
Relay(it.url, it.read, it.write, it.feedTypes, useProxy)
|
Relay(it.url, it.read, it.write, it.feedTypes, proxy)
|
||||||
}.toTypedArray()
|
}.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ object Client : RelayPool.Listener {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/** temporary connection */
|
/** 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.send(signedEvent)
|
||||||
it.disconnect()
|
it.disconnect()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ object Constants {
|
|||||||
|
|
||||||
fun convertDefaultRelays(): Array<Relay> {
|
fun convertDefaultRelays(): Array<Relay> {
|
||||||
return defaultRelays.map {
|
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()
|
}.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import okhttp3.Request
|
|||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
import okhttp3.WebSocket
|
import okhttp3.WebSocket
|
||||||
import okhttp3.WebSocketListener
|
import okhttp3.WebSocketListener
|
||||||
import java.net.InetSocketAddress
|
|
||||||
import java.net.Proxy
|
import java.net.Proxy
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
@@ -23,10 +22,8 @@ class Relay(
|
|||||||
var read: Boolean = true,
|
var read: Boolean = true,
|
||||||
var write: Boolean = true,
|
var write: Boolean = true,
|
||||||
var activeTypes: Set<FeedType> = FeedType.values().toSet(),
|
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()
|
private val httpClient = OkHttpClient.Builder()
|
||||||
.proxy(proxy)
|
.proxy(proxy)
|
||||||
.followRedirects(true)
|
.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.AccountBackupDialog
|
||||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.Proxy
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterialApi::class)
|
@OptIn(ExperimentalMaterialApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
@@ -233,7 +235,7 @@ fun ListContent(
|
|||||||
) {
|
) {
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
var backupDialogOpen by remember { mutableStateOf(false) }
|
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()) {
|
Column(modifier = modifier.fillMaxHeight()) {
|
||||||
if (accountUser != null) {
|
if (accountUser != null) {
|
||||||
@@ -279,7 +281,7 @@ fun ListContent(
|
|||||||
onClick = {
|
onClick = {
|
||||||
checked = !checked
|
checked = !checked
|
||||||
println("changed tor to $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.pause()
|
||||||
ServiceManager.start()
|
ServiceManager.start()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ import kotlinx.coroutines.flow.update
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import nostr.postr.Persona
|
import nostr.postr.Persona
|
||||||
import nostr.postr.bechToBytes
|
import nostr.postr.bechToBytes
|
||||||
|
import java.net.InetSocketAddress
|
||||||
|
import java.net.Proxy
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
class AccountStateViewModel() : ViewModel() {
|
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 pattern = Pattern.compile(".+@.+\\.[a-z]+")
|
||||||
val parsed = Nip19.uriToRoute(key)
|
val parsed = Nip19.uriToRoute(key)
|
||||||
val pubKeyParsed = parsed?.hex?.toByteArray()
|
val pubKeyParsed = parsed?.hex?.toByteArray()
|
||||||
|
var proxy = if (useProxy) Proxy(Proxy.Type.SOCKS, InetSocketAddress("127.0.0.1", 9050)) else null
|
||||||
|
|
||||||
val account =
|
val account =
|
||||||
if (key.startsWith("nsec")) {
|
if (key.startsWith("nsec")) {
|
||||||
Account(Persona(privKey = key.bechToBytes()))
|
Account(Persona(privKey = key.bechToBytes()), proxy = proxy)
|
||||||
} else if (pubKeyParsed != null) {
|
} else if (pubKeyParsed != null) {
|
||||||
Account(Persona(pubKey = pubKeyParsed))
|
Account(Persona(pubKey = pubKeyParsed), proxy = proxy)
|
||||||
} else if (pattern.matcher(key).matches()) {
|
} else if (pattern.matcher(key).matches()) {
|
||||||
// Evaluate NIP-5
|
// Evaluate NIP-5
|
||||||
Account(Persona())
|
Account(Persona(), proxy = proxy)
|
||||||
} else {
|
} else {
|
||||||
Account(Persona(Hex.decode(key)))
|
Account(Persona(Hex.decode(key)), proxy = proxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalPreferences.updatePrefsForLogin(account)
|
LocalPreferences.updatePrefsForLogin(account)
|
||||||
@@ -66,8 +69,9 @@ class AccountStateViewModel() : ViewModel() {
|
|||||||
tryLoginExistingAccount()
|
tryLoginExistingAccount()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun newKey() {
|
fun newKey(useProxy: Boolean) {
|
||||||
val account = Account(Persona())
|
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
|
// saves to local preferences
|
||||||
LocalPreferences.updatePrefsForLogin(account)
|
LocalPreferences.updatePrefsForLogin(account)
|
||||||
startUI(account)
|
startUI(account)
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ fun LoginPage(
|
|||||||
var dialogOpen by remember {
|
var dialogOpen by remember {
|
||||||
mutableStateOf(false)
|
mutableStateOf(false)
|
||||||
}
|
}
|
||||||
|
val useProxy = remember { mutableStateOf(false) }
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -158,7 +159,7 @@ fun LoginPage(
|
|||||||
keyboardActions = KeyboardActions(
|
keyboardActions = KeyboardActions(
|
||||||
onGo = {
|
onGo = {
|
||||||
try {
|
try {
|
||||||
accountViewModel.startUI(key.value.text)
|
accountViewModel.startUI(key.value.text, useProxy.value)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
errorMessage = context.getString(R.string.invalid_key)
|
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))
|
Spacer(modifier = Modifier.height(20.dp))
|
||||||
|
|
||||||
Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.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()) {
|
if (acceptedTerms.value && key.value.text.isNotBlank()) {
|
||||||
try {
|
try {
|
||||||
accountViewModel.startUI(key.value.text)
|
accountViewModel.startUI(key.value.text, useProxy.value)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
errorMessage = context.getString(R.string.invalid_key)
|
errorMessage = context.getString(R.string.invalid_key)
|
||||||
}
|
}
|
||||||
@@ -265,7 +275,7 @@ fun LoginPage(
|
|||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
onClick = {
|
onClick = {
|
||||||
if (acceptedTerms.value) {
|
if (acceptedTerms.value) {
|
||||||
accountViewModel.newKey()
|
accountViewModel.newKey(useProxy.value)
|
||||||
} else {
|
} else {
|
||||||
termsAcceptanceIsRequired =
|
termsAcceptanceIsRequired =
|
||||||
context.getString(R.string.acceptance_of_terms_is_required)
|
context.getString(R.string.acceptance_of_terms_is_required)
|
||||||
|
|||||||
Reference in New Issue
Block a user