From 6be5d2a504edfeead57a345410f2a506582f6312 Mon Sep 17 00:00:00 2001 From: pokkst Date: Fri, 7 Oct 2022 23:13:04 -0500 Subject: [PATCH] Add option to onboarding screen to connect to proxy --- .../java/net/mynero/wallet/MainActivity.java | 1 + .../wallet/adapter/NodeSelectionAdapter.java | 2 +- .../onboarding/OnboardingFragment.java | 91 +++++++++++++++++++ .../onboarding/OnboardingViewModel.java | 38 ++++++++ .../fragment/settings/SettingsFragment.java | 4 +- .../fragment/settings/SettingsViewModel.java | 2 +- .../wallet/service/MoneroHandlerThread.java | 8 +- .../main/res/layout/fragment_onboarding.xml | 65 ++++++++++++- app/src/main/res/values/strings.xml | 2 +- 9 files changed, 202 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/MainActivity.java b/app/src/main/java/net/mynero/wallet/MainActivity.java index 212fcf1..6735568 100644 --- a/app/src/main/java/net/mynero/wallet/MainActivity.java +++ b/app/src/main/java/net/mynero/wallet/MainActivity.java @@ -12,6 +12,7 @@ import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.navigation.fragment.NavHostFragment; +import net.mynero.wallet.data.DefaultNodes; import net.mynero.wallet.fragment.dialog.PasswordBottomSheetDialog; import net.mynero.wallet.fragment.dialog.SendBottomSheetDialog; import net.mynero.wallet.livedata.SingleLiveEvent; diff --git a/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java b/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java index 1dfd9e0..dc0c0a1 100644 --- a/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java +++ b/app/src/main/java/net/mynero/wallet/adapter/NodeSelectionAdapter.java @@ -96,7 +96,7 @@ public class NodeSelectionAdapter extends RecyclerView.Adapter mViewModel.onMoreOptionsClicked()); moreOptionsChevronImageView.setOnClickListener(view12 -> mViewModel.onMoreOptionsClicked()); createWalletButton.setOnClickListener(view1 -> { + prepareDefaultNode(); ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { createOrImportWallet( walletPasswordEditText.getText().toString(), @@ -81,6 +126,26 @@ public class OnboardingFragment extends Fragment { } } }); + torSwitch.setOnCheckedChangeListener((compoundButton, b) -> { + PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_TOR, b).apply(); + if (b) { + String proxyString = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); + if (proxyString.contains(":")) { + removeProxyTextListeners(); + + String proxyAddress = proxyString.split(":")[0]; + String proxyPort = proxyString.split(":")[1]; + initProxyStuff(proxyAddress, proxyPort); + + addProxyTextListeners(); + } + proxySettingsLayout.setVisibility(View.VISIBLE); + } else { + proxySettingsLayout.setVisibility(View.GONE); + } + + mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication())); + }); mViewModel.showMoreOptions.observe(getViewLifecycleOwner(), show -> { if (show) { @@ -95,6 +160,12 @@ public class OnboardingFragment extends Fragment { }); } + private void prepareDefaultNode() { + boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); + DefaultNodes defaultNode = usesTor ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; + PrefService.getInstance().edit().putString(Constants.PREF_NODE, defaultNode.getAddress()).apply(); + } + private void createOrImportWallet(String walletPassword, String walletSeed, String restoreHeightText) { MainActivity mainActivity = (MainActivity) getActivity(); if (mainActivity != null) { @@ -130,6 +201,26 @@ public class OnboardingFragment extends Fragment { } } + private void removeProxyTextListeners() { + walletProxyAddressEditText.removeTextChangedListener(proxyAddressListener); + walletProxyPortEditText.removeTextChangedListener(proxyPortListener); + } + + private void addProxyTextListeners() { + walletProxyAddressEditText.addTextChangedListener(proxyAddressListener); + walletProxyPortEditText.addTextChangedListener(proxyPortListener); + } + + private void initProxyStuff(String proxyAddress, String proxyPort) { + boolean validIpAddress = Patterns.IP_ADDRESS.matcher(proxyAddress).matches(); + if (validIpAddress) { + mViewModel.setProxyAddress(proxyAddress); + mViewModel.setProxyPort(proxyPort); + walletProxyAddressEditText.setText(proxyAddress); + walletProxyPortEditText.setText(proxyPort); + } + } + private boolean checkMnemonic(String seed) { return (seed.split("\\s").length == 25); } diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java index 08f2403..218775d 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingViewModel.java @@ -1,16 +1,54 @@ package net.mynero.wallet.fragment.onboarding; +import android.util.Patterns; + import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import net.mynero.wallet.MoneroApplication; +import net.mynero.wallet.data.DefaultNodes; +import net.mynero.wallet.model.WalletManager; +import net.mynero.wallet.service.PrefService; +import net.mynero.wallet.util.Constants; + public class OnboardingViewModel extends ViewModel { private final MutableLiveData _showMoreOptions = new MutableLiveData<>(false); public LiveData showMoreOptions = _showMoreOptions; + private String proxyAddress = ""; + private String proxyPort = ""; public void onMoreOptionsClicked() { boolean currentValue = showMoreOptions.getValue() != null ? showMoreOptions.getValue() : false; boolean newValue = !currentValue; _showMoreOptions.setValue(newValue); } + + public void updateProxy(MoneroApplication application) { + application.getExecutor().execute(() -> { + boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); + + if (!usesProxy) { + return; + } + + if (proxyAddress.isEmpty()) proxyAddress = "127.0.0.1"; + if (proxyPort.isEmpty()) proxyPort = "9050"; + boolean validIpAddress = Patterns.IP_ADDRESS.matcher(proxyAddress).matches(); + + if (validIpAddress) { + String proxy = proxyAddress + ":" + proxyPort; + PrefService.getInstance().edit().putString(Constants.PREF_PROXY, proxy).apply(); + } + }); + } + + + public void setProxyAddress(String address) { + this.proxyAddress = address; + } + + public void setProxyPort(String port) { + this.proxyPort = port; + } } \ No newline at end of file diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java index b6e3baa..e132754 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java @@ -174,7 +174,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia statusTextView.setText(getResources().getText(R.string.version_mismatch)); } }); - Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress())); + Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, "")); // shouldn't use default value here selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); selectNodeButton.setOnClickListener(view1 -> { NodeSelectionBottomSheetDialog dialog = new NodeSelectionBottomSheetDialog(); @@ -222,7 +222,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia @Override public void onNodeSelected() { - Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress())); + Node node = Node.fromString(PrefService.getInstance().getString(Constants.PREF_NODE, "")); selectNodeButton.setText(getString(R.string.node_button_text, node.getAddress())); mViewModel.updateProxy(((MoneroApplication)getActivity().getApplication())); ((MoneroApplication)getActivity().getApplication()).getExecutor().execute(() -> { diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java index be143a6..e44313d 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsViewModel.java @@ -21,7 +21,7 @@ public class SettingsViewModel extends ViewModel { public void updateProxy(MoneroApplication application) { application.getExecutor().execute(() -> { boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); - String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress()); + String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE, ""); boolean isNodeLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); if (!usesProxy || isNodeLocalIp) { diff --git a/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java b/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java index 49a0967..40c737a 100644 --- a/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java +++ b/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java @@ -56,9 +56,13 @@ public class MoneroHandlerThread extends Thread implements WalletListener { @Override public void run() { - String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE, DefaultNodes.XMRTW.getAddress()); - Node selectedNode = Node.fromString(currentNodeString); boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); + DefaultNodes defaultNode = DefaultNodes.SAMOURAI; + if(usesTor) { + defaultNode = DefaultNodes.SAMOURAI_ONION; + } + String currentNodeString = PrefService.getInstance().getString(Constants.PREF_NODE, defaultNode.getAddress()); + Node selectedNode = Node.fromString(currentNodeString); boolean isLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); if (usesTor && !isLocalIp) { String proxy = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); diff --git a/app/src/main/res/layout/fragment_onboarding.xml b/app/src/main/res/layout/fragment_onboarding.xml index 9bd0276..a4246cf 100644 --- a/app/src/main/res/layout/fragment_onboarding.xml +++ b/app/src/main/res/layout/fragment_onboarding.xml @@ -28,25 +28,82 @@ android:background="@drawable/edittext_bg" android:hint="@string/password_optional" android:inputType="textPassword" - app:layout_constraintBottom_toTopOf="@id/advanced_settings_dropdown_textview" + app:layout_constraintBottom_toTopOf="@id/tor_onboarding_switch" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/create_wallet_textview" tools:visibility="visible" /> + + + + + + + + + + + app:layout_constraintTop_toBottomOf="@id/wallet_proxy_settings_layout" /> Copied to clipboard Night mode Display wallet keys - Enable proxy + Connect to proxy Connection failed 87MRtZPrWUCVUgcFHdsVb5MoZUcLtqfD3FvQVGwftFb8eSdMnE39JhAJcbuSW8X2vRaRsB9RQfuCpFciybJFHaz3QYPhCLw 0.00