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

Get SOCKS5 port from Tor daemon #2122

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 2 additions & 6 deletions network/tor/tor/src/main/java/bisq/tor/TorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class TorService implements Service {
private final AtomicBoolean isRunning = new AtomicBoolean();

private Optional<NativeTorProcess> torProcess = Optional.empty();
private Optional<Integer> socksPort = Optional.empty();
private Optional<TorSocksProxyFactory> torSocksProxyFactory = Optional.empty();

public TorService(TorTransportConfig transportConfig) {
Expand Down Expand Up @@ -92,7 +91,7 @@ public CompletableFuture<Boolean> initialize() {
nativeTorController.enableTorNetworking();
nativeTorController.waitUntilBootstrapped();

int port = socksPort.orElseThrow();
int port = nativeTorController.getSocksPort().orElseThrow();
torSocksProxyFactory = Optional.of(new TorSocksProxyFactory(port));
})
.thenApply(unused -> true);
Expand Down Expand Up @@ -153,13 +152,10 @@ private void installTorIfNotUpToDate() {
}

private void createTorrcConfigFile(Path dataDir, PasswordDigest hashedControlPassword) {
int socksPort = NetworkUtils.findFreeSystemPort();
this.socksPort = Optional.of(socksPort);

TorrcClientConfigFactory torrcClientConfigFactory = TorrcClientConfigFactory.builder()
.isTestNetwork(transportConfig.isTestNetwork())
.dataDir(dataDir)
.socksPort(socksPort)
.socksPort(NetworkUtils.findFreeSystemPort())
.hashedControlPassword(hashedControlPassword)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ public boolean isHiddenServiceAvailable(String onionUrl) {
}
}

public Optional<Integer> getSocksPort() {
try {
TorControlConnection controlConnection = torControlConnection.orElseThrow();
String socksListenersString = controlConnection.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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log an error or warning?

}
}

public void waitUntilBootstrapped() {
try {
while (true) {
Expand Down
Loading