From fbc95acbfdd5ec78661b366aeee93aee60565aa6 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Fri, 26 Apr 2024 18:16:25 +0200 Subject: [PATCH] Get SOCKS5 port from Tor daemon We don't know the SOCKS5 port when we're using to a running Tor daemon. This change is needed to support external Tor instances. Ref: #1894 --- .../src/main/java/bisq/tor/TorService.java | 8 ++----- .../tor/controller/NativeTorController.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) 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 3f49140d98..97ab41d398 100644 --- a/network/tor/tor/src/main/java/bisq/tor/TorService.java +++ b/network/tor/tor/src/main/java/bisq/tor/TorService.java @@ -53,7 +53,6 @@ public class TorService implements Service { private final AtomicBoolean isRunning = new AtomicBoolean(); private Optional torProcess = Optional.empty(); - private Optional socksPort = Optional.empty(); private Optional torSocksProxyFactory = Optional.empty(); public TorService(TorTransportConfig transportConfig) { @@ -92,7 +91,7 @@ public CompletableFuture initialize() { nativeTorController.enableTorNetworking(); nativeTorController.waitUntilBootstrapped(); - int port = socksPort.orElseThrow(); + int port = nativeTorController.getSocksPort().orElseThrow(); torSocksProxyFactory = Optional.of(new TorSocksProxyFactory(port)); }) .thenApply(unused -> true); @@ -153,13 +152,10 @@ private void installTorIfNotUpToDate() { } private void createTorrcConfigFile(Path dataDir, PasswordDigest hashedControlPassword) { - int socksPort = NetworkUtils.findFreeSystemPort(); - this.socksPort = Optional.of(socksPort); - TorrcClientConfigFactory torrcClientConfigFactory = TorrcClientConfigFactory.builder() .isTestNetwork(transportConfig.isTestNetwork()) .dataDir(dataDir) - .socksPort(socksPort) + .socksPort(NetworkUtils.findFreeSystemPort()) .hashedControlPassword(hashedControlPassword) .build(); diff --git a/network/tor/tor/src/main/java/bisq/tor/controller/NativeTorController.java b/network/tor/tor/src/main/java/bisq/tor/controller/NativeTorController.java index e00417482a..32c00fd4f6 100644 --- a/network/tor/tor/src/main/java/bisq/tor/controller/NativeTorController.java +++ b/network/tor/tor/src/main/java/bisq/tor/controller/NativeTorController.java @@ -139,6 +139,30 @@ public boolean isHiddenServiceAvailable(String onionUrl) { } } + public Optional getSocksPort() { + try { + TorControlConnection controlConnection = torControlConnection.orElseThrow(); + String socksListenersString = controlConnection.getInfo("net/listeners/socks"); + + String socksListener; + if (socksListenersString.contains(" ")) { + String[] socksPorts = socksListenersString.split(" "); + socksListener = socksPorts[0]; + } else { + socksListener = socksListenersString; + } + + // "127.0.0.1:12345" + socksListener = socksListener.replace("\"", ""); + String portString = socksListener.split(":")[1]; + + int port = Integer.parseInt(portString); + return Optional.of(port); + } catch (IOException e) { + return Optional.empty(); + } + } + public void waitUntilBootstrapped() { try { while (true) {