Skip to content

Commit

Permalink
Merge pull request #2248 from alvasw/tor_controller_implement_get_soc…
Browse files Browse the repository at this point in the history
…ks_port

TorController: Implement getSocksPort
  • Loading branch information
djing-chan authored Jun 4, 2024
2 parents 620b0d7 + 7891909 commit 6306941
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ public void bootstrapTor() throws IOException {
waitUntilBootstrapped();
}

public Optional<Integer> getSocksPort() {
try {
TorControlProtocol torControlProtocol = getTorControlProtocol();
String socksListenersString = torControlProtocol.getInfo("net/listeners/socks");

String socksListener;
if (socksListenersString.contains(" ")) {
String[] socksPorts = socksListenersString.split(" ");
socksListener = socksPorts[0];
} else {
socksListener = socksListenersString;
}

// "127.0.0.1:12345"
socksListener = socksListener.replace("\"", "");
String portString = socksListener.split(":")[1];

int port = Integer.parseInt(portString);
return Optional.of(port);
} catch (IOException e) {
return Optional.empty();
}
}

@Override
public void onBootstrapStatusEvent(BootstrapEvent bootstrapEvent) {
log.info("Tor bootstrap event: {}", bootstrapEvent);
Expand Down

0 comments on commit 6306941

Please sign in to comment.