Skip to content

Commit

Permalink
WhonixTorControlReader: Handle all events
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Jun 1, 2024
1 parent 61e5ee5 commit a5c858f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
public class BootstrapEventParser {
public static Optional<BootstrapEvent> tryParse(String line) {
// 650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=50 TAG=loading_descriptors SUMMARY="Loading relay descriptors"
String[] parts = line.split(" ");
if (isStatusClientEvent(line)) {
String[] parts = line.split(" ");

if (isBootstrapEvent(parts)) {
BootstrapEvent bootstrapEvent = parseBootstrapEvent(parts);
return Optional.of(bootstrapEvent);
if (isBootstrapEvent(parts)) {
BootstrapEvent bootstrapEvent = parseBootstrapEvent(parts);
return Optional.of(bootstrapEvent);
}
}

return Optional.empty();
}

private static boolean isStatusClientEvent(String line) {
// 650 STATUS_CLIENT NOTICE CIRCUIT_ESTABLISHED
return line.startsWith("650 STATUS_CLIENT");
}

private static boolean isBootstrapEvent(String[] parts) {
// 650 STATUS_CLIENT NOTICE BOOTSTRAP PROGRESS=50 TAG=loading_descriptors SUMMARY="Loading relay descriptors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ public void start() {
String line;
while ((line = bufferedReader.readLine()) != null) {

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

if (bootstrapEventOptional.isPresent()) {
BootstrapEvent bootstrapEvent = bootstrapEventOptional.get();
bootstrapEventListeners.forEach(listener -> listener.onBootstrapStatusEvent(bootstrapEvent));
} else {
log.info("Unknown status client event: {}", line);
log.info("Unknown Tor event: {}", line);
}

} else {
Expand Down Expand Up @@ -81,8 +80,8 @@ public void removeBootstrapEventListener(BootstrapEventListener listener) {
bootstrapEventListeners.remove(listener);
}

private boolean isStatusClientEvent(String line) {
private boolean isEvent(String line) {
// 650 STATUS_CLIENT NOTICE CIRCUIT_ESTABLISHED
return line.startsWith("650 STATUS_CLIENT");
return line.startsWith("650");
}
}

0 comments on commit a5c858f

Please sign in to comment.