Skip to content

Commit

Permalink
Add ability to check whether a given onion service is online
Browse files Browse the repository at this point in the history
When another peer connects to us over Tor the connecting peer can lie
about its onion address. We can challenge the peer to sign a payload to
proof that the connecting peer has the private key of the provided onion
address. This change adds the ability to check whether the onion service
is known to the Tor directory servers.
  • Loading branch information
alvasw committed Dec 2, 2022
1 parent 4b522d4 commit 8263076
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ public CompletableFuture<Void> shutdown() {
public Optional<Address> getServerAddress(String serverId) {
return Optional.empty();
}

@Override
public boolean isAddressAvailable(Address address) {
try (Socket ignored = getSocket(address)) {
return true;
} catch (IOException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public Socket getSocket(Address address) throws IOException {
}
}

@Override
public boolean isAddressAvailable(Address address) {
throw new UnsupportedOperationException("isAddressAvailable needs to be implemented for I2P.");
}

@Override
public CompletableFuture<Void> shutdown() {
initializeCalled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public Socket getSocket(Address address) throws IOException {
return socket;
}

@Override
public boolean isAddressAvailable(Address address) {
return tor.isHiddenServiceAvailable(address.getHost());
}

public Optional<Socks5Proxy> getSocksProxy() throws IOException {
return Optional.of(tor.getSocks5Proxy(null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@ default Optional<Socks5Proxy> getSocksProxy() throws IOException {

Optional<Address> getServerAddress(String serverId);

boolean isAddressAvailable(Address address);

CompletableFuture<Void> shutdown();
}
4 changes: 4 additions & 0 deletions tor/src/main/java/bisq/tor/Tor.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ public Optional<String> getHostName(String serverId) {
return Optional.empty();
}

public boolean isHiddenServiceAvailable(String onionUrl) {
return torController.isHiddenServiceAvailable(onionUrl);
}

private void setState(State newState) {
log.info("Set new state {}", newState);
checkArgument(newState.ordinal() > state.get().ordinal(),
Expand Down
8 changes: 8 additions & 0 deletions tor/src/main/java/bisq/tor/TorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ int getProxyPort() throws IOException {
return Integer.parseInt(port);
}

boolean isHiddenServiceAvailable(String onionUrl) {
try {
return torControlConnection().isHSAvailable(onionUrl);
} catch (IOException e) {
return false;
}
}

TorControlConnection.CreateHiddenServiceResult createHiddenService(int hiddenServicePort,
int localPort) throws IOException {
assertState();
Expand Down

0 comments on commit 8263076

Please sign in to comment.