Skip to content

Commit

Permalink
Merge pull request #2247 from alvasw/tor_control_protocol_implement_g…
Browse files Browse the repository at this point in the history
…etinfo_command

TorControlProtocol: Implement GETINFO command
  • Loading branch information
djing-chan authored Jun 4, 2024
2 parents e765243 + 437e897 commit 620b0d7
Show file tree
Hide file tree
Showing 3 changed files with 24 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 @@ -55,6 +55,16 @@ public void addOnion(TorKeyPair torKeyPair, int onionPort, int localPort) throws
String reply = receiveReply();
}

public String getInfo(String keyword) throws IOException {
String command = "GETINFO " + keyword + "\r\n";
sendCommand(command);
String reply = receiveReply();
if (!reply.startsWith("250-")) {
throw new ControlCommandFailedException("Couldn't get info: " + keyword);
}
return reply;
}

public void resetConf(String configName) throws IOException {
String command = "RESETCONF " + configName + "\r\n";
sendCommand(command);
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 620b0d7

Please sign in to comment.