Skip to content

Commit

Permalink
disable tor for private ip addresses to fix #1026
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Jun 23, 2024
1 parent 4819e5e commit 4494af8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
22 changes: 11 additions & 11 deletions core/src/main/java/haveno/core/api/XmrConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ public void setAutoSwitch(boolean autoSwitch) {
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() {
Expand Down Expand Up @@ -328,26 +328,26 @@ private void doneDownload() {
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() {
Expand Down Expand Up @@ -475,7 +475,7 @@ public void onConnectionChanged(MoneroRpcConnection connection) {
// 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
Expand All @@ -495,7 +495,7 @@ public void onConnectionChanged(MoneroRpcConnection connection) {
// 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
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/haveno/core/app/WalletAppSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ void setRejectedTxErrorMessageHandler(Consumer<String> rejectedTxErrorMessageHan

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 = "";
Expand All @@ -279,7 +279,7 @@ else if (xmrConnectionService.isConnectionTor())

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");
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/haveno/core/offer/OfferBookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public void onSpentStatusChanged(Map<String, MoneroKeyImageSpentStatus> spentSta
}

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) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/haveno/core/offer/OpenOfferManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void onSpentStatusChanged(Map<String, MoneroKeyImageSpentStatus> spentSta
}

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() {
Expand Down
27 changes: 24 additions & 3 deletions core/src/main/java/haveno/core/trade/HavenoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand Down Expand Up @@ -418,12 +419,32 @@ public static String getBurnAddress() {
/**
* 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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public boolean isProxyApplied() {
}

public boolean isProxyApplied(boolean wasWalletSynced) {
return preferences.isProxyApplied(wasWalletSynced);
return preferences.isProxyApplied(wasWalletSynced) && xmrConnectionService.isProxyApplied();
}

public String getWalletPassword() {
Expand Down

0 comments on commit 4494af8

Please sign in to comment.