Skip to content

Commit

Permalink
Let Tor pick SOCKS5 port
Browse files Browse the repository at this point in the history
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: bisq-network#1798
  • Loading branch information
alvasw committed Apr 26, 2024
1 parent fbc95ac commit 8e36f95
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> generate() {
Map<String, String> torConfigMap = baseTorrcConfigGenerator.generate();
torConfigMap.put("SocksPort", String.valueOf(socksPort));
torConfigMap.put("SocksPort", "auto");
return torConfigMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +37,6 @@ public static TorrcConfigGenerator clientTorrcGenerator(TorNode clientNode) {
var testNetworkTorrcGenerator = testNetworkTorrcGenerator(clientNode);
return ClientTorrcGenerator.builder()
.baseTorrcConfigGenerator(testNetworkTorrcGenerator)
.socksPort(NetworkUtils.findFreeSystemPort())
.build();
}

Expand Down
2 changes: 0 additions & 2 deletions network/tor/tor/src/main/java/bisq/tor/TorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -62,7 +59,6 @@ private TorrcConfigGenerator clientTorrcGenerator() {

return ClientTorrcGenerator.builder()
.baseTorrcConfigGenerator(baseTorrcGenerator)
.socksPort(socksPort)
.build();
}

Expand Down

0 comments on commit 8e36f95

Please sign in to comment.