From 4494af8bc06e049d88fb97028ae73202b358a865 Mon Sep 17 00:00:00 2001 From: woodser Date: Sat, 22 Jun 2024 09:01:52 -0400 Subject: [PATCH] disable tor for private ip addresses to fix #1026 --- .../haveno/core/api/XmrConnectionService.java | 22 +++++++-------- .../java/haveno/core/app/WalletAppSetup.java | 6 ++--- .../haveno/core/offer/OfferBookService.java | 2 +- .../haveno/core/offer/OpenOfferManager.java | 2 +- .../java/haveno/core/trade/HavenoUtils.java | 27 ++++++++++++++++--- .../core/xmr/wallet/XmrWalletService.java | 2 +- 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/haveno/core/api/XmrConnectionService.java b/core/src/main/java/haveno/core/api/XmrConnectionService.java index a837f79f20..40a33f7699 100644 --- a/core/src/main/java/haveno/core/api/XmrConnectionService.java +++ b/core/src/main/java/haveno/core/api/XmrConnectionService.java @@ -253,12 +253,12 @@ public final class XmrConnectionService { connectionList.setAutoSwitch(autoSwitch); } - public boolean isConnectionLocal() { - return isConnectionLocal(getConnection()); + public boolean isConnectionLocalHost() { + return isConnectionLocalHost(getConnection()); } - public boolean isConnectionTor() { - return useTorProxy(getConnection()); + public boolean isProxyApplied() { + return isProxyApplied(getConnection()); } public long getRefreshPeriodMs() { @@ -328,26 +328,26 @@ public final class XmrConnectionService { downloadListener.doneDownload(); } - private boolean isConnectionLocal(MoneroRpcConnection connection) { + private boolean isConnectionLocalHost(MoneroRpcConnection connection) { return connection != null && HavenoUtils.isLocalHost(connection.getUri()); } private long getDefaultRefreshPeriodMs() { MoneroRpcConnection connection = getConnection(); if (connection == null) return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; - if (isConnectionLocal(connection)) { + if (isConnectionLocalHost(connection)) { if (lastInfo != null && (lastInfo.isBusySyncing() || (lastInfo.getHeightWithoutBootstrap() != null && lastInfo.getHeightWithoutBootstrap() > 0 && lastInfo.getHeightWithoutBootstrap() < lastInfo.getHeight()))) return REFRESH_PERIOD_HTTP_MS; // refresh slower if syncing or bootstrapped else return XmrLocalNode.REFRESH_PERIOD_LOCAL_MS; // TODO: announce faster refresh after done syncing - } else if (useTorProxy(connection)) { + } else if (isProxyApplied(connection)) { return REFRESH_PERIOD_ONION_MS; } else { return REFRESH_PERIOD_HTTP_MS; } } - private boolean useTorProxy(MoneroRpcConnection connection) { + private boolean isProxyApplied(MoneroRpcConnection connection) { if (connection == null) return false; - return connection.isOnion() || (preferences.getUseTorForXmr().isUseTorForXmr() && !HavenoUtils.isLocalHost(connection.getUri())); + return connection.isOnion() || (preferences.getUseTorForXmr().isUseTorForXmr() && !HavenoUtils.isPrivateIp(connection.getUri())); } private void initialize() { @@ -475,7 +475,7 @@ public final class XmrConnectionService { // set connection proxies log.info("TOR proxy URI: " + getProxyUri()); for (MoneroRpcConnection connection : connectionManager.getConnections()) { - if (useTorProxy(connection)) connection.setProxyUri(getProxyUri()); + if (isProxyApplied(connection)) connection.setProxyUri(getProxyUri()); } // restore auto switch @@ -495,7 +495,7 @@ public final class XmrConnectionService { // set connection from startup argument if given connectionManager.setAutoSwitch(false); MoneroRpcConnection connection = new MoneroRpcConnection(config.xmrNode, config.xmrNodeUsername, config.xmrNodePassword).setPriority(1); - if (useTorProxy(connection)) connection.setProxyUri(getProxyUri()); + if (isProxyApplied(connection)) connection.setProxyUri(getProxyUri()); connectionManager.setConnection(connection); // start local node if applicable diff --git a/core/src/main/java/haveno/core/app/WalletAppSetup.java b/core/src/main/java/haveno/core/app/WalletAppSetup.java index a55e35d84d..7a1458ab4a 100644 --- a/core/src/main/java/haveno/core/app/WalletAppSetup.java +++ b/core/src/main/java/haveno/core/app/WalletAppSetup.java @@ -268,9 +268,9 @@ public class WalletAppSetup { private String getXmrDaemonNetworkAsString() { String postFix; - if (xmrConnectionService.isConnectionLocal()) + if (xmrConnectionService.isConnectionLocalHost()) postFix = " " + Res.get("mainView.footer.localhostMoneroNode"); - else if (xmrConnectionService.isConnectionTor()) + else if (xmrConnectionService.isProxyApplied()) postFix = " " + Res.get("mainView.footer.usingTor"); else postFix = ""; @@ -279,7 +279,7 @@ public class WalletAppSetup { private String getXmrWalletNetworkAsString() { String postFix; - if (xmrConnectionService.isConnectionLocal()) + if (xmrConnectionService.isConnectionLocalHost()) postFix = " " + Res.get("mainView.footer.localhostMoneroNode"); else if (xmrWalletService.isProxyApplied()) postFix = " " + Res.get("mainView.footer.usingTor"); diff --git a/core/src/main/java/haveno/core/offer/OfferBookService.java b/core/src/main/java/haveno/core/offer/OfferBookService.java index 4e7adbb52e..0df2de483f 100644 --- a/core/src/main/java/haveno/core/offer/OfferBookService.java +++ b/core/src/main/java/haveno/core/offer/OfferBookService.java @@ -293,7 +293,7 @@ public class OfferBookService { } private long getKeyImageRefreshPeriodMs() { - return xmrConnectionService.isConnectionLocal() ? KEY_IMAGE_REFRESH_PERIOD_MS_LOCAL : KEY_IMAGE_REFRESH_PERIOD_MS_REMOTE; + return xmrConnectionService.isConnectionLocalHost() ? KEY_IMAGE_REFRESH_PERIOD_MS_LOCAL : KEY_IMAGE_REFRESH_PERIOD_MS_REMOTE; } private void updateAffectedOffers(String keyImage) { diff --git a/core/src/main/java/haveno/core/offer/OpenOfferManager.java b/core/src/main/java/haveno/core/offer/OpenOfferManager.java index f507cd9758..b9670c2d39 100644 --- a/core/src/main/java/haveno/core/offer/OpenOfferManager.java +++ b/core/src/main/java/haveno/core/offer/OpenOfferManager.java @@ -288,7 +288,7 @@ public class OpenOfferManager implements PeerManager.Listener, DecryptedDirectMe } private long getKeyImageRefreshPeriodMs() { - return xmrConnectionService.isConnectionLocal() ? KEY_IMAGE_REFRESH_PERIOD_MS_LOCAL : KEY_IMAGE_REFRESH_PERIOD_MS_REMOTE; + return xmrConnectionService.isConnectionLocalHost() ? KEY_IMAGE_REFRESH_PERIOD_MS_LOCAL : KEY_IMAGE_REFRESH_PERIOD_MS_REMOTE; } public void onAllServicesInitialized() { diff --git a/core/src/main/java/haveno/core/trade/HavenoUtils.java b/core/src/main/java/haveno/core/trade/HavenoUtils.java index 34371c9e06..351d6bffee 100644 --- a/core/src/main/java/haveno/core/trade/HavenoUtils.java +++ b/core/src/main/java/haveno/core/trade/HavenoUtils.java @@ -40,6 +40,7 @@ import haveno.core.xmr.wallet.XmrWalletService; import haveno.network.p2p.NodeAddress; import java.math.BigDecimal; import java.math.BigInteger; +import java.net.InetAddress; import java.net.URI; import java.security.PrivateKey; import java.text.DecimalFormat; @@ -418,12 +419,32 @@ public class HavenoUtils { /** * Check if the given URI is on local host. */ - public static boolean isLocalHost(String uri) { + public static boolean isLocalHost(String uriString) { try { - String host = new URI(uri).getHost(); + String host = new URI(uriString).getHost(); return LOOPBACK_HOST.equals(host) || LOCALHOST.equals(host); } catch (Exception e) { - throw new RuntimeException(e); + return false; + } + } + + /** + * Check if the given URI is local or a private IP address. + */ + public static boolean isPrivateIp(String uriString) { + if (isLocalHost(uriString)) return true; + try { + + // get the host + URI uri = new URI(uriString); + String host = uri.getHost(); + + // check if private IP address + if (host == null) return false; + InetAddress inetAddress = InetAddress.getByName(host); + return inetAddress.isAnyLocalAddress() || inetAddress.isLoopbackAddress() || inetAddress.isSiteLocalAddress(); + } catch (Exception e) { + return false; } } diff --git a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java index 073f06f5f4..d470478167 100644 --- a/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java +++ b/core/src/main/java/haveno/core/xmr/wallet/XmrWalletService.java @@ -322,7 +322,7 @@ public class XmrWalletService { } public boolean isProxyApplied(boolean wasWalletSynced) { - return preferences.isProxyApplied(wasWalletSynced); + return preferences.isProxyApplied(wasWalletSynced) && xmrConnectionService.isProxyApplied(); } public String getWalletPassword() {