Merge pull request #6905 from alvasw/external_tor_wait_until_bootstrapped

tor: Wait until external Tor bootstrapped
This commit is contained in:
Alejandro García 2023-10-10 23:24:36 +00:00 committed by GitHub
commit 13c87b2a05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,12 +19,16 @@ package bisq.network.p2p.network;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import org.berndpruenster.netlayer.tor.ExternalTor; import org.berndpruenster.netlayer.tor.ExternalTor;
import org.berndpruenster.netlayer.tor.Tor; import org.berndpruenster.netlayer.tor.Tor;
import org.berndpruenster.netlayer.tor.TorCtlException; import org.berndpruenster.netlayer.tor.TorCtlException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
@ -47,8 +51,12 @@ public class RunningTor extends TorMode {
private final boolean useSafeCookieAuthentication; 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 boolean useSafeCookieAuthentication) { final String controlHost,
final int controlPort,
final String password,
final File cookieFile,
final boolean useSafeCookieAuthentication) {
super(torDir); super(torDir);
this.controlHost = controlHost; this.controlHost = controlHost;
this.controlPort = controlPort; this.controlPort = controlPort;
@ -60,24 +68,47 @@ public class RunningTor extends TorMode {
@Override @Override
public Tor getTor() throws IOException, TorCtlException { public Tor getTor() throws IOException, TorCtlException {
long ts1 = new Date().getTime(); long ts1 = new Date().getTime();
boolean retry = true;
long twoMinutesInMilli = 1000 * 60 * 2;
log.info("Connecting to running tor"); while (retry && ((new Date().getTime() - ts1) <= twoMinutesInMilli)) {
retry = false;
try {
log.info("Connecting to running tor");
Tor result; Tor result;
if (!password.isEmpty()) if (!password.isEmpty())
result = new ExternalTor(controlHost, controlPort, password); result = new ExternalTor(controlHost, controlPort, password);
else if (cookieFile != null && cookieFile.exists()) else if (cookieFile != null && cookieFile.exists())
result = new ExternalTor(controlHost, controlPort, cookieFile, useSafeCookieAuthentication); result = new ExternalTor(controlHost, controlPort, cookieFile, useSafeCookieAuthentication);
else else
result = new ExternalTor(controlHost, controlPort); result = new ExternalTor(controlHost, controlPort);
log.info( boolean isTorBootstrapped = result.control.waitUntilBootstrapped();
"\n################################################################\n" if (!isTorBootstrapped) {
+ "Connecting to Tor successful after {} ms. Start publishing hidden service.\n" log.error("Couldn't bootstrap Tor.");
+ "################################################################", }
(new Date().getTime() - ts1)); // takes usually a few seconds
return result; log.info(
"\n################################################################\n"
+ "Connecting to Tor successful after {} ms. Start publishing hidden service.\n"
+ "################################################################",
(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 @Override