From 8e36f955da06c0b64ca7afc856592adff9c116db Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Fri, 26 Apr 2024 18:24:48 +0200 Subject: [PATCH] Let Tor pick SOCKS5 port To select the Tor SOCKS5 port, we bind to a random port, close it and pass the port number to Tor. It can happen that Tor tries to bind to the port before it is closed. In this case, the Tor startup will fail. Ref: #1798 --- .../bisq/network/tor/common/torrc/ClientTorrcGenerator.java | 6 ++---- .../torrc/TestNetworkTorrcGeneratorFactory.java | 2 -- network/tor/tor/src/main/java/bisq/tor/TorService.java | 2 -- .../src/main/java/bisq/tor/TorrcClientConfigFactory.java | 4 ---- 4 files changed, 2 insertions(+), 12 deletions(-) diff --git a/network/tor/tor-common/src/main/java/bisq/network/tor/common/torrc/ClientTorrcGenerator.java b/network/tor/tor-common/src/main/java/bisq/network/tor/common/torrc/ClientTorrcGenerator.java index 5f0870640b..4e4034ab77 100644 --- a/network/tor/tor-common/src/main/java/bisq/network/tor/common/torrc/ClientTorrcGenerator.java +++ b/network/tor/tor-common/src/main/java/bisq/network/tor/common/torrc/ClientTorrcGenerator.java @@ -24,17 +24,15 @@ @Builder public class ClientTorrcGenerator implements TorrcConfigGenerator { private final TorrcConfigGenerator baseTorrcConfigGenerator; - private final int socksPort; - public ClientTorrcGenerator(TorrcConfigGenerator baseTorrcConfigGenerator, int socksPort) { + public ClientTorrcGenerator(TorrcConfigGenerator baseTorrcConfigGenerator) { this.baseTorrcConfigGenerator = baseTorrcConfigGenerator; - this.socksPort = socksPort; } @Override public Map generate() { Map torConfigMap = baseTorrcConfigGenerator.generate(); - torConfigMap.put("SocksPort", String.valueOf(socksPort)); + torConfigMap.put("SocksPort", "auto"); return torConfigMap; } } diff --git a/network/tor/tor-local-network/src/main/java/bisq/tor/local_network/torrc/TestNetworkTorrcGeneratorFactory.java b/network/tor/tor-local-network/src/main/java/bisq/tor/local_network/torrc/TestNetworkTorrcGeneratorFactory.java index 64851380e9..e5aace90a7 100644 --- a/network/tor/tor-local-network/src/main/java/bisq/tor/local_network/torrc/TestNetworkTorrcGeneratorFactory.java +++ b/network/tor/tor-local-network/src/main/java/bisq/tor/local_network/torrc/TestNetworkTorrcGeneratorFactory.java @@ -17,7 +17,6 @@ package bisq.tor.local_network.torrc; -import bisq.common.util.NetworkUtils; import bisq.network.tor.common.torrc.*; import bisq.tor.local_network.TorNode; @@ -38,7 +37,6 @@ public static TorrcConfigGenerator clientTorrcGenerator(TorNode clientNode) { var testNetworkTorrcGenerator = testNetworkTorrcGenerator(clientNode); return ClientTorrcGenerator.builder() .baseTorrcConfigGenerator(testNetworkTorrcGenerator) - .socksPort(NetworkUtils.findFreeSystemPort()) .build(); } diff --git a/network/tor/tor/src/main/java/bisq/tor/TorService.java b/network/tor/tor/src/main/java/bisq/tor/TorService.java index 97ab41d398..fe4195071a 100644 --- a/network/tor/tor/src/main/java/bisq/tor/TorService.java +++ b/network/tor/tor/src/main/java/bisq/tor/TorService.java @@ -19,7 +19,6 @@ import bisq.common.application.Service; import bisq.common.observable.Observable; -import bisq.common.util.NetworkUtils; import bisq.network.tor.common.torrc.TorrcFileGenerator; import bisq.tor.controller.NativeTorController; import bisq.tor.controller.events.events.BootstrapEvent; @@ -155,7 +154,6 @@ private void createTorrcConfigFile(Path dataDir, PasswordDigest hashedControlPas TorrcClientConfigFactory torrcClientConfigFactory = TorrcClientConfigFactory.builder() .isTestNetwork(transportConfig.isTestNetwork()) .dataDir(dataDir) - .socksPort(NetworkUtils.findFreeSystemPort()) .hashedControlPassword(hashedControlPassword) .build(); diff --git a/network/tor/tor/src/main/java/bisq/tor/TorrcClientConfigFactory.java b/network/tor/tor/src/main/java/bisq/tor/TorrcClientConfigFactory.java index a0f156ad89..5a5873e7cc 100644 --- a/network/tor/tor/src/main/java/bisq/tor/TorrcClientConfigFactory.java +++ b/network/tor/tor/src/main/java/bisq/tor/TorrcClientConfigFactory.java @@ -34,16 +34,13 @@ public class TorrcClientConfigFactory { private final boolean isTestNetwork; private final Path dataDir; - private final int socksPort; private final PasswordDigest hashedControlPassword; public TorrcClientConfigFactory(boolean isTestNetwork, Path dataDir, - int socksPort, PasswordDigest hashedControlPassword) { this.isTestNetwork = isTestNetwork; this.dataDir = dataDir; - this.socksPort = socksPort; this.hashedControlPassword = hashedControlPassword; } @@ -62,7 +59,6 @@ private TorrcConfigGenerator clientTorrcGenerator() { return ClientTorrcGenerator.builder() .baseTorrcConfigGenerator(baseTorrcGenerator) - .socksPort(socksPort) .build(); }