Cleanup proxy code

This commit is contained in:
pokkst 2022-12-08 10:59:53 -06:00
parent f622bb053e
commit ecdd6d12d3
No known key found for this signature in database
GPG key ID: 90C2ED85E67A50FF
4 changed files with 53 additions and 21 deletions

View file

@ -9,7 +9,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; 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.Constants;
import net.mynero.wallet.util.RestoreHeight; import net.mynero.wallet.util.RestoreHeight;
import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.util.Calendar; import java.util.Calendar;
@ -186,12 +183,11 @@ public class OnboardingFragment extends Fragment {
torSwitch.setOnCheckedChangeListener((compoundButton, b) -> { torSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_TOR, b).apply(); PrefService.getInstance().edit().putBoolean(Constants.PREF_USES_TOR, b).apply();
if (b) { if (b) {
String proxyString = PrefService.getInstance().getString(Constants.PREF_PROXY, "");
removeProxyTextListeners(); removeProxyTextListeners();
if (proxyString.contains(":")) { if (PrefService.getInstance().hasProxySet()) {
String proxyAddress = proxyString.split(":")[0]; String proxyAddress = PrefService.getInstance().getProxyAddress();
String proxyPort = proxyString.split(":")[1]; String proxyPort = PrefService.getInstance().getProxyPort();
initProxyStuff(proxyAddress, proxyPort); initProxyStuff(proxyAddress, proxyPort);
} else { } else {
initProxyStuff("127.0.0.1", "9050"); initProxyStuff("127.0.0.1", "9050");

View file

@ -131,11 +131,11 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
donationSwitch.setChecked(PrefService.getInstance().getBoolean(Constants.PREF_DONATE_PER_TX, false)); 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()); 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); PrefService prefService = PrefService.getInstance();
String proxy = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); boolean usesProxy = prefService.getBoolean(Constants.PREF_USES_TOR, false);
if (proxy.contains(":")) { if (prefService.hasProxySet()) {
String proxyAddress = proxy.split(":")[0]; String proxyAddress = prefService.getProxyAddress();
String proxyPort = proxy.split(":")[1]; String proxyPort = prefService.getProxyPort();
initProxyStuff(proxyAddress, proxyPort); initProxyStuff(proxyAddress, proxyPort);
} }
torSwitch.setChecked(usesProxy); torSwitch.setChecked(usesProxy);
@ -148,14 +148,13 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia
addProxyTextListeners(); addProxyTextListeners();
torSwitch.setOnCheckedChangeListener((compoundButton, b) -> { 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) { if (b) {
String proxyString = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); if (prefService.hasProxySet()) {
if (proxyString.contains(":")) {
removeProxyTextListeners(); removeProxyTextListeners();
String proxyAddress = proxyString.split(":")[0]; String proxyAddress = prefService.getProxyAddress();
String proxyPort = proxyString.split(":")[1]; String proxyPort = prefService.getProxyPort();
initProxyStuff(proxyAddress, proxyPort); initProxyStuff(proxyAddress, proxyPort);
addProxyTextListeners(); addProxyTextListeners();

View file

@ -60,12 +60,13 @@ public class MoneroHandlerThread extends Thread implements WalletListener {
@Override @Override
public void run() { public void run() {
boolean usesTor = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); PrefService prefService = PrefService.getInstance();
String currentNodeString = PrefService.getInstance().getNode().toNodeString(); boolean usesTor = prefService.getBoolean(Constants.PREF_USES_TOR, false);
String currentNodeString = prefService.getNode().toNodeString();
Node selectedNode = Node.fromString(currentNodeString); Node selectedNode = Node.fromString(currentNodeString);
boolean isLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1"); boolean isLocalIp = currentNodeString.startsWith("10.") || currentNodeString.startsWith("192.168.") || currentNodeString.equals("localhost") || currentNodeString.equals("127.0.0.1");
if (usesTor && !isLocalIp) { if (usesTor && !isLocalIp) {
String proxy = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); String proxy = prefService.getProxy();
WalletManager.getInstance().setProxy(proxy); WalletManager.getInstance().setProxy(proxy);
wallet.setProxy(proxy); wallet.setProxy(proxy);
} }

View file

@ -27,7 +27,18 @@ public class PrefService extends ServiceBase {
public Node getNode() { public Node getNode() {
boolean usesProxy = getBoolean(Constants.PREF_USES_TOR, false); 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()); String nodeString = getString(Constants.PREF_NODE_2, defaultNode.getUri());
if(!nodeString.isEmpty()) { if(!nodeString.isEmpty()) {
return Node.fromString(nodeString); 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) { public String getString(String key, String defaultValue) {
String value = preferences.getString(key, ""); String value = preferences.getString(key, "");
if(value.isEmpty() && !defaultValue.isEmpty()) { if(value.isEmpty() && !defaultValue.isEmpty()) {