Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tor startup issues on Windows #2288

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading