Skip to content

Commit

Permalink
Merge pull request #581 from alvasw/add_ability_to_check_whether_onio…
Browse files Browse the repository at this point in the history
…n_service_online

Add ability to check whether a given onion service is online
  • Loading branch information
alvasw authored Dec 2, 2022
2 parents 4b522d4 + 8263076 commit 96d496d
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 96d496d

Please sign in to comment.