Skip to content

Commit

Permalink
Merge pull request bisq-network#2288 from alvasw/fix_tor_startup_bug_…
Browse files Browse the repository at this point in the history
…on_windows

Fix Tor startup issues on Windows
  • Loading branch information
djing-chan authored Jun 12, 2024
2 parents 869e05b + 307099b commit 872adf9
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 872adf9

Please sign in to comment.