Skip to content

Commit

Permalink
Merge pull request #6905 from alvasw/external_tor_wait_until_bootstra…
Browse files Browse the repository at this point in the history
…pped

tor: Wait until external Tor bootstrapped
  • Loading branch information
alejandrogarcia83 authored Oct 10, 2023
2 parents 336aac1 + 887fc39 commit 13c87b2
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions p2p/src/main/java/bisq/network/p2p/network/RunningTor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@

import java.io.File;
import java.io.IOException;

import java.util.Date;

import org.berndpruenster.netlayer.tor.ExternalTor;
import org.berndpruenster.netlayer.tor.Tor;
import org.berndpruenster.netlayer.tor.TorCtlException;

import java.net.ConnectException;
import java.net.UnknownHostException;

import lombok.extern.slf4j.Slf4j;

/**
Expand All @@ -47,8 +51,12 @@ public class RunningTor extends TorMode {
private final boolean useSafeCookieAuthentication;


public RunningTor(final File torDir, final String controlHost, final int controlPort, final String password, final File cookieFile,
final boolean useSafeCookieAuthentication) {
public RunningTor(final File torDir,
final String controlHost,
final int controlPort,
final String password,
final File cookieFile,
final boolean useSafeCookieAuthentication) {
super(torDir);
this.controlHost = controlHost;
this.controlPort = controlPort;
Expand All @@ -60,24 +68,47 @@ public RunningTor(final File torDir, final String controlHost, final int control
@Override
public Tor getTor() throws IOException, TorCtlException {
long ts1 = new Date().getTime();
boolean retry = true;
long twoMinutesInMilli = 1000 * 60 * 2;

while (retry && ((new Date().getTime() - ts1) <= twoMinutesInMilli)) {
retry = false;
try {
log.info("Connecting to running tor");

Tor result;
if (!password.isEmpty())
result = new ExternalTor(controlHost, controlPort, password);
else if (cookieFile != null && cookieFile.exists())
result = new ExternalTor(controlHost, controlPort, cookieFile, useSafeCookieAuthentication);
else
result = new ExternalTor(controlHost, controlPort);

boolean isTorBootstrapped = result.control.waitUntilBootstrapped();
if (!isTorBootstrapped) {
log.error("Couldn't bootstrap Tor.");
}

log.info("Connecting to running tor");
log.info(
"\n################################################################\n"
+ "Connecting to Tor successful after {} ms. Start publishing hidden service.\n"
+ "################################################################",
(new Date().getTime() - ts1)); // takes usually a few seconds

Tor result;
if (!password.isEmpty())
result = new ExternalTor(controlHost, controlPort, password);
else if (cookieFile != null && cookieFile.exists())
result = new ExternalTor(controlHost, controlPort, cookieFile, useSafeCookieAuthentication);
else
result = new ExternalTor(controlHost, controlPort);
return result;
} catch (Exception e) {
// netlayer throws UnknownHostException when tor docker container is not ready yet.
// netlayer throws ConnectException before tor container bind to control port.
if (e instanceof UnknownHostException || e instanceof ConnectException) {
log.warn("Couldn't connect to Tor control port. Retrying...", e);
retry = true;
}

log.info(
"\n################################################################\n"
+ "Connecting to Tor successful after {} ms. Start publishing hidden service.\n"
+ "################################################################",
(new Date().getTime() - ts1)); // takes usually a few seconds
log.error("Couldn't connect to Tor.", e);
}
}

return result;
return null;
}

@Override
Expand Down

0 comments on commit 13c87b2

Please sign in to comment.