Skip to content

Commit

Permalink
Fix Tor startup issues on Windows
Browse files Browse the repository at this point in the history
On Windows Tor writes its control port number to a file before the port
is accessible by other applications. Users reported that Bisq throws a
ConnectException (ControlCommandFailedException) at startup. All logs,
I received so far, are from Windows users.

Fixes bisq-network#2239, bisq-network#2253
  • Loading branch information
alvasw committed Jun 11, 2024
1 parent 869e05b commit 307099b
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
Expand All @@ -20,6 +21,8 @@
import java.util.stream.Stream;

public class TorControlProtocol implements AutoCloseable {
private static final int MAX_CONNECTION_ATTEMPTS = 10;

private final Socket controlSocket;
private final WhonixTorControlReader whonixTorControlReader;
private Optional<OutputStream> outputStream = Optional.empty();
Expand All @@ -35,11 +38,10 @@ public TorControlProtocol() {

public void initialize(int port) {
try {
var socketAddress = new InetSocketAddress("127.0.0.1", port);
controlSocket.connect(socketAddress);
connectToTor(port);
whonixTorControlReader.start(controlSocket.getInputStream());
outputStream = Optional.of(controlSocket.getOutputStream());
} catch (IOException e) {
} catch (IOException | InterruptedException e) {
throw new CannotConnectWithTorException(e);
}
}
Expand Down Expand Up @@ -151,6 +153,22 @@ public void removeHsDescEventListener(HsDescEventListener listener) {
whonixTorControlReader.removeHsDescEventListener(listener);
}

private void connectToTor(int port) throws InterruptedException {
int connectionAttempt = 0;
while (connectionAttempt < MAX_CONNECTION_ATTEMPTS) {
try {
var socketAddress = new InetSocketAddress("127.0.0.1", port);
controlSocket.connect(socketAddress);
break;
} catch (ConnectException e) {
connectionAttempt++;
Thread.sleep(200);
} catch (IOException e) {
throw new CannotConnectWithTorException(e);
}
}
}

private void sendCommand(String command) {
try {
@SuppressWarnings("resource") OutputStream outputStream = this.outputStream.orElseThrow();
Expand Down

0 comments on commit 307099b

Please sign in to comment.