tor: Wait until external Tor bootstrapped

This commit is contained in:
Alva Swanson 2023-10-10 17:46:58 +02:00
parent 49bc7267b3
commit 887fc39509
No known key found for this signature in database
GPG Key ID: 004760E77F753090

View File

@ -19,12 +19,16 @@ package bisq.network.p2p.network;
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;
/**
@ -47,7 +51,11 @@ 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,
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;
@ -60,7 +68,12 @@ public class RunningTor extends TorMode {
@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;
@ -71,6 +84,11 @@ public class RunningTor extends TorMode {
else
result = new ExternalTor(controlHost, controlPort);
boolean isTorBootstrapped = result.control.waitUntilBootstrapped();
if (!isTorBootstrapped) {
log.error("Couldn't bootstrap Tor.");
}
log.info(
"\n################################################################\n"
+ "Connecting to Tor successful after {} ms. Start publishing hidden service.\n"
@ -78,6 +96,19 @@ public class RunningTor extends TorMode {
(new Date().getTime() - ts1)); // takes usually a few seconds
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.error("Couldn't connect to Tor.", e);
}
}
return null;
}
@Override