Skip to content

Commit

Permalink
WhonixTorController: Add BootstrapEventListener support
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Jun 1, 2024
1 parent f86a6ca commit 3d2f632
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package bisq.tor.controller;

import bisq.tor.controller.events.events.BootstrapEvent;
import bisq.tor.controller.events.listener.BootstrapEventListener;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
public class WhonixTorControlReader implements AutoCloseable {
private final BufferedReader bufferedReader;
private final BlockingQueue<String> replies = new LinkedBlockingQueue<>();
private final List<BootstrapEventListener> bootstrapEventListeners = new CopyOnWriteArrayList<>();

private Optional<Thread> workerThread = Optional.empty();

public WhonixTorControlReader(InputStream inputStream) {
Expand All @@ -29,10 +34,11 @@ public void start() {
while ((line = bufferedReader.readLine()) != null) {

if (isStatusClientEvent(line)) {
Optional<BootstrapEvent> bootstrapEvent = BootstrapEventParser.tryParse(line);
Optional<BootstrapEvent> bootstrapEventOptional = BootstrapEventParser.tryParse(line);

if (bootstrapEvent.isPresent()) {
log.info("{}", bootstrapEvent.get());
if (bootstrapEventOptional.isPresent()) {
BootstrapEvent bootstrapEvent = bootstrapEventOptional.get();
bootstrapEventListeners.forEach(listener -> listener.onBootstrapStatusEvent(bootstrapEvent));
} else {
log.info("Unknown status client event: {}", line);
}
Expand Down Expand Up @@ -67,6 +73,14 @@ public String readLine() {
}
}

public void addBootstrapEventListener(BootstrapEventListener listener) {
bootstrapEventListeners.add(listener);
}

public void removeBootstrapEventListener(BootstrapEventListener listener) {
bootstrapEventListeners.remove(listener);
}

private boolean isStatusClientEvent(String line) {
// 650 STATUS_CLIENT NOTICE CIRCUIT_ESTABLISHED
return line.startsWith("650 STATUS_CLIENT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bisq.common.encoding.Hex;
import bisq.security.keys.TorKeyPair;
import bisq.tor.controller.events.listener.BootstrapEventListener;
import net.freehaven.tor.control.PasswordDigest;

import java.io.*;
Expand Down Expand Up @@ -94,6 +95,14 @@ public void takeOwnership() throws IOException {
}
}

public void addBootstrapEventListener(BootstrapEventListener listener) {
whonixTorControlReader.addBootstrapEventListener(listener);
}

public void removeBootstrapEventListener(BootstrapEventListener listener) {
whonixTorControlReader.removeBootstrapEventListener(listener);
}

private void sendCommand(String command) throws IOException {
byte[] commandBytes = command.getBytes(StandardCharsets.US_ASCII);
outputStream.write(commandBytes);
Expand Down

0 comments on commit 3d2f632

Please sign in to comment.