Skip to content

Commit

Permalink
Merge pull request #2470 from blabno/api-tor
Browse files Browse the repository at this point in the history
API over TOR
  • Loading branch information
ManfredKarrer authored Mar 1, 2019
2 parents ef21beb + 39e5146 commit 5ca73fc
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions api/src/main/java/bisq/api/http/service/HttpApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

import javax.servlet.DispatcherType;

import org.berndpruenster.netlayer.tor.HsContainer;
import org.berndpruenster.netlayer.tor.Tor;
import org.berndpruenster.netlayer.tor.TorCtlException;

import javax.inject.Inject;

import java.net.InetSocketAddress;

import java.io.IOException;

import java.util.EnumSet;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -31,13 +37,12 @@
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;

@SuppressWarnings("Duplicates")
@Slf4j
public class HttpApiServer {
private final HttpApiInterfaceV1 httpApiInterfaceV1;
private final ApiPasswordManager apiPasswordManager;
private final BisqEnvironment bisqEnvironment;
private final HttpApiInterfaceV1 httpApiInterfaceV1;
private final TokenRegistry tokenRegistry;
private final ApiPasswordManager apiPasswordManager;

@Inject
public HttpApiServer(ApiPasswordManager apiPasswordManager, BisqEnvironment bisqEnvironment, HttpApiInterfaceV1 httpApiInterfaceV1,
Expand All @@ -59,7 +64,8 @@ public void startServer() {
server.setRequestLog(new Slf4jRequestLog());
server.start();
log.info("HTTP API started on {}", socketAddress);
} catch (Exception e) {
startTorIfNeeded();
} catch (Exception | TorCtlException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -98,4 +104,19 @@ private void setupAuth(ServletContextHandler appContextHandler) {
AuthFilter authFilter = new AuthFilter(apiPasswordManager, tokenRegistry);
appContextHandler.addFilter(new FilterHolder(authFilter), "/*", EnumSet.allOf(DispatcherType.class));
}

/**
* If Bisq is configured to use start Tor then the default Tor instance should be available
* by the time this method is executed.
*/
private void startTorIfNeeded() throws IOException, TorCtlException {
Tor tor = Tor.getDefault();
if (null == tor) {
log.info("Tor not started so API will be available only locally");
return;
}
// TODO how to log that service has been published?
final HsContainer hsContainer = tor.publishHiddenService("api", 80, bisqEnvironment.getHttpApiPort());
log.info("HTTP API Tor hostname: {}", hsContainer.getHostname$tor());
}
}

0 comments on commit 5ca73fc

Please sign in to comment.