From 51a8fcde3d43120e9ed08d4e4d4ce5ae9b712118 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Tue, 11 Jun 2024 00:57:00 +0000 Subject: [PATCH] TorController: Make TorControlProtocol field final --- .../bisq/tor/controller/TorController.java | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/network/tor/tor/src/main/java/bisq/tor/controller/TorController.java b/network/tor/tor/src/main/java/bisq/tor/controller/TorController.java index 0d1d8bfa95..df78fa1a23 100644 --- a/network/tor/tor/src/main/java/bisq/tor/controller/TorController.java +++ b/network/tor/tor/src/main/java/bisq/tor/controller/TorController.java @@ -15,7 +15,6 @@ import lombok.extern.slf4j.Slf4j; import net.freehaven.tor.control.PasswordDigest; -import java.io.IOException; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Collections; @@ -29,6 +28,8 @@ @Slf4j public class TorController implements BootstrapEventListener, HsDescEventListener { + private final TorControlProtocol torControlProtocol = new TorControlProtocol(); + private final int bootstrapTimeout; // in ms private final int hsUploadTimeout; // in ms private final CountDownLatch isBootstrappedCountdownLatch = new CountDownLatch(1); @@ -39,8 +40,6 @@ public class TorController implements BootstrapEventListener, HsDescEventListene private final Map> pendingIsOnionServiceOnlineLookupFutureMap = new ConcurrentHashMap<>(); - private Optional torControlProtocol = Optional.empty(); - public TorController(int bootstrapTimeout, int hsUploadTimeout) { this.bootstrapTimeout = bootstrapTimeout; this.hsUploadTimeout = hsUploadTimeout; @@ -55,7 +54,7 @@ public void initialize(int controlPort, PasswordDigest hashedControlPassword) { } public void shutdown() { - torControlProtocol.ifPresent(TorControlProtocol::close); + torControlProtocol.close(); } public void bootstrapTor() { @@ -70,7 +69,6 @@ public CompletableFuture isOnionServiceOnline(String onionAddress) { pendingIsOnionServiceOnlineLookupFutureMap.put(onionAddress, onionServiceLookupCompletableFuture); subscribeToHsDescEvents(); - TorControlProtocol torControlProtocol = getTorControlProtocol(); String serviceId = onionAddress.replace(".onion", ""); torControlProtocol.hsFetch(serviceId); @@ -88,7 +86,6 @@ public void publish(TorKeyPair torKeyPair, int onionServicePort, int localPort) pendingOnionServicePublishLatchMap.put(onionAddress, onionServicePublishedLatch); subscribeToHsDescEvents(); - TorControlProtocol torControlProtocol = getTorControlProtocol(); torControlProtocol.addOnion(torKeyPair, onionServicePort, localPort); boolean isSuccess = onionServicePublishedLatch.await(hsUploadTimeout, TimeUnit.MILLISECONDS); @@ -101,9 +98,7 @@ public void publish(TorKeyPair torKeyPair, int onionServicePort, int localPort) } public int getSocksPort() { - TorControlProtocol torControlProtocol = getTorControlProtocol(); String socksListenersString = torControlProtocol.getInfo("net/listeners/socks"); - String socksListener; if (socksListenersString.contains(" ")) { String[] socksPorts = socksListenersString.split(" "); @@ -162,47 +157,34 @@ public void onHsDescEvent(HsDescEvent hsDescEvent) { } private void initialize(int controlPort, Optional hashedControlPassword) { - var torControlProtocol = new TorControlProtocol(); - this.torControlProtocol = Optional.of(torControlProtocol); - torControlProtocol.initialize(controlPort); hashedControlPassword.ifPresent(torControlProtocol::authenticate); } private void bindToBisq() { - TorControlProtocol torControlProtocol = getTorControlProtocol(); torControlProtocol.takeOwnership(); torControlProtocol.resetConf(NativeTorProcess.ARG_OWNER_PID); } private void subscribeToBootstrapEvents() { - TorControlProtocol torControlProtocol = getTorControlProtocol(); torControlProtocol.addBootstrapEventListener(this); torControlProtocol.setEvents(List.of("STATUS_CLIENT")); } private void subscribeToHsDescEvents() { - TorControlProtocol torControlProtocol = getTorControlProtocol(); torControlProtocol.addHsDescEventListener(this); torControlProtocol.setEvents(List.of("HS_DESC")); } private void enableNetworking() { - TorControlProtocol torControlProtocol = getTorControlProtocol(); torControlProtocol.setConfig(TorrcClientConfigFactory.DISABLE_NETWORK_CONFIG_KEY, "0"); } private void waitUntilBootstrapped() { try { while (true) { - if (torControlProtocol.isEmpty()) { - throw new TorBootstrapFailedException("Tor is not initializing."); - } - boolean isSuccess = isBootstrappedCountdownLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS); - if (isSuccess) { - TorControlProtocol torControlProtocol = this.torControlProtocol.get(); torControlProtocol.removeBootstrapEventListener(this); torControlProtocol.setEvents(Collections.emptyList()); break; @@ -221,8 +203,4 @@ private boolean isBootstrapTimeoutTriggered() { Instant bootstrapTimeoutAgo = Instant.now().minus(bootstrapTimeout, ChronoUnit.MILLIS); return bootstrapTimeoutAgo.isAfter(timestamp); } - - private TorControlProtocol getTorControlProtocol() { - return this.torControlProtocol.orElseThrow(); - } }