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

API over TOR #2470

Merged
merged 1 commit into from
Mar 1, 2019
Merged
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
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());
}
}