From ecdd6d12d3b274e03873cc5b3a06193c2238c04c Mon Sep 17 00:00:00 2001 From: pokkst Date: Thu, 8 Dec 2022 10:59:53 -0600 Subject: [PATCH] Cleanup proxy code --- .../onboarding/OnboardingFragment.java | 10 ++--- .../fragment/settings/SettingsFragment.java | 19 +++++----- .../wallet/service/MoneroHandlerThread.java | 7 ++-- .../mynero/wallet/service/PrefService.java | 38 ++++++++++++++++++- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java index e5119bd..c13a2e5 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/onboarding/OnboardingFragment.java @@ -9,7 +9,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; -import android.widget.CompoundButton; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; @@ -31,8 +30,6 @@ import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.Constants; import net.mynero.wallet.util.RestoreHeight; -import org.json.JSONObject; - import java.io.File; import java.util.Calendar; @@ -186,12 +183,11 @@ 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, ""); removeProxyTextListeners(); - if (proxyString.contains(":")) { - String proxyAddress = proxyString.split(":")[0]; - String proxyPort = proxyString.split(":")[1]; + if (PrefService.getInstance().hasProxySet()) { + String proxyAddress = PrefService.getInstance().getProxyAddress(); + String proxyPort = PrefService.getInstance().getProxyPort(); initProxyStuff(proxyAddress, proxyPort); } else { initProxyStuff("127.0.0.1", "9050"); 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 c78faee..d93fba0 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 @@ -131,11 +131,11 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia donationSwitch.setChecked(PrefService.getInstance().getBoolean(Constants.PREF_DONATE_PER_TX, false)); donationSwitch.setOnCheckedChangeListener((compoundButton, b) -> PrefService.getInstance().edit().putBoolean(Constants.PREF_DONATE_PER_TX, b).apply()); - boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); - String proxy = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); - if (proxy.contains(":")) { - String proxyAddress = proxy.split(":")[0]; - String proxyPort = proxy.split(":")[1]; + PrefService prefService = PrefService.getInstance(); + boolean usesProxy = prefService.getBoolean(Constants.PREF_USES_TOR, false); + if (prefService.hasProxySet()) { + String proxyAddress = prefService.getProxyAddress(); + String proxyPort = prefService.getProxyPort(); initProxyStuff(proxyAddress, proxyPort); } torSwitch.setChecked(usesProxy); @@ -148,14 +148,13 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia addProxyTextListeners(); torSwitch.setOnCheckedChangeListener((compoundButton, b) -> { - PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_TOR, b).apply(); + prefService.edit().putBoolean(Constants.PREF_USES_TOR, b).apply(); if (b) { - String proxyString = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); - if (proxyString.contains(":")) { + if (prefService.hasProxySet()) { removeProxyTextListeners(); - String proxyAddress = proxyString.split(":")[0]; - String proxyPort = proxyString.split(":")[1]; + String proxyAddress = prefService.getProxyAddress(); + String proxyPort = prefService.getProxyPort(); initProxyStuff(proxyAddress, proxyPort); addProxyTextListeners(); 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 1b27134..35cd69c 100644 --- a/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java +++ b/app/src/main/java/net/mynero/wallet/service/MoneroHandlerThread.java @@ -60,12 +60,13 @@ public class MoneroHandlerThread extends Thread implements WalletListener { @Override public void run() { - boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); - String currentNodeString = PrefService.getInstance().getNode().toNodeString(); + PrefService prefService = PrefService.getInstance(); + boolean usesTor = prefService.getBoolean(Constants.PREF_USES_TOR, false); + String currentNodeString = prefService.getNode().toNodeString(); 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, ""); + String proxy = prefService.getProxy(); WalletManager.getInstance().setProxy(proxy); wallet.setProxy(proxy); } diff --git a/app/src/main/java/net/mynero/wallet/service/PrefService.java b/app/src/main/java/net/mynero/wallet/service/PrefService.java index e865c50..e866c18 100644 --- a/app/src/main/java/net/mynero/wallet/service/PrefService.java +++ b/app/src/main/java/net/mynero/wallet/service/PrefService.java @@ -27,7 +27,18 @@ public class PrefService extends ServiceBase { public Node getNode() { boolean usesProxy = getBoolean(Constants.PREF_USES_TOR, false); - DefaultNodes defaultNode = usesProxy ? DefaultNodes.SAMOURAI_ONION : DefaultNodes.SAMOURAI; + DefaultNodes defaultNode = DefaultNodes.SAMOURAI; + if(usesProxy) { + String proxyPort = getProxyPort(); + if (!proxyPort.isEmpty()) { + int port = Integer.parseInt(proxyPort); + if(port == 4447) { + defaultNode = DefaultNodes.MYNERO_I2P; + } else { + defaultNode = DefaultNodes.SAMOURAI_ONION; + } + } + } String nodeString = getString(Constants.PREF_NODE_2, defaultNode.getUri()); if(!nodeString.isEmpty()) { return Node.fromString(nodeString); @@ -88,6 +99,31 @@ public class PrefService extends ServiceBase { } } + public String getProxy() { + return PrefService.getInstance().getString(Constants.PREF_PROXY, ""); + } + + public boolean hasProxySet() { + String proxyString = getProxy(); + return proxyString.contains(":"); + } + + public String getProxyAddress() { + if (hasProxySet()) { + String proxyString = getProxy(); + return proxyString.split(":")[0]; + } + return ""; + } + + public String getProxyPort() { + if (hasProxySet()) { + String proxyString = getProxy(); + return proxyString.split(":")[1]; + } + return ""; + } + public String getString(String key, String defaultValue) { String value = preferences.getString(key, ""); if(value.isEmpty() && !defaultValue.isEmpty()) {