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.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");

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.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();

View file

@ -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);
}

View file

@ -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()) {