Skip to content

Commit

Permalink
TorController: Make TorControlProtocol field final
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Jun 11, 2024
1 parent c390bef commit 51a8fcd
Showing 1 changed file with 3 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -39,8 +40,6 @@ public class TorController implements BootstrapEventListener, HsDescEventListene
private final Map<String, CompletableFuture<Boolean>> pendingIsOnionServiceOnlineLookupFutureMap =
new ConcurrentHashMap<>();

private Optional<TorControlProtocol> torControlProtocol = Optional.empty();

public TorController(int bootstrapTimeout, int hsUploadTimeout) {
this.bootstrapTimeout = bootstrapTimeout;
this.hsUploadTimeout = hsUploadTimeout;
Expand All @@ -55,7 +54,7 @@ public void initialize(int controlPort, PasswordDigest hashedControlPassword) {
}

public void shutdown() {
torControlProtocol.ifPresent(TorControlProtocol::close);
torControlProtocol.close();
}

public void bootstrapTor() {
Expand All @@ -70,7 +69,6 @@ public CompletableFuture<Boolean> isOnionServiceOnline(String onionAddress) {
pendingIsOnionServiceOnlineLookupFutureMap.put(onionAddress, onionServiceLookupCompletableFuture);
subscribeToHsDescEvents();

TorControlProtocol torControlProtocol = getTorControlProtocol();
String serviceId = onionAddress.replace(".onion", "");
torControlProtocol.hsFetch(serviceId);

Expand All @@ -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);
Expand All @@ -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(" ");
Expand Down Expand Up @@ -162,47 +157,34 @@ public void onHsDescEvent(HsDescEvent hsDescEvent) {
}

private void initialize(int controlPort, Optional<PasswordDigest> 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;
Expand All @@ -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();
}
}

0 comments on commit 51a8fcd

Please sign in to comment.